feat: add types and list change

This commit is contained in:
xion 2024-09-18 05:19:20 +08:00
parent 0a9e5c1d4f
commit 99f6f58f52
8 changed files with 127 additions and 271 deletions

1
.npmrc
View File

@ -1 +1,2 @@
@abearxiong:registry=https://npm.pkg.github.com @abearxiong:registry=https://npm.pkg.github.com
@build:registry=https://npm.xiongxiao.me

View File

@ -1,9 +1,10 @@
{ {
"name": "@abearxiong/code-flow", "name": "@build/code-flow",
"version": "0.0.2", "version": "0.0.2",
"description": "code的flow流程成图", "description": "code的flow流程成图",
"type": "module", "type": "module",
"main": "index.js", "main": "index.js",
"author": "abearxiong",
"scripts": { "scripts": {
"watch": "cross-env ENV=production webpack --mode=production --watch", "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", "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: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: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: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": [], "keywords": [],
"types": "types/index.d.ts", "types": "types/index.d.ts",
"files": [ "files": [
"dist" "types"
], ],
"author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@abearxiong/router": "0.0.1-alpha.27", "@abearxiong/router": "0.0.1-alpha.27",
@ -35,7 +36,7 @@
"@babel/preset-env": "^7.25.4", "@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7", "@babel/preset-typescript": "^7.24.7",
"dayjs": "^1.11.13", "dayjs": "^1.11.13",
"dts-bundle": "^0.7.3", "dts-bundle-generator": "^9.5.1",
"json5": "^2.2.3", "json5": "^2.2.3",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
@ -43,11 +44,11 @@
"pg": "^8.12.0", "pg": "^8.12.0",
"sequelize": "^6.37.3", "sequelize": "^6.37.3",
"socket.io": "^4.7.5", "socket.io": "^4.7.5",
"strip-ansi": "^7.1.0" "strip-ansi": "^7.1.0",
"zod": "^3.23.8"
}, },
"devDependencies": { "devDependencies": {
"@types/crypto-js": "^4.2.2", "@types/crypto-js": "^4.2.2",
"@types/dts-bundle": "^0.0.35",
"@types/jest": "^29.5.13", "@types/jest": "^29.5.13",
"@types/jsonwebtoken": "^9.0.6", "@types/jsonwebtoken": "^9.0.6",
"@types/lodash-es": "^4.17.12", "@types/lodash-es": "^4.17.12",

21
pnpm-lock.yaml generated
View File

@ -29,6 +29,9 @@ importers:
dts-bundle: dts-bundle:
specifier: ^0.7.3 specifier: ^0.7.3
version: 0.7.3 version: 0.7.3
dts-bundle-generator:
specifier: ^9.5.1
version: 9.5.1
json5: json5:
specifier: ^2.2.3 specifier: ^2.2.3
version: 2.2.3 version: 2.2.3
@ -53,6 +56,9 @@ importers:
strip-ansi: strip-ansi:
specifier: ^7.1.0 specifier: ^7.1.0
version: 7.1.0 version: 7.1.0
zod:
specifier: ^3.23.8
version: 3.23.8
devDependencies: devDependencies:
'@types/crypto-js': '@types/crypto-js':
specifier: ^4.2.2 specifier: ^4.2.2
@ -1437,6 +1443,11 @@ packages:
dottie@2.0.6: dottie@2.0.6:
resolution: {integrity: sha512-iGCHkfUc5kFekGiqhe8B/mdaurD+lakO9txNnTvKtA6PISrw86LgqHvRzWYPyoE2Ph5aMIrCw9/uko6XHTKCwA==} 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: dts-bundle@0.7.3:
resolution: {integrity: sha512-EEAEuPRk8QyKhoN90NHTh+spSQujkkvOnKWUfuzpmC/fgryiWopL1SegSktx0UsoPfNidIGVDN7/AXpBDBv0WQ==} resolution: {integrity: sha512-EEAEuPRk8QyKhoN90NHTh+spSQujkkvOnKWUfuzpmC/fgryiWopL1SegSktx0UsoPfNidIGVDN7/AXpBDBv0WQ==}
engines: {node: '>= 0.10.0'} engines: {node: '>= 0.10.0'}
@ -2860,6 +2871,9 @@ packages:
resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}
engines: {node: '>=10'} engines: {node: '>=10'}
zod@3.23.8:
resolution: {integrity: sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==}
snapshots: snapshots:
'@abearxiong/router@0.0.1-alpha.27': {} '@abearxiong/router@0.0.1-alpha.27': {}
@ -4524,6 +4538,11 @@ snapshots:
dottie@2.0.6: {} dottie@2.0.6: {}
dts-bundle-generator@9.5.1:
dependencies:
typescript: 5.6.2
yargs: 17.7.2
dts-bundle@0.7.3: dts-bundle@0.7.3:
dependencies: dependencies:
'@types/detect-indent': 0.1.30 '@types/detect-indent': 0.1.30
@ -6058,3 +6077,5 @@ snapshots:
yn@3.1.1: {} yn@3.1.1: {}
yocto-queue@0.1.0: {} yocto-queue@0.1.0: {}
zod@3.23.8: {}

View File

@ -1,5 +1,5 @@
import { app } from '../../app.ts'; import { app } from '../../app.ts';
import { ContainerModel, ContainerData } from './models/index.ts'; import { ContainerModel, ContainerData, Container } from './models/index.ts';
const list = app.route({ const list = app.route({
path: 'container', path: 'container',
@ -15,27 +15,49 @@ list.addTo(app);
const add = app.route({ const add = app.route({
path: 'container', path: 'container',
key: 'add', key: 'update',
}); });
add.run = async (ctx) => { add.run = async (ctx) => {
// const data = ctx.query; const data = ctx.query.data;
const data: ContainerData = {
className: 'name', const _data: Container = {
style: { title: '',
color: 'red', description: '',
code: '',
source: '',
type: '',
sourceType: '',
data: {
className: '',
style: {},
showChild: true,
shadowRoot: false,
}, },
showChild: true,
shadowRoot: false,
}; };
const container = await ContainerModel.create({ const container = {
title: 'title', ..._data,
data: data as ContainerData, ...data,
description: 'description', };
code: `console.log('hello world')`, let containerModel: any = null;
source: 'source', if (container.id) {
type: 'typescript', containerModel = await ContainerModel.findByPk(container.id);
}); if (containerModel) {
ctx.body = container; containerModel.update(container);
containerModel.save();
}
} else {
try{
containerModel = await ContainerModel.create({
...container,
});
} catch (e) {
console.log('error', e);
}
console.log('containerModel', container);
}
ctx.body = containerModel;
return ctx; return ctx;
}; };
add.addTo(app); add.addTo(app);

View File

@ -7,6 +7,16 @@ export interface ContainerData {
showChild?: boolean; showChild?: boolean;
shadowRoot?: 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 { export class ContainerModel extends Model {
declare id: string; declare id: string;
declare title: string; declare title: string;
@ -14,6 +24,7 @@ export class ContainerModel extends Model {
declare type: string; declare type: string;
declare code: string; declare code: string;
declare source: string; declare source: string;
declare sourceType: string;
declare data: ContainerData; declare data: ContainerData;
} }
ContainerModel.init( ContainerModel.init(
@ -37,13 +48,17 @@ ContainerModel.init(
defaultValue: '', defaultValue: '',
}, },
code: { code: {
type: DataTypes.STRING, type: DataTypes.TEXT,
defaultValue: '', defaultValue: '',
}, },
source: { source: {
type: DataTypes.STRING, type: DataTypes.STRING,
defaultValue: '', defaultValue: '',
}, },
sourceType: {
type: DataTypes.STRING,
defaultValue: '',
},
data: { data: {
type: DataTypes.JSON, type: DataTypes.JSON,
defaultValue: {}, defaultValue: {},

1
src/routes/types.ts Normal file
View File

@ -0,0 +1 @@
export * from './container/type.ts'

View File

@ -1,3 +1,5 @@
import { CodeManager } from './admin/dashboard/load.ts'; import { CodeManager } from './admin/dashboard/load.ts';
import { ContainerData } from './routes/types.ts';
export { CodeManager }; export { CodeManager };
export { ContainerData };

279
types/index.d.ts vendored
View File

@ -1,248 +1,41 @@
// Generated by dts-bundle v0.7.3 // Generated by dts-bundle-generator v9.5.1
// 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',
});
export type RouterCode = { export type RouterCode = {
id: string; id: string;
path: string; path: string;
key: string; key: string;
active: boolean; active: boolean;
project: string; project: string;
code: string; code: string;
exec: string; exec: string;
type: RouterCodeType; type: RouterCodeType;
middleware: string[]; middleware: string[];
next: string; next: string;
data: any; data: any;
validator: any; validator: any;
}; };
declare enum RouterCodeType {
export enum RouterCodeType { route = "route",
route = 'route', middleware = "middleware"
middleware = 'middleware', }
declare enum CodeStatus {
running = "running",
stop = "stop",
fail = "fail"
}
export type CodeManager = {
fn?: any;
status?: CodeStatus;
errorMsg?: string;
lock?: boolean;
} & Partial<RouterCode>;
export interface ContainerData {
style?: {
[key: string]: string;
};
className?: string;
showChild?: boolean;
shadowRoot?: boolean;
} }
export class RouterCodeModel extends Model { export {};
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;
}
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;
};
};
const config = useConfig<PostgresConfig>();
const postgresConfig = config.postgres;
if (!postgresConfig) {
console.error('postgres config is required');
process.exit(1);
}
export const sequelize = new Sequelize({
dialect: 'postgres',
...postgresConfig,
// logging: false,
});