feat: add code-flow base load

This commit is contained in:
2024-06-25 23:56:12 +08:00
parent 5118891c41
commit 776c1287b8
10 changed files with 261 additions and 132 deletions

View File

@@ -2,9 +2,10 @@ import { router } from '../../modules/router.ts';
import { Route } from '@abearxiong/router';
import { RouterCodeModel, RouterCode } from '../../models/code.ts';
enum CodeStatus {
success = 0,
fail = 1,
export enum CodeStatus {
running = 'running',
stop = 'stop',
fail = 'fail',
}
export type CodeManager = {
@@ -16,11 +17,54 @@ const codeDemoRun = `async function run(ctx) {
ctx.body = 'test js';
return ctx;
}`;
export const loadOne = async (item: RouterCodeModel) => {
const { path, key, id, code, project } = item.toJSON();
try {
const fn: any = new Function(
'ctx',
`
${code}
return run(ctx);
`,
);
// run code
const codeRunRoute = new Route(path, key, { id });
codeRunRoute.run = fn;
router.add(codeRunRoute);
return {
path,
key,
id,
project,
fn,
status: CodeStatus.running,
};
} catch (e) {
console.error('error id:', id, '\n', e);
return {
path,
key,
id,
project,
status: CodeStatus.fail,
errorMsg: e.message.toString(),
};
}
};
export const load = async function () {
const codes = await RouterCodeModel.findAll();
const codeManager: CodeManager[] = codes.map((item) => {
const { path, key, id, code, project } = item.toJSON();
console.log('item', item, 'code', item.code);
const { path, key, id, code, project, active } = item.toJSON();
if (!active) {
return {
path,
key,
id,
code,
project,
status: CodeStatus.stop,
};
}
try {
const fn: any = new Function(
'ctx',
@@ -37,9 +81,11 @@ export const load = async function () {
path,
key,
id,
code,
project,
type: item.type,
fn,
status: CodeStatus.success,
status: CodeStatus.running,
};
} catch (e) {
console.error('error id:', id, '\n', e);
@@ -47,7 +93,9 @@ export const load = async function () {
path,
key,
id,
code,
project,
type: item.type,
status: CodeStatus.fail,
errorMsg: e.message.toString(),
};

View File

@@ -1,5 +1,5 @@
import { EventEmitter, once } from 'stream';
import { load, CodeManager } from './load.ts';
import { load, CodeManager, CodeStatus } from './load.ts';
export enum LoadStatus {
LOADING = 'loading',
@@ -11,6 +11,30 @@ export const manager = {
list: [] as CodeManager[],
};
// 更新
export const updateNewCode = (code: CodeManager) => {
const index = manager.list.findIndex((item) => item.id === code.id);
if (index === -1) {
manager.list.push(code);
} else {
manager.list[index] = code;
}
};
// 删除
export const removeCode = (id: string) => {
const index = manager.list.findIndex((item) => item.id === id);
if (index !== -1) {
manager.list.splice(index, 1);
}
};
export const stopCode = (id: string) => {
const index = manager.list.findIndex((item) => item.id === id);
if (index !== -1) {
manager.list[index].status = CodeStatus.stop;
}
};
// 事件
export const events = new EventEmitter();
once(events, 'loaded')
@@ -26,7 +50,7 @@ once(events, 'loaded')
const init = async function () {
const r = await load();
manager.list = r;
events.emit('loaded');
};