feat: 更新 @kevisual/router 版本至 0.0.89;添加 download 命令模块以支持文件下载
This commit is contained in:
@@ -49,7 +49,7 @@
|
||||
"@kevisual/local-app-manager": "^0.1.32",
|
||||
"@kevisual/logger": "^0.0.4",
|
||||
"@kevisual/query": "0.0.53",
|
||||
"@kevisual/router": "^0.0.88",
|
||||
"@kevisual/router": "^0.0.89",
|
||||
"@kevisual/types": "^0.0.12",
|
||||
"@kevisual/use-config": "^1.0.30",
|
||||
"@opencode-ai/plugin": "^1.2.21",
|
||||
|
||||
@@ -5,5 +5,6 @@ import './routes/index.ts';
|
||||
import './routes-simple/index.ts';
|
||||
|
||||
export const AgentPlugin: Plugin = createRouterAgentPluginFn({
|
||||
router: app,
|
||||
})
|
||||
router: app as any,
|
||||
})
|
||||
export { app }
|
||||
3
assistant/src/router.ts
Normal file
3
assistant/src/router.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { parse } from '@kevisual/router/commander';
|
||||
import { app, program } from './server.ts';
|
||||
parse({ app: app as any, program: program as any })
|
||||
@@ -164,3 +164,5 @@ export const runParser = async (argv: string[]) => {
|
||||
console.error('执行错误:', error.message);
|
||||
}
|
||||
};
|
||||
|
||||
export { app, program };
|
||||
|
||||
@@ -45,6 +45,7 @@
|
||||
"@kevisual/app": "^0.0.2",
|
||||
"@kevisual/auth": "^2.0.3",
|
||||
"@kevisual/context": "^0.0.8",
|
||||
"@kevisual/router": "^0.0.89",
|
||||
"@kevisual/use-config": "^1.0.30",
|
||||
"@opencode-ai/sdk": "^1.2.21",
|
||||
"@types/busboy": "^1.5.4",
|
||||
|
||||
14
pnpm-lock.yaml
generated
14
pnpm-lock.yaml
generated
@@ -20,6 +20,9 @@ importers:
|
||||
'@kevisual/context':
|
||||
specifier: ^0.0.8
|
||||
version: 0.0.8
|
||||
'@kevisual/router':
|
||||
specifier: ^0.0.89
|
||||
version: 0.0.89
|
||||
'@kevisual/use-config':
|
||||
specifier: ^1.0.30
|
||||
version: 1.0.30(dotenv@17.3.1)
|
||||
@@ -188,8 +191,8 @@ importers:
|
||||
specifier: 0.0.53
|
||||
version: 0.0.53
|
||||
'@kevisual/router':
|
||||
specifier: ^0.0.88
|
||||
version: 0.0.88
|
||||
specifier: ^0.0.89
|
||||
version: 0.0.89
|
||||
'@kevisual/types':
|
||||
specifier: ^0.0.12
|
||||
version: 0.0.12
|
||||
@@ -1365,6 +1368,9 @@ packages:
|
||||
'@kevisual/router@0.0.88':
|
||||
resolution: {integrity: sha512-T8kEbxyTGxZpbxAKDplDjZMIY+HCnXOeEdjwQ11AQetrLuqLFDZS5PnaWdVAHnONUDLhYoftkNj7bGWLtyQDlg==}
|
||||
|
||||
'@kevisual/router@0.0.89':
|
||||
resolution: {integrity: sha512-Sa8Fk3CeZzeZ78CUP9EDZnylkaKp9ibC1AzrF6Mivh5ifHJfyS5TZBw4NpNE11qoJN84MN/irX+poFU662V08A==}
|
||||
|
||||
'@kevisual/types@0.0.12':
|
||||
resolution: {integrity: sha512-zJXH2dosir3jVrQ6QG4i0+iLQeT9gJ3H+cKXs8ReWboxBSYzUZO78XssVeVrFPsJ33iaAqo4q3DWbSS1dWGn7Q==}
|
||||
|
||||
@@ -6759,6 +6765,10 @@ snapshots:
|
||||
dependencies:
|
||||
es-toolkit: 1.45.1
|
||||
|
||||
'@kevisual/router@0.0.89':
|
||||
dependencies:
|
||||
es-toolkit: 1.45.1
|
||||
|
||||
'@kevisual/types@0.0.12': {}
|
||||
|
||||
'@kevisual/use-config@1.0.30(dotenv@17.2.3)':
|
||||
|
||||
123
src/command/download.ts
Normal file
123
src/command/download.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* download 命令模块
|
||||
* 用于从 Kevisual 平台下载项目文件
|
||||
*/
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { queryLogin } from '@/module/query.ts';
|
||||
import { program, Command } from '@/program.ts';
|
||||
import { fetchLink } from '@/module/download/install.ts';
|
||||
import { chalk } from '@/module/chalk.ts';
|
||||
|
||||
/**
|
||||
* 文件项类型定义
|
||||
* 代表从 Kevisual 平台获取的文件元信息
|
||||
*/
|
||||
export type FileItem = {
|
||||
/** 文件名称,包含完整路径 */
|
||||
name: string;
|
||||
/** 文件大小(字节) */
|
||||
size: number;
|
||||
/** 最后修改时间 */
|
||||
lastModified: string;
|
||||
/** 文件 ETag 值,用于缓存验证 */
|
||||
etag: string;
|
||||
/** 相对路径 */
|
||||
path: string;
|
||||
/** 完整路径名 */
|
||||
pathname: string;
|
||||
/** 文件下载 URL */
|
||||
url: string;
|
||||
};
|
||||
|
||||
const downloadCmd = new Command('download')
|
||||
.description('下载项目')
|
||||
.option('-l, --link <link>', '下载链接')
|
||||
.option('-d, --directory <directory>', '下载目录', process.cwd())
|
||||
.action(async (opts) => {
|
||||
let link = opts.link || '';
|
||||
if (!link) {
|
||||
console.log('请提供下载链接');
|
||||
return;
|
||||
}
|
||||
let url = new URL(link);
|
||||
if (!url.pathname.endsWith('/')) {
|
||||
url.pathname += '/';
|
||||
}
|
||||
url.searchParams.set('recursive', 'true');
|
||||
const directory = opts.directory || process.cwd();
|
||||
const token = await queryLogin.getToken();
|
||||
|
||||
const res = await queryLogin.query.fetchText({
|
||||
url: url.toString(),
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
});
|
||||
|
||||
if (res.code === 200 && res.data) {
|
||||
const files = res.data as FileItem[];
|
||||
console.log(`获取到 ${files.length} 个文件`);
|
||||
await downloadFiles(files, { directory });
|
||||
} else {
|
||||
console.log(chalk.red('获取文件列表失败:'), res.message || '未知错误');
|
||||
}
|
||||
});
|
||||
|
||||
program.addCommand(downloadCmd);
|
||||
|
||||
/**
|
||||
* 下载文件列表
|
||||
* @param files 文件列表
|
||||
* @param opts 下载选项
|
||||
* @param opts.directory 下载目录,默认为当前目录
|
||||
*/
|
||||
export const downloadFiles = async (files: FileItem[], opts?: { directory?: string }) => {
|
||||
const directory = opts?.directory || process.cwd();
|
||||
let successCount = 0;
|
||||
let failCount = 0;
|
||||
|
||||
for (const file of files) {
|
||||
try {
|
||||
const downloadPath = path.join(directory, file.path);
|
||||
const dir = path.dirname(downloadPath);
|
||||
|
||||
// 创建目录
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
// 下载文件
|
||||
console.log(`下载中: ${file.name}`);
|
||||
const { blob, type } = await fetchLink(file.url);
|
||||
|
||||
// 检查是否为错误响应
|
||||
if (type.includes('text/html')) {
|
||||
const text = await blob.text();
|
||||
if (text === 'fetchRes is error') {
|
||||
console.log(chalk.red('下载失败:'), file.name);
|
||||
failCount++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
fs.writeFileSync(downloadPath, Buffer.from(await blob.arrayBuffer()));
|
||||
successCount++;
|
||||
console.log(chalk.green('下载成功:'), file.name);
|
||||
} catch (error) {
|
||||
failCount++;
|
||||
console.log(chalk.red('下载失败:'), file.name, error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(chalk.blue('下载完成'));
|
||||
console.log(chalk.green(`成功: ${successCount}`));
|
||||
console.log(chalk.red(`失败: ${failCount}`));
|
||||
|
||||
return {
|
||||
successCount,
|
||||
failCount,
|
||||
};
|
||||
};
|
||||
@@ -23,6 +23,7 @@ import './command/docker.ts';
|
||||
import './command/jwks.ts';
|
||||
|
||||
import './command/cnb/index.ts';
|
||||
import './command/download.ts';
|
||||
|
||||
// program.parse(process.argv);
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
}
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"src/**/*.ts"
|
||||
],
|
||||
"exclude": [],
|
||||
}
|
||||
Reference in New Issue
Block a user