feat: add silky cli tools

This commit is contained in:
2025-04-27 22:16:09 +08:00
parent 75d181ef43
commit 6867de838e
42 changed files with 3867 additions and 251 deletions

View File

@@ -9,6 +9,9 @@ import { checkAppDir, installApp, uninstallApp } from '@/module/download/install
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');
});
@@ -55,7 +58,6 @@ const downloadAppCommand = new Command('download')
} else {
data.id = id;
}
const res = await queryApp(data);
let registry = 'https://kevisual.cn';
if (options?.registry) {
registry = new URL(options.registry).origin;
@@ -63,6 +65,8 @@ const downloadAppCommand = new Command('download')
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';
@@ -88,9 +92,36 @@ const downloadAppCommand = new Command('download')
const uninstallAppCommand = new Command('uninstall')
.alias('remove')
.description('卸载 app serve client的包')
.description('卸载 app serve client的包。 手动删除更简单。')
.option('-i, --id <id>', 'user/key')
.option('-t, --type <type>', 'app或者web 默认为web', 'web')
.option('-p, --path <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'));
@@ -102,7 +133,7 @@ const uninstallAppCommand = new Command('uninstall')
data.user = user;
data.key = key;
} else {
console.error(chalk.red('id is required'));
console.error(chalk.red('id is required user/key'));
return;
}
@@ -113,6 +144,7 @@ const uninstallAppCommand = new Command('uninstall')
},
{
appDir: '',
type: options.type,
},
);
if (result.code === 200) {

View File

@@ -105,11 +105,17 @@ export const installApp = async (app: Package, opts: InstallAppOpts = {}) => {
};
}
};
/**
* 检查是否为空,如果为空则删除
* @param appDir
*/
export const checkAppDir = (appDir: string) => {
const files = fs.readdirSync(appDir);
if (files.length === 0) {
fs.rmSync(appDir, { recursive: true });
}
try {
const files = fs.readdirSync(appDir);
if (files.length === 0) {
fs.rmSync(appDir, { recursive: true });
}
} catch (error) {}
};
export const checkFileExists = (path: string) => {
try {
@@ -121,9 +127,10 @@ export const checkFileExists = (path: string) => {
};
type UninstallAppOpts = {
appDir?: string;
type?: 'app' | 'web';
};
export const uninstallApp = async (app: Partial<Package>, opts: UninstallAppOpts = {}) => {
const { appDir = '' } = opts;
const { appDir = '', type = 'web' } = opts;
try {
const { user, key } = app;
const keyDir = path.join(appDir, user, key);
@@ -140,7 +147,7 @@ export const uninstallApp = async (app: Partial<Package>, opts: UninstallAppOpts
} catch (error) {
console.error(error);
}
checkAppDir(parentDir);
type === 'web' && checkAppDir(parentDir);
return {
code: 200,
message: 'Uninstall app success',

View File

@@ -45,3 +45,12 @@ export const queryLogin = new QueryLoginNode({
// console.log('onLoad');
},
});
/**
*
* @param url
* @returns
*/
export const getUrl = (url: string) => {
return new URL('/api/router', url).href;
};

View File

@@ -5,12 +5,15 @@ type QueryAppParams = {
user?: string;
key?: string;
};
export const queryApp = async (params: QueryAppParams) => {
return await query.post({
path: 'app',
key: 'getApp',
data: {
...params,
export const queryApp = async (params: QueryAppParams, opts?: any) => {
return await query.post(
{
path: 'app',
key: 'getApp',
data: {
...params,
},
},
});
opts,
);
};