feat: add types and list change
This commit is contained in:
parent
0a9e5c1d4f
commit
99f6f58f52
1
.npmrc
1
.npmrc
@ -1 +1,2 @@
|
||||
@abearxiong:registry=https://npm.pkg.github.com
|
||||
@build:registry=https://npm.xiongxiao.me
|
15
package.json
15
package.json
@ -1,9 +1,10 @@
|
||||
{
|
||||
"name": "@abearxiong/code-flow",
|
||||
"name": "@build/code-flow",
|
||||
"version": "0.0.2",
|
||||
"description": "code的flow流程成图",
|
||||
"type": "module",
|
||||
"main": "index.js",
|
||||
"author": "abearxiong",
|
||||
"scripts": {
|
||||
"watch": "cross-env ENV=production webpack --mode=production --watch",
|
||||
"dev": "cross-env NODE_ENV=development nodemon --delay 2.5 -e js,cjs,mjs --exec node dist/app.cjs",
|
||||
@ -19,14 +20,14 @@
|
||||
"docker:push": "docker push docker.xiongxiao.me/code-flow:v0.0.2",
|
||||
"docker:run": "docker run -it --name code-flow -p 4000:4000 docker.xiongxiao.me/code-flow:v0.0.2",
|
||||
"docker:build:gitea": "docker build -t git.xiongxiao.me/abearxiong/code-flow:v0.0.2 .",
|
||||
"docker:push:gitea": "docker push git.xiongxiao.me/abearxiong/code-flow:v0.0.2"
|
||||
"docker:push:gitea": "docker push git.xiongxiao.me/abearxiong/code-flow:v0.0.2",
|
||||
"dts": "./node_modules/.bin/dts-bundle-generator -o types/index.d.ts src/type.ts"
|
||||
},
|
||||
"keywords": [],
|
||||
"types": "types/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
"types"
|
||||
],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@abearxiong/router": "0.0.1-alpha.27",
|
||||
@ -35,7 +36,7 @@
|
||||
"@babel/preset-env": "^7.25.4",
|
||||
"@babel/preset-typescript": "^7.24.7",
|
||||
"dayjs": "^1.11.13",
|
||||
"dts-bundle": "^0.7.3",
|
||||
"dts-bundle-generator": "^9.5.1",
|
||||
"json5": "^2.2.3",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"lodash-es": "^4.17.21",
|
||||
@ -43,11 +44,11 @@
|
||||
"pg": "^8.12.0",
|
||||
"sequelize": "^6.37.3",
|
||||
"socket.io": "^4.7.5",
|
||||
"strip-ansi": "^7.1.0"
|
||||
"strip-ansi": "^7.1.0",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/crypto-js": "^4.2.2",
|
||||
"@types/dts-bundle": "^0.0.35",
|
||||
"@types/jest": "^29.5.13",
|
||||
"@types/jsonwebtoken": "^9.0.6",
|
||||
"@types/lodash-es": "^4.17.12",
|
||||
|
21
pnpm-lock.yaml
generated
21
pnpm-lock.yaml
generated
@ -29,6 +29,9 @@ importers:
|
||||
dts-bundle:
|
||||
specifier: ^0.7.3
|
||||
version: 0.7.3
|
||||
dts-bundle-generator:
|
||||
specifier: ^9.5.1
|
||||
version: 9.5.1
|
||||
json5:
|
||||
specifier: ^2.2.3
|
||||
version: 2.2.3
|
||||
@ -53,6 +56,9 @@ importers:
|
||||
strip-ansi:
|
||||
specifier: ^7.1.0
|
||||
version: 7.1.0
|
||||
zod:
|
||||
specifier: ^3.23.8
|
||||
version: 3.23.8
|
||||
devDependencies:
|
||||
'@types/crypto-js':
|
||||
specifier: ^4.2.2
|
||||
@ -1437,6 +1443,11 @@ packages:
|
||||
dottie@2.0.6:
|
||||
resolution: {integrity: sha512-iGCHkfUc5kFekGiqhe8B/mdaurD+lakO9txNnTvKtA6PISrw86LgqHvRzWYPyoE2Ph5aMIrCw9/uko6XHTKCwA==}
|
||||
|
||||
dts-bundle-generator@9.5.1:
|
||||
resolution: {integrity: sha512-DxpJOb2FNnEyOzMkG11sxO2dmxPjthoVWxfKqWYJ/bI/rT1rvTMktF5EKjAYrRZu6Z6t3NhOUZ0sZ5ZXevOfbA==}
|
||||
engines: {node: '>=14.0.0'}
|
||||
hasBin: true
|
||||
|
||||
dts-bundle@0.7.3:
|
||||
resolution: {integrity: sha512-EEAEuPRk8QyKhoN90NHTh+spSQujkkvOnKWUfuzpmC/fgryiWopL1SegSktx0UsoPfNidIGVDN7/AXpBDBv0WQ==}
|
||||
engines: {node: '>= 0.10.0'}
|
||||
@ -2860,6 +2871,9 @@ packages:
|
||||
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
|
||||
engines: {node: '>=10'}
|
||||
|
||||
zod@3.23.8:
|
||||
resolution: {integrity: sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==}
|
||||
|
||||
snapshots:
|
||||
|
||||
'@abearxiong/router@0.0.1-alpha.27': {}
|
||||
@ -4524,6 +4538,11 @@ snapshots:
|
||||
|
||||
dottie@2.0.6: {}
|
||||
|
||||
dts-bundle-generator@9.5.1:
|
||||
dependencies:
|
||||
typescript: 5.6.2
|
||||
yargs: 17.7.2
|
||||
|
||||
dts-bundle@0.7.3:
|
||||
dependencies:
|
||||
'@types/detect-indent': 0.1.30
|
||||
@ -6058,3 +6077,5 @@ snapshots:
|
||||
yn@3.1.1: {}
|
||||
|
||||
yocto-queue@0.1.0: {}
|
||||
|
||||
zod@3.23.8: {}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { app } from '../../app.ts';
|
||||
import { ContainerModel, ContainerData } from './models/index.ts';
|
||||
import { ContainerModel, ContainerData, Container } from './models/index.ts';
|
||||
|
||||
const list = app.route({
|
||||
path: 'container',
|
||||
@ -15,27 +15,49 @@ list.addTo(app);
|
||||
|
||||
const add = app.route({
|
||||
path: 'container',
|
||||
key: 'add',
|
||||
key: 'update',
|
||||
});
|
||||
add.run = async (ctx) => {
|
||||
// const data = ctx.query;
|
||||
const data: ContainerData = {
|
||||
className: 'name',
|
||||
style: {
|
||||
color: 'red',
|
||||
},
|
||||
const data = ctx.query.data;
|
||||
|
||||
const _data: Container = {
|
||||
title: '',
|
||||
description: '',
|
||||
code: '',
|
||||
source: '',
|
||||
type: '',
|
||||
sourceType: '',
|
||||
data: {
|
||||
className: '',
|
||||
style: {},
|
||||
showChild: true,
|
||||
shadowRoot: false,
|
||||
},
|
||||
};
|
||||
const container = await ContainerModel.create({
|
||||
title: 'title',
|
||||
data: data as ContainerData,
|
||||
description: 'description',
|
||||
code: `console.log('hello world')`,
|
||||
source: 'source',
|
||||
type: 'typescript',
|
||||
const container = {
|
||||
..._data,
|
||||
...data,
|
||||
};
|
||||
let containerModel: any = null;
|
||||
if (container.id) {
|
||||
containerModel = await ContainerModel.findByPk(container.id);
|
||||
if (containerModel) {
|
||||
containerModel.update(container);
|
||||
containerModel.save();
|
||||
}
|
||||
} else {
|
||||
try{
|
||||
|
||||
containerModel = await ContainerModel.create({
|
||||
...container,
|
||||
});
|
||||
ctx.body = container;
|
||||
} catch (e) {
|
||||
console.log('error', e);
|
||||
}
|
||||
console.log('containerModel', container);
|
||||
}
|
||||
|
||||
ctx.body = containerModel;
|
||||
return ctx;
|
||||
};
|
||||
add.addTo(app);
|
||||
|
@ -7,6 +7,16 @@ export interface ContainerData {
|
||||
showChild?: boolean;
|
||||
shadowRoot?: boolean;
|
||||
}
|
||||
export type Container = {
|
||||
id?: string;
|
||||
title?: string;
|
||||
description?: string;
|
||||
type?: string;
|
||||
code?: string;
|
||||
source?: string;
|
||||
sourceType?: string;
|
||||
data?: ContainerData;
|
||||
};
|
||||
export class ContainerModel extends Model {
|
||||
declare id: string;
|
||||
declare title: string;
|
||||
@ -14,6 +24,7 @@ export class ContainerModel extends Model {
|
||||
declare type: string;
|
||||
declare code: string;
|
||||
declare source: string;
|
||||
declare sourceType: string;
|
||||
declare data: ContainerData;
|
||||
}
|
||||
ContainerModel.init(
|
||||
@ -37,13 +48,17 @@ ContainerModel.init(
|
||||
defaultValue: '',
|
||||
},
|
||||
code: {
|
||||
type: DataTypes.STRING,
|
||||
type: DataTypes.TEXT,
|
||||
defaultValue: '',
|
||||
},
|
||||
source: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
sourceType: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
data: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
|
1
src/routes/types.ts
Normal file
1
src/routes/types.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './container/type.ts'
|
@ -1,3 +1,5 @@
|
||||
import { CodeManager } from './admin/dashboard/load.ts';
|
||||
import { ContainerData } from './routes/types.ts';
|
||||
|
||||
export { CodeManager };
|
||||
export { ContainerData };
|
||||
|
249
types/index.d.ts
vendored
249
types/index.d.ts
vendored
@ -1,133 +1,4 @@
|
||||
// Generated by dts-bundle v0.7.3
|
||||
// Dependencies for this module:
|
||||
// ../@abearxiong/router
|
||||
// ../sequelize
|
||||
// ../@abearxiong/use-config
|
||||
|
||||
import { Route } from '@abearxiong/router';
|
||||
import { QueryRouter, Server } from '@abearxiong/router';
|
||||
import { DataTypes, Model } from 'sequelize';
|
||||
import { useConfig } from '@abearxiong/use-config';
|
||||
import { Sequelize } from 'sequelize';
|
||||
|
||||
|
||||
export { CodeManager };
|
||||
|
||||
|
||||
export enum CodeStatus {
|
||||
running = 'running',
|
||||
stop = 'stop',
|
||||
fail = 'fail',
|
||||
}
|
||||
|
||||
export type CodeManager = {
|
||||
fn?: any;
|
||||
status?: CodeStatus;
|
||||
errorMsg?: string;
|
||||
} & Partial<RouterCode>;
|
||||
|
||||
const codeDemoRun = `async function run(ctx) {
|
||||
ctx.body = 'test js';
|
||||
return ctx;
|
||||
}`;
|
||||
const templateFn = (codeStr: string) => {
|
||||
return `
|
||||
${codeStr}
|
||||
if(run) {
|
||||
return run(ctx);
|
||||
}
|
||||
if(main) {
|
||||
return main(ctx);
|
||||
}
|
||||
return 'no run or main function';
|
||||
`;
|
||||
};
|
||||
export const loadOne = async (item: RouterCodeModel) => {
|
||||
const { path, key, id, code, exec, project } = item.toJSON();
|
||||
const codeStr = exec || code;
|
||||
try {
|
||||
const fn: any = new Function('ctx', templateFn(codeStr));
|
||||
// run code
|
||||
const codeRunRoute = new Route(path, key, { id });
|
||||
codeRunRoute.run = fn;
|
||||
router.removeById(id); // TODO:
|
||||
router.add(codeRunRoute);
|
||||
return {
|
||||
...item.toJSON(),
|
||||
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, exec, project, active } = item.toJSON();
|
||||
if (!active) {
|
||||
return {
|
||||
...item.toJSON(),
|
||||
path,
|
||||
key,
|
||||
id,
|
||||
code,
|
||||
project,
|
||||
status: CodeStatus.stop,
|
||||
};
|
||||
}
|
||||
try {
|
||||
const codeStr = exec || code;
|
||||
const fn: any = new Function('ctx', templateFn(codeStr));
|
||||
// run code
|
||||
const codeRunRoute = new Route(path, key, { id });
|
||||
codeRunRoute.run = fn;
|
||||
router.add(codeRunRoute);
|
||||
return {
|
||||
...item.toJSON(),
|
||||
path,
|
||||
key,
|
||||
id,
|
||||
code,
|
||||
project,
|
||||
type: item.type,
|
||||
fn,
|
||||
status: CodeStatus.running,
|
||||
};
|
||||
} catch (e) {
|
||||
console.error('error id:', id, '\n', e);
|
||||
return {
|
||||
path,
|
||||
key,
|
||||
id,
|
||||
code,
|
||||
project,
|
||||
type: item.type,
|
||||
status: CodeStatus.fail,
|
||||
errorMsg: e.message.toString(),
|
||||
};
|
||||
}
|
||||
});
|
||||
return codeManager;
|
||||
};
|
||||
|
||||
export const router = new QueryRouter();
|
||||
export const server = new Server({
|
||||
path: '/api/router',
|
||||
});
|
||||
|
||||
// Generated by dts-bundle-generator v9.5.1
|
||||
|
||||
export type RouterCode = {
|
||||
id: string;
|
||||
@ -143,106 +14,28 @@ export type RouterCode = {
|
||||
data: any;
|
||||
validator: any;
|
||||
};
|
||||
|
||||
export enum RouterCodeType {
|
||||
route = 'route',
|
||||
middleware = 'middleware',
|
||||
declare enum RouterCodeType {
|
||||
route = "route",
|
||||
middleware = "middleware"
|
||||
}
|
||||
|
||||
export class RouterCodeModel extends Model {
|
||||
declare id: string;
|
||||
declare path: string;
|
||||
declare key: string;
|
||||
declare active: boolean;
|
||||
declare project: string;
|
||||
declare code: string;
|
||||
declare exec: string;
|
||||
declare type: RouterCodeType;
|
||||
declare middleware: string[];
|
||||
declare next: string; // 如果是中间件,不存在
|
||||
declare data: any; // 内容
|
||||
declare validator: any;
|
||||
declare enum CodeStatus {
|
||||
running = "running",
|
||||
stop = "stop",
|
||||
fail = "fail"
|
||||
}
|
||||
RouterCodeModel.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
comment: '用户id',
|
||||
},
|
||||
path: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
key: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
active: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false,
|
||||
},
|
||||
project: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: 'default',
|
||||
},
|
||||
code: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
exec: {
|
||||
type: DataTypes.STRING, // 对代码进行编译后的代码
|
||||
defaultValue: '',
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.ENUM(RouterCodeType.route, RouterCodeType.middleware),
|
||||
defaultValue: RouterCodeType.route,
|
||||
},
|
||||
middleware: {
|
||||
type: DataTypes.ARRAY(DataTypes.STRING),
|
||||
defaultValue: [],
|
||||
},
|
||||
next: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
data: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
},
|
||||
validator: {
|
||||
type: DataTypes.JSON,
|
||||
defaultValue: {},
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'cf_router_code',
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
type PostgresConfig = {
|
||||
postgres: {
|
||||
username: string;
|
||||
password: string;
|
||||
host: string;
|
||||
port: number;
|
||||
database: string;
|
||||
export type CodeManager = {
|
||||
fn?: any;
|
||||
status?: CodeStatus;
|
||||
errorMsg?: string;
|
||||
lock?: boolean;
|
||||
} & Partial<RouterCode>;
|
||||
export interface ContainerData {
|
||||
style?: {
|
||||
[key: string]: string;
|
||||
};
|
||||
};
|
||||
const config = useConfig<PostgresConfig>();
|
||||
|
||||
const postgresConfig = config.postgres;
|
||||
|
||||
if (!postgresConfig) {
|
||||
console.error('postgres config is required');
|
||||
process.exit(1);
|
||||
className?: string;
|
||||
showChild?: boolean;
|
||||
shadowRoot?: boolean;
|
||||
}
|
||||
export const sequelize = new Sequelize({
|
||||
dialect: 'postgres',
|
||||
...postgresConfig,
|
||||
// logging: false,
|
||||
});
|
||||
|
||||
export {};
|
||||
|
Loading…
x
Reference in New Issue
Block a user