- Replaced inquirer with @inquirer/prompts in various command files for improved prompt handling. - Updated confirmation and input prompts in commands such as app, config, deploy, login, and others. - Added a new command 'cc' for switching between Claude code models with appropriate configurations. - Enhanced user experience by ensuring prompts are more streamlined and consistent across the application.
212 lines
5.5 KiB
TypeScript
212 lines
5.5 KiB
TypeScript
import { program as app, Command } from '@/program.ts';
|
|
import { checkFileExists, getConfig, writeConfig } from '@/module/index.ts';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
import { chalk } from '@/module/chalk.ts';
|
|
import { confirm, input } from '@inquirer/prompts';
|
|
|
|
// 设置工作目录
|
|
const setWorkdir = async (options: { workdir: string }) => {
|
|
const execPath = process.cwd();
|
|
let flag = false;
|
|
let config = getConfig();
|
|
const workdir = options.workdir;
|
|
if (workdir) {
|
|
let finalPath: string;
|
|
if (workdir.startsWith('/')) {
|
|
// 如果以 / 开头,处理为绝对路径
|
|
finalPath = workdir;
|
|
} else {
|
|
// 否则,处理为相对路径
|
|
finalPath = path.join(execPath, workdir);
|
|
}
|
|
if (!checkFileExists(finalPath)) {
|
|
console.log('路径不存在');
|
|
fs.mkdirSync(finalPath, { recursive: true });
|
|
}
|
|
const confirmed = await confirm({
|
|
message: `Are you sure you want to set the workdir to: ${finalPath}?`,
|
|
default: false,
|
|
});
|
|
if (confirmed) {
|
|
flag = true;
|
|
config.workdir = finalPath;
|
|
console.log(chalk.green(`set workdir success:`, finalPath));
|
|
} else {
|
|
console.log('Cancel set workdir');
|
|
}
|
|
}
|
|
if (flag) {
|
|
writeConfig(config);
|
|
}
|
|
};
|
|
const command = new Command('config')
|
|
.description('')
|
|
.option('-d, --dev <dev>', 'Specify dev')
|
|
.option('-s --set <set>', 'set config')
|
|
.option('-g --get <get>', 'get config')
|
|
.option('-r --remove <remove>', 'remove config')
|
|
.option('-v --value <value>', 'value')
|
|
.option('-w --workdir <path>', 'web config')
|
|
.action(async (options) => {
|
|
const { dev, workdir } = options || {};
|
|
let config = getConfig();
|
|
let flag = false;
|
|
const execPath = process.cwd();
|
|
if (dev === 'true' || dev === 'false') {
|
|
flag = true;
|
|
const config = getConfig();
|
|
if (dev === 'true') {
|
|
config.dev = true;
|
|
} else {
|
|
config.dev = false;
|
|
}
|
|
}
|
|
if (workdir) {
|
|
let finalPath: string;
|
|
const workdir = options.workdir;
|
|
|
|
if (workdir.startsWith('/')) {
|
|
// 如果以 / 开头,处理为绝对路径
|
|
finalPath = workdir;
|
|
} else {
|
|
// 否则,处理为相对路径
|
|
finalPath = path.join(execPath, workdir);
|
|
}
|
|
if (!checkFileExists(finalPath)) {
|
|
console.log('路径不存在');
|
|
fs.mkdirSync(finalPath, { recursive: true });
|
|
}
|
|
const confirmed = await confirm({
|
|
message: `Are you sure you want to set the workdir to: ${finalPath}?`,
|
|
default: false,
|
|
});
|
|
if (confirmed) {
|
|
flag = true;
|
|
config.workdir = finalPath;
|
|
console.log(chalk.green(`set workdir success:`, finalPath));
|
|
} else {
|
|
console.log('Cancel set workdir');
|
|
}
|
|
}
|
|
if (options.set) {
|
|
const key = options.set;
|
|
let value = options.value;
|
|
if (!value) {
|
|
value = await input({
|
|
message: `Enter your ${key}:(current: ${config[key]})`,
|
|
});
|
|
}
|
|
if (key && value) {
|
|
flag = true;
|
|
config[key] = value;
|
|
}
|
|
}
|
|
if (options.remove) {
|
|
const key = options.remove;
|
|
if (key) {
|
|
flag = true;
|
|
delete config[key];
|
|
}
|
|
}
|
|
|
|
if (flag) {
|
|
writeConfig(config);
|
|
}
|
|
});
|
|
|
|
const setCommand = new Command('set')
|
|
.argument('<key>')
|
|
.argument('[value]', 'value', 'not_input')
|
|
.description('set config')
|
|
.action(async (key, value) => {
|
|
const config = getConfig();
|
|
if (!key) {
|
|
console.log('key is empty');
|
|
return;
|
|
}
|
|
let flag = false;
|
|
if (value === 'not_input') {
|
|
value = await input({
|
|
message: `Enter your ${key}:(current: ${config[key]})`,
|
|
});
|
|
}
|
|
if (key === 'workdir') {
|
|
await setWorkdir({ workdir: value });
|
|
return;
|
|
}
|
|
const transformValue = (value: string) => {
|
|
if (value === 'true') {
|
|
return true;
|
|
}
|
|
if (value === 'false') {
|
|
return false;
|
|
}
|
|
// 如果是数字
|
|
if (!isNaN(Number(value))) {
|
|
return Number(value);
|
|
}
|
|
return value;
|
|
};
|
|
const newValue = transformValue(value);
|
|
if (key && value) {
|
|
flag = true;
|
|
if (key === 'dev') {
|
|
if (value === 'true') {
|
|
config.dev = true;
|
|
} else {
|
|
config.dev = false;
|
|
}
|
|
} else {
|
|
config[key] = value;
|
|
}
|
|
console.log(chalk.green(`set ${key} success:`, config.key));
|
|
}
|
|
if (flag) {
|
|
writeConfig(config);
|
|
}
|
|
});
|
|
|
|
command.addCommand(setCommand);
|
|
const getCommand = new Command('get')
|
|
.argument('[key]', 'key')
|
|
.description('get config')
|
|
.action(async (key) => {
|
|
const config = getConfig();
|
|
const keys = Object.keys(config);
|
|
if (!key) {
|
|
key = await input({
|
|
message: `Enter your key:(keys: ${JSON.stringify(keys)})`,
|
|
});
|
|
}
|
|
|
|
if (config[key]) {
|
|
console.log(chalk.green(`get ${key}:`));
|
|
console.log(config[key]);
|
|
} else {
|
|
console.log(chalk.red(`not found ${key}`));
|
|
}
|
|
});
|
|
command.addCommand(getCommand);
|
|
|
|
const removeCommand = new Command('remove')
|
|
.argument('<key>')
|
|
.description('remove config')
|
|
.action(async (key) => {
|
|
const config = getConfig();
|
|
if (key) {
|
|
delete config[key];
|
|
writeConfig(config);
|
|
console.log(chalk.green(`remove ${key} success`));
|
|
}
|
|
});
|
|
command.addCommand(removeCommand);
|
|
|
|
const list = new Command('list').action(async () => {
|
|
const config = getConfig();
|
|
console.log(chalk.green('config', JSON.stringify(config, null, 2)));
|
|
});
|
|
command.addCommand(list);
|
|
|
|
app.addCommand(command);
|