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

@@ -60,8 +60,9 @@ export const initConfig = (configRootPath: string) => {
};
export type ReturnInitConfigType = ReturnType<typeof initConfig>;
type AssistantConfigData = {
pageApi?: string; // https://kevisual.silkyai.cn
export type AssistantConfigData = {
pageApi?: string; // https://kevisual.cn
registry?: string; // https://kevisual.cn
proxy?: ProxyInfo[];
apiProxyList?: ProxyInfo[];
description?: string;
@@ -135,6 +136,10 @@ export class AssistantConfig {
}
return this.getConfig();
}
getRegistry() {
const config = this.getCacheAssistantConfig();
return config?.registry || config?.pageApi;
}
/**
* 设置 assistant-config.json 配置
* @param config
@@ -211,19 +216,23 @@ export class AssistantConfig {
* pem: 证书目录
* @param configDir
*/
static detectConfigDir(configDir?: string) {
static detectConfigDir(configDir?: string, deep = 3) {
const checkConfigDir = path.resolve(configDir || process.env.ASSISTANT_CONFIG_DIR || process.cwd());
const configPath = path.join(checkConfigDir, 'assistant-app');
if (checkFileExists(configPath)) {
return path.join(checkConfigDir);
}
const lastConfigPath = path.join(checkConfigDir, '..', 'assistant-app');
if (checkFileExists(lastConfigPath)) {
return path.join(checkConfigDir, '..');
if (deep >= 2) {
const lastConfigPath = path.join(checkConfigDir, '..', 'assistant-app');
if (checkFileExists(lastConfigPath)) {
return path.join(checkConfigDir, '..');
}
}
const lastConfigPath2 = path.join(checkConfigDir, '../..', 'assistant-app');
if (checkFileExists(lastConfigPath2)) {
return path.join(checkConfigDir, '../..');
if (deep >= 3) {
const lastConfigPath2 = path.join(checkConfigDir, '../..', 'assistant-app');
if (checkFileExists(lastConfigPath2)) {
return path.join(checkConfigDir, '../..');
}
}
// 如果没有找到助手配置文件目录,则返回当前目录, 执行默认创建助手配置文件目录
return checkConfigDir;

View File

@@ -1,4 +1,3 @@
export * from './install/index.ts';
export * from './config/index.ts';
export * from './file/index.ts';

View File

@@ -1,121 +0,0 @@
import path from 'node:path';
import fs from 'node:fs';
import { checkFileExists } from '../file/index.ts';
type DownloadTask = {
downloadPath: string;
downloadUrl: string;
user: string;
key: string;
version: string;
};
export type Package = {
id: string;
name?: string;
version?: string;
description?: string;
title?: string;
user?: string;
key?: string;
[key: string]: any;
};
type InstallAppOpts = {
appDir?: string;
kevisualUrl?: string;
/**
* 是否是客户端, 下载到 assistant-config的下面
*/
};
export const installApp = async (app: Package, opts: InstallAppOpts = {}) => {
// const _app = demoData;
const { appDir = '', kevisualUrl = 'https://kevisual.cn' } = opts;
const _app = app;
try {
let files = _app.data.files || [];
const version = _app.version;
const user = _app.user;
const key = _app.key;
const downFiles = files.map((file: any) => {
const noVersionPath = file.path.replace(`/${version}`, '');
return {
...file,
downloadPath: path.join(appDir, noVersionPath),
downloadUrl: `${kevisualUrl}/${noVersionPath}`,
};
});
const downloadTasks: DownloadTask[] = downFiles as any;
for (const file of downloadTasks) {
const downloadPath = file.downloadPath;
const downloadUrl = file.downloadUrl;
const dir = path.dirname(downloadPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const res = await fetch(downloadUrl);
const blob = await res.blob();
fs.writeFileSync(downloadPath, Buffer.from(await blob.arrayBuffer()));
}
let indexHtml = files.find((file: any) => file.name === 'index.html');
if (!indexHtml) {
files.push({
name: 'index.html',
path: `${user}/${key}/index.html`,
});
fs.writeFileSync(path.join(appDir, `${user}/${key}/index.html`), JSON.stringify(app, null, 2));
}
_app.data.files = files;
return {
code: 200,
data: _app,
message: 'Install app success',
};
} catch (error) {
console.error(error);
return {
code: 500,
message: 'Install app failed',
};
}
};
export const checkAppDir = (appDir: string) => {
const files = fs.readdirSync(appDir);
if (files.length === 0) {
fs.rmSync(appDir, { recursive: true });
}
};
type UninstallAppOpts = {
appDir?: string;
};
export const uninstallApp = async (app: Partial<Package>, opts: UninstallAppOpts = {}) => {
const { appDir = '' } = opts;
try {
const { user, key } = app;
const keyDir = path.join(appDir, user, key);
const parentDir = path.join(appDir, user);
if (!checkFileExists(appDir) || !checkFileExists(keyDir)) {
return {
code: 200,
message: 'uninstall app success',
};
}
try {
// 删除appDir和文件
fs.rmSync(keyDir, { recursive: true });
} catch (error) {
console.error(error);
}
checkAppDir(parentDir);
return {
code: 200,
message: 'Uninstall app success',
};
} catch (error) {
console.error(error);
return {
code: 500,
message: 'Uninstall app failed',
};
}
};

View File