update pkgs fix lightcode
This commit is contained in:
@@ -98,10 +98,12 @@ export type AssistantConfigData = {
|
||||
* Router代理, 会自动获取 {path: 'router', key: 'list'}的路由信息,然后注入到整个router应用当中.
|
||||
* 例子: { proxy: [ { type: 'router', api: 'https://localhost:50002/api/router' } ] }
|
||||
* base: 是否使用 /api/router的基础路径,默认false
|
||||
* lightcode: 是否启用lightcode路由,默认false
|
||||
*/
|
||||
router?: {
|
||||
proxy: ProxyInfo[];
|
||||
base?: boolean;
|
||||
lightcode?: boolean;
|
||||
}
|
||||
/**
|
||||
* API 代理配置, 比如,api开头的,v1开头的等等
|
||||
|
||||
@@ -10,6 +10,7 @@ import { logger } from '@/module/logger.ts';
|
||||
import { getEnvToken } from '@/module/http-token.ts';
|
||||
import { initApi } from '@kevisual/api/proxy'
|
||||
import { Query } from '@kevisual/query';
|
||||
import { initLightCode } from '@/module/light-code/index.ts';
|
||||
export class AssistantApp extends Manager {
|
||||
config: AssistantConfig;
|
||||
pagesPath: string;
|
||||
@@ -129,6 +130,7 @@ export class AssistantApp extends Manager {
|
||||
const config = this.config.getConfig();
|
||||
const routerProxy = config?.router?.proxy || [];
|
||||
const base = config.router?.base ?? false;
|
||||
const lightcode = config.router?.lightcode ?? true;
|
||||
if (base) {
|
||||
routerProxy.push({
|
||||
type: 'router',
|
||||
@@ -137,12 +139,27 @@ export class AssistantApp extends Manager {
|
||||
}
|
||||
})
|
||||
}
|
||||
if (lightcode) {
|
||||
routerProxy.push({
|
||||
type: 'lightcode',
|
||||
lightcode: {
|
||||
check: true,
|
||||
}
|
||||
})
|
||||
}
|
||||
if (routerProxy.length === 0) {
|
||||
return
|
||||
}
|
||||
for (const proxyInfo of routerProxy) {
|
||||
if (proxyInfo.type !== 'router') {
|
||||
console.warn('路由的type必须是"router"');
|
||||
if (proxyInfo.type !== 'router' && proxyInfo.type !== 'lightcode') {
|
||||
console.warn('路由的type必须是"router", 或者lightcode');
|
||||
continue;
|
||||
}
|
||||
if (proxyInfo.type === 'lightcode') {
|
||||
initLightCode({
|
||||
router: this.mainApp,
|
||||
config: this.config
|
||||
});
|
||||
continue;
|
||||
}
|
||||
const url = proxyInfo.router!.url;
|
||||
|
||||
@@ -14,7 +14,7 @@ export type ProxyInfo = {
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
type?: 'file' | 'dynamic' | 'minio' | 'http' | 's3' | 'router';
|
||||
type?: 'file' | 'dynamic' | 'minio' | 'http' | 's3' | 'router' | 'lightcode';
|
||||
/**
|
||||
* 目标的 pathname, 默认为请求的url.pathname, 设置了pathname,则会使用pathname作为请求的url.pathname
|
||||
* @default undefined
|
||||
@@ -45,6 +45,13 @@ export type ProxyInfo = {
|
||||
router?: {
|
||||
id?: string;
|
||||
url?: string;
|
||||
},
|
||||
lightcode?: {
|
||||
id?: string;
|
||||
/**
|
||||
* 是否检测远程服务更新
|
||||
*/
|
||||
check?: boolean;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,6 @@ type RunCode = {
|
||||
};
|
||||
error?: any
|
||||
timestamp?: string
|
||||
[key: string]: any
|
||||
}
|
||||
export const runCode = async (tsPath: string, params: RunCodeParams = {}, opts?: RunCodeOptions): Promise<RunCode> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { App } from '@kevisual/router';
|
||||
import { loadAppInfo, AppInfoConfig, saveAppInfo, getAppsPath } from './app-file.ts';
|
||||
import { fork } from 'node:child_process';
|
||||
import { merge } from 'lodash-es';
|
||||
import { merge } from 'es-toolkit';
|
||||
import { deleteFileAppInfo } from './app-file.ts';
|
||||
import { fileIsExist } from '@kevisual/use-config/env';
|
||||
import path from 'node:path';
|
||||
|
||||
Reference in New Issue
Block a user