/** * 下载 app serve client的包的命令 */ import { chalk } from '@/module/chalk.ts'; import { program, Command } from '../../../program.ts'; import { queryApp } from '../../../query/app-manager/query-app.ts'; import { checkAppDir, installApp, uninstallApp } from '@/module/download/install.ts'; import { fileIsExist } from '@/uitls/file.ts'; import fs from 'fs'; import { getConfig } from '@/module/get-config.ts'; import path from 'path'; import inquirer from 'inquirer'; import { baseURL, getUrl } from '@/module/query.ts'; export const appCommand = new Command('app').description('app 命令').action(() => { console.log('app'); }); program.addCommand(appCommand); /** * 下载 app serve client的包 * 如果output不存在,则创建, 路径为相对路径, 如果存在,则不创建 * * -t 类型, app,或者web, 默认为web * -r 使用私有源 * -o 输出路径 * -i 下载 app serve client的包, id 或者user/key * */ const downloadAppCommand = new Command('download') .description('下载 app serve client的包. \napp download -i root/code-center') .option('-i, --id ', '下载 app serve client的包, id 或者user/key') .option('-o, --output ', '下载 app serve client的包, 输出路径, 默认是当前目录') .option('-t, --type ', '下载 app serve client的包, 类型, app,或者web, 默认为web') .option('-r, --registry ', '下载 app serve client的包, 使用私有源') .action(async (options) => { const id = options.id || ''; const output = options.output || ''; if (!id) { console.error(chalk.red('id is required')); return; } if (output) { const checkOutput = fileIsExist(output); if (!checkOutput) { // console.error(chalk.red('output is error, 请输入正确的路径')); // return; fs.mkdirSync(output, { recursive: true }); } } const [user, key] = id.split('/'); const data: any = {}; if (user && key) { data.user = user; data.key = key; } else { data.id = id; } let registry = 'https://kevisual.cn'; if (options?.registry) { registry = new URL(options.registry).origin; } else { const config = getConfig(); registry = new URL(config.baseURL).origin; } const res = await queryApp(data, { url: getUrl(registry) }); console.log('registry', registry, data); if (res.code === 200) { const app = res.data; let appType: 'app' | 'web' = 'web'; if (options.type === 'app') { appType = 'app'; } else if (options.type === 'web') { appType = 'web'; } const result = await installApp(app, { appDir: output, kevisualUrl: registry, appType: appType, }); if (result.code === 200) { console.log(chalk.green('下载成功', res.data?.user, res.data?.key)); } else { console.error(chalk.red(result.message || '下载失败')); } } else { console.error(chalk.red(res.message || '下载失败')); } }); const uninstallAppCommand = new Command('uninstall') .alias('remove') .description('卸载 app serve client的包。 手动删除更简单。') .option('-i, --id ', 'user/key') .option('-t, --type ', 'app,或者web, 默认为web', 'web') .option('-p, --path ', '删除的路径, 如果存在,则优先执行,不会去判断 id 和 type 。') .action(async (options) => { if (options.path) { const _path = path.resolve(options.path); try { const checkPath = fileIsExist(_path); if (!checkPath) { console.error(chalk.red('path is error, 请输入正确的路径')); } else { const answer = await inquirer.prompt([ { type: 'confirm', name: 'confirm', message: `确定要删除 ${_path} 吗?`, default: false, }, ]); if (answer.confirm) { fs.rmSync(_path, { recursive: true }); console.log(chalk.green('删除成功', _path)); } } } catch (e) { console.error(chalk.red('删除失败', e)); } return; } const id = options.id || ''; if (!id) { console.error(chalk.red('id is required')); return; } const [user, key] = id.split('/'); const data: any = {}; if (user && key) { data.user = user; data.key = key; } else { console.error(chalk.red('id is required user/key')); return; } const result = await uninstallApp( { user, key, }, { appDir: '', type: options.type, }, ); if (result.code === 200) { console.log(chalk.green('卸载成功', user, key)); } else { console.error(chalk.red(result.message || '卸载失败')); } }); appCommand.addCommand(downloadAppCommand); appCommand.addCommand(uninstallAppCommand);