import { program, Command } from '@/program.ts'; import { AssistantInit } from '@/services/init/index.ts'; import path from 'node:path'; import fs from 'node:fs'; import inquirer from 'inquirer'; import chalk from 'chalk'; type InitCommandOptions = { path?: string; }; const Init = new Command('init') .description('初始化一个助手客户端,生成配置文件。') .option('-p --path ', '助手路径,默认为执行命令的目录,如果助手路径不存在则创建。') .action((opts: InitCommandOptions) => { // 如果path参数存在,检测path是否是相对路径,如果是相对路径,则转换为绝对路径 if (opts.path && !opts.path.startsWith('/')) { opts.path = path.join(process.cwd(), opts.path); } else if (opts.path) { opts.path = path.resolve(opts.path); } const configDir = AssistantInit.detectConfigDir(opts.path); console.log('configDir', configDir); const assistantInit = new AssistantInit({ path: configDir, }); assistantInit.init(); }); program.addCommand(Init); const removeCommand = new Command('remove') .description('删除助手配置文件') // TODO .option('-p --path ', '助手路径,默认为执行命令的目录,如果助手路径不存在则创建。') .action((opts) => { if (opts.path && !opts.path.startsWith('/')) { opts.path = path.join(process.cwd(), opts.path); } else if (opts.path) { opts.path = path.resolve(opts.path); } const configDir = AssistantInit.detectConfigDir(opts.path); const assistantDir = path.join(configDir, 'assistant-app'); if (fs.existsSync(assistantDir)) { inquirer .prompt([ { type: 'confirm', name: 'confirm', message: `确定要删除助手配置文件吗?\n助手配置文件路径:${assistantDir}`, default: false, }, ]) .then((answers) => { if (answers.confirm) { fs.rmSync(assistantDir, { recursive: true, force: true }); console.log(chalk.green('助手配置文件已删除')); } else { console.log(chalk.blue('助手配置文件未删除')); } }); } else { console.log(chalk.blue('助手配置文件不存在')); } }); program.addCommand(removeCommand);