update pkgs fix lightcode
This commit is contained in:
70
assistant/src/module/light-code/index.ts
Normal file
70
assistant/src/module/light-code/index.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { App, QueryRouterServer } from '@kevisual/router';
|
||||
import { AssistantConfig } from '../assistant/index.ts';
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
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';
|
||||
|
||||
const app = new Mini();
|
||||
|
||||
app.route({
|
||||
path: 'hello',
|
||||
describetion: 'LightCode 示例路由',
|
||||
}).define(async (ctx) => {
|
||||
ctx.body = 'Hello from LightCode!';
|
||||
}).addTo(app);
|
||||
|
||||
app.wait();
|
||||
`;
|
||||
type opts = {
|
||||
router: QueryRouterServer | App
|
||||
config: AssistantConfig
|
||||
}
|
||||
export const initLightCode = async (opts: opts) => {
|
||||
// 注册 lightcode 路由
|
||||
console.log('初始化 lightcode 路由');
|
||||
const config = opts.config;
|
||||
const appDir = config.configPath.appsDir;
|
||||
const lightcodeDir = path.join(appDir, 'light-code', 'code');
|
||||
if (!fs.existsSync(lightcodeDir)) {
|
||||
fs.mkdirSync(lightcodeDir, { recursive: true });
|
||||
}
|
||||
const codeFiles = glob.sync(['**/*.ts', '**/*.js'], {
|
||||
cwd: lightcodeDir,
|
||||
onlyFiles: true,
|
||||
});
|
||||
if (codeFiles.length === 0) {
|
||||
// 如果没有代码文件,创建一个示例文件
|
||||
const demoPath = path.join(lightcodeDir, `${codeDemoId}.ts`);
|
||||
fs.writeFileSync(demoPath, codeDemo, 'utf-8');
|
||||
}
|
||||
|
||||
for (const file of codeFiles) {
|
||||
const tsPath = path.join(lightcodeDir, file);
|
||||
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}`);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user