"feat: 升级本地应用管理依赖,新增应用删除功能及强制覆盖下载选项"
This commit is contained in:
@@ -34,6 +34,8 @@ const downloadAppCommand = new Command('download')
|
||||
.option('-o, --output <output>', '下载 app serve client的包, 输出路径, 默认是当前目录')
|
||||
.option('-t, --type <type>', '下载 app serve client的包, 类型, app,或者web, 默认为web')
|
||||
.option('-r, --registry <registry>', '下载 app serve client的包, 使用私有源')
|
||||
.option('-f, --force ', '强制覆盖')
|
||||
.option('-y, --yes ', '覆盖的时候不提示')
|
||||
.action(async (options) => {
|
||||
const id = options.id || '';
|
||||
const output = options.output || '';
|
||||
@@ -79,6 +81,8 @@ const downloadAppCommand = new Command('download')
|
||||
appDir: output,
|
||||
kevisualUrl: registry,
|
||||
appType: appType,
|
||||
force: options.force,
|
||||
yes: options.yes,
|
||||
});
|
||||
if (result.code === 200) {
|
||||
console.log(chalk.green('下载成功', res.data?.user, res.data?.key));
|
||||
|
||||
@@ -3,6 +3,10 @@ import fs from 'fs';
|
||||
import { storage, baseURL } from '../query.ts';
|
||||
import { chalk } from '../chalk.ts';
|
||||
import { Result } from '@kevisual/query';
|
||||
import { fileIsExist } from '@/uitls/file.ts';
|
||||
import { glob } from 'fast-glob';
|
||||
import inquirer from 'inquirer';
|
||||
|
||||
type DownloadTask = {
|
||||
downloadPath: string;
|
||||
downloadUrl: string;
|
||||
@@ -62,6 +66,34 @@ export const fetchLink = async (url: string, opts?: Options) => {
|
||||
content,
|
||||
};
|
||||
};
|
||||
const checkDelete = async (opts?: { force?: boolean; dir?: string; yes?: boolean }) => {
|
||||
const { force = false, dir = '', yes = false } = opts || {};
|
||||
if (force) {
|
||||
try {
|
||||
if (fileIsExist(dir)) {
|
||||
const files = await glob(`${dir}/**/*`, { onlyFiles: true });
|
||||
const answers = await inquirer.prompt([
|
||||
{
|
||||
type: 'confirm',
|
||||
name: 'confirm',
|
||||
message: `是否你需要删除 【${opts?.dir}】 目录下的文件. [${files.length}] 个?`,
|
||||
when: () => files.length > 0 && !yes, // 当 username 为空时,提示用户输入
|
||||
},
|
||||
]);
|
||||
if (answers?.confirm || yes) {
|
||||
fs.rmSync(dir, { recursive: true });
|
||||
console.log(chalk.green('删除成功', dir));
|
||||
} else {
|
||||
console.log(chalk.red('取消删除', dir));
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
}
|
||||
};
|
||||
type InstallAppOpts = {
|
||||
appDir?: string;
|
||||
kevisualUrl?: string;
|
||||
@@ -73,6 +105,11 @@ type InstallAppOpts = {
|
||||
* 是否是web, 下载到 web-config的下面
|
||||
*/
|
||||
appType?: 'app' | 'web';
|
||||
/**
|
||||
* 是否强制覆盖, 下载前删除已有的
|
||||
*/
|
||||
force?: boolean;
|
||||
yes?: boolean;
|
||||
};
|
||||
export const installApp = async (app: Package, opts: InstallAppOpts = {}) => {
|
||||
// const _app = demoData;
|
||||
@@ -84,6 +121,8 @@ export const installApp = async (app: Package, opts: InstallAppOpts = {}) => {
|
||||
let hasPackage = false;
|
||||
const user = _app.user;
|
||||
const key = _app.key;
|
||||
const downloadDirPath = appType === 'web' ? path.join(appDir, user, key) : path.join(appDir);
|
||||
await checkDelete({ force: opts?.force, yes: opts?.yes, dir: downloadDirPath });
|
||||
const packagePath = path.join(appDir, appType === 'app' ? 'package.json' : `${user}/${key}/package.json`);
|
||||
const downFiles = files
|
||||
.filter((file: any) => file?.path)
|
||||
|
||||
Reference in New Issue
Block a user