更新依赖项版本,添加文件哈希功能,优化远程应用连接逻辑
This commit is contained in:
@@ -1,70 +1,169 @@
|
||||
import { App, QueryRouterServer } from '@kevisual/router';
|
||||
import { AssistantConfig } from '../assistant/index.ts';
|
||||
import { AssistantInit } from '../../services/init/index.ts';
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import fs, { write } from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import glob from 'fast-glob';
|
||||
import { runCode } from './run.ts';
|
||||
import { pick } from 'es-toolkit';
|
||||
const codeDemoId = '0e700dc8-90dd-41b7-91dd-336ea51de3d2'
|
||||
import { filter } from "@kevisual/js-filter";
|
||||
const codeDemo = `// 这是一个示例代码文件
|
||||
import {Mini} from '@kevisual/router';
|
||||
import { getHash, getStringHash } from '../file-hash.ts';
|
||||
|
||||
const app = new Mini();
|
||||
const codeDemo = `// 这是一个示例代码文件
|
||||
import {App} from '@kevisual/router';
|
||||
|
||||
const app = new App();
|
||||
|
||||
app.route({
|
||||
path: 'hello',
|
||||
describetion: 'LightCode 示例路由',
|
||||
description: 'LightCode 示例路由',
|
||||
metadata: {
|
||||
tags: ['light-code', 'example'],
|
||||
},
|
||||
}).define(async (ctx) => {
|
||||
console.log('tokenUser:', ctx.query?.tokenUser);
|
||||
ctx.body = 'Hello from LightCode!';
|
||||
}).addTo(app);
|
||||
|
||||
app.wait();
|
||||
`;
|
||||
const writeCodeDemo = async (appDir: string) => {
|
||||
const lightcodeDir = path.join(appDir, 'light-code', 'code');
|
||||
const demoPath = path.join(lightcodeDir, `${codeDemoId}.ts`);
|
||||
fs.writeFileSync(demoPath, codeDemo, 'utf-8');
|
||||
}
|
||||
// writeCodeDemo(path.join(os.homedir(), 'kevisual', 'assistant-app', 'apps'));
|
||||
|
||||
type opts = {
|
||||
router: QueryRouterServer | App
|
||||
config: AssistantConfig
|
||||
config: AssistantInit
|
||||
sync?: boolean
|
||||
}
|
||||
type LightCodeFile = {
|
||||
id?: string, code?: string, hash?: string, filepath: string
|
||||
}
|
||||
export const initLightCode = async (opts: opts) => {
|
||||
// 注册 lightcode 路由
|
||||
console.log('初始化 lightcode 路由');
|
||||
// 注册 light-code 路由
|
||||
console.log('初始化 light-code 路由');
|
||||
const config = opts.config;
|
||||
const app = opts.router;
|
||||
const token = config.getConfig()?.token || '';
|
||||
const query = config.query;
|
||||
const sync = opts.sync ?? true;
|
||||
if (!config || !app) {
|
||||
console.error('initLightCode 缺少必要参数, config 或 app');
|
||||
return;
|
||||
}
|
||||
const appDir = config.configPath.appsDir;
|
||||
const lightcodeDir = path.join(appDir, 'light-code', 'code');
|
||||
if (!fs.existsSync(lightcodeDir)) {
|
||||
fs.mkdirSync(lightcodeDir, { recursive: true });
|
||||
}
|
||||
let diffList: LightCodeFile[] = [];
|
||||
|
||||
const codeFiles = glob.sync(['**/*.ts', '**/*.js'], {
|
||||
cwd: lightcodeDir,
|
||||
onlyFiles: true,
|
||||
}).map(file => {
|
||||
return {
|
||||
filepath: path.join(lightcodeDir, file),
|
||||
// hash: getHash(path.join(lightcodeDir, file))
|
||||
}
|
||||
});
|
||||
if (codeFiles.length === 0) {
|
||||
// 如果没有代码文件,创建一个示例文件
|
||||
const demoPath = path.join(lightcodeDir, `${codeDemoId}.ts`);
|
||||
fs.writeFileSync(demoPath, codeDemo, 'utf-8');
|
||||
|
||||
if (sync) {
|
||||
const queryRes = await query.post({
|
||||
path: 'light-code',
|
||||
key: 'list',
|
||||
token,
|
||||
});
|
||||
if (queryRes.code === 200) {
|
||||
const lightQueryList = queryRes.data?.list || [];
|
||||
for (const item of lightQueryList) {
|
||||
const codeHash = getStringHash(item.code || '');
|
||||
diffList.push({ id: item.id!, code: item.code || '', hash: codeHash, filepath: path.join(lightcodeDir, `${item.id}.ts`) });
|
||||
}
|
||||
|
||||
|
||||
const codeFileSet = new Set(codeFiles.map(f => f.filepath));
|
||||
// 需要新增的文件 (在 diffList 中但不在 codeFiles 中)
|
||||
const toAdd = diffList.filter(d => !codeFileSet.has(d.filepath));
|
||||
// 需要删除的文件 (在 codeFiles 中但不在 diffList 中)
|
||||
const toDelete = codeFiles.filter(f => !diffList.some(d => d.filepath === f.filepath));
|
||||
// 需要更新的文件 (两边都有但 hash 不同)
|
||||
const toUpdate = diffList.filter(d => codeFileSet.has(d.filepath) && d.hash !== getHash(d.filepath));
|
||||
const unchanged = diffList.filter(d => codeFileSet.has(d.filepath) && d.hash === getHash(d.filepath));
|
||||
|
||||
// 执行新增
|
||||
for (const item of toAdd) {
|
||||
fs.writeFileSync(item.filepath, item.code, 'utf-8');
|
||||
// console.log(`新增 light-code 文件: ${item.filepath}`);
|
||||
}
|
||||
|
||||
// 执行删除
|
||||
for (const filepath of toDelete) {
|
||||
fs.unlinkSync(filepath.filepath);
|
||||
// console.log(`删除 light-code 文件: ${filepath.filepath}`);
|
||||
}
|
||||
|
||||
// 执行更新
|
||||
for (const item of toUpdate) {
|
||||
fs.writeFileSync(item.filepath, item.code, 'utf-8');
|
||||
// console.log(`更新 light-code 文件: ${item.filepath}`);
|
||||
}
|
||||
// 记录未更新的文件
|
||||
// const lightCodeList = [...toAdd, ...unchanged].map(d => ({
|
||||
// filepath: d.filepath,
|
||||
// hash: d.hash
|
||||
// }));
|
||||
} else {
|
||||
console.error('light-code 同步失败', queryRes.message);
|
||||
diffList = codeFiles;
|
||||
}
|
||||
} else {
|
||||
diffList = codeFiles;
|
||||
}
|
||||
|
||||
for (const file of codeFiles) {
|
||||
const tsPath = path.join(lightcodeDir, file);
|
||||
|
||||
for (const file of diffList) {
|
||||
const tsPath = file.filepath;
|
||||
const runRes = await runCode(tsPath, { path: 'router', key: 'list' }, { timeout: 10000 });
|
||||
if (runRes.success) {
|
||||
const res = runRes.data;
|
||||
if (res.code === 200) {
|
||||
const list = res.data?.list || [];
|
||||
for (const routerItem of list) {
|
||||
const pickValues = pick(routerItem, ['path', 'id', 'description', 'metadata']);
|
||||
if (routerItem.path?.includes('auth') || routerItem.path?.includes('router') || routerItem.path?.includes('call')) {
|
||||
continue;
|
||||
}
|
||||
console.log('lightcode 路由必须包含 path 和 id', pickValues);
|
||||
console.log(`注册 lightcode 路由: ${routerItem.path} ${routerItem.id} 来自文件: ${file}`);
|
||||
// console.log(`注册 light-code 路由: [${routerItem.path}] ${routerItem.id} 来自文件: ${file.filepath}`);
|
||||
app.route({
|
||||
id: routerItem.id,
|
||||
path: routerItem.id!,
|
||||
description: routerItem.description || '',
|
||||
metadata: routerItem.metadata || {},
|
||||
middleware: ['auth'],
|
||||
}).define(async (ctx) => {
|
||||
const tokenUser = ctx.state?.tokenUser || {};
|
||||
const query = { ...ctx.query, tokenUser }
|
||||
const runRes2 = await runCode(tsPath, query, { timeout: 30000 });
|
||||
if (runRes2.success) {
|
||||
const res2 = runRes2.data;
|
||||
if (res2.code === 200) {
|
||||
ctx.body = res2.data;
|
||||
} else {
|
||||
ctx.throw(res2.code, res2.message || 'Lightcode 路由执行失败');
|
||||
}
|
||||
} else {
|
||||
ctx.throw(runRes2.error || 'Lightcode 路由执行失败');
|
||||
}
|
||||
}).addTo(app);
|
||||
|
||||
const runRes2 = await runCode(tsPath, { path: routerItem.path, key: routerItem.key, id: routerItem.id }, { timeout: 10000 });
|
||||
console.log('lightcode 路由执行结果', runRes2);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.error('lightcode 路由执行失败', runRes.error);
|
||||
console.error('light-code 路由执行失败', runRes.error);
|
||||
}
|
||||
}
|
||||
console.log(`light-code 路由注册成功`, `注册${diffList.length}个路由`);
|
||||
}
|
||||
Reference in New Issue
Block a user