From 1f380478ba1aabe1dde247675032db96fc78947f Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sat, 7 Dec 2024 00:47:43 +0800 Subject: [PATCH] add external export --- rollup.config.mjs | 51 +++++++++++++++++++++++++++++++++++---- src/modules/manager.ts | 23 +++++++++++++++++- src/route/dev/dev-list.ts | 2 ++ src/route/dev/model.ts | 3 +++ src/route/dev/operate.ts | 32 ++++++++++++++++++++++++ src/shared/external.ts | 14 +++++++++++ 6 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 src/shared/external.ts diff --git a/rollup.config.mjs b/rollup.config.mjs index 055372a..a4f4724 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -1,11 +1,8 @@ // @ts-check -import typescript from '@rollup/plugin-typescript'; import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; -import copy from 'rollup-plugin-copy'; import { dts } from 'rollup-plugin-dts'; import json from '@rollup/plugin-json'; -import * as glob from 'fast-glob'; import path from 'path'; import esbuild from 'rollup-plugin-esbuild'; import alias from '@rollup/plugin-alias'; @@ -19,7 +16,6 @@ const isDev = process.env.NODE_ENV === 'development'; */ const config = { input: './src/index.ts', - // input: './src/micro-client.ts', output: { dir: './dist', entryFileNames: 'app.mjs', @@ -114,5 +110,50 @@ const configs = [ '@msgpack/msgpack', ], }, + { + input: './src/scripts/init-data.ts', + output: { + file: './dist/init-data.d.ts', + }, + plugins: [dts()], + }, + { + input: './src/index.ts', + output: { + file: './dist/app.d.ts', + }, + plugins: [dts()], + }, ]; -export default [...configs, config]; +const external = [ + { + input: './src/shared/external.ts', + output: { + file: './dist/external.mjs', + }, + plugins: [ + alias({ + // only esbuild needs to be configured + entries: [ + { find: '@', replacement: path.resolve('src') }, // 配置 @ 为 src 目录 + ], + }), + resolve(), + commonjs(), + esbuild({ + target: 'node22', + minify: false, + tsconfig: 'tsconfig.json', + }), + ], + }, + { + input: './src/shared/external.ts', + output: { + file: './dist/external.d.ts', + }, + plugins: [dts()], + }, +]; + +export default [...configs, config, ...external]; diff --git a/src/modules/manager.ts b/src/modules/manager.ts index 013990f..af7e88b 100644 --- a/src/modules/manager.ts +++ b/src/modules/manager.ts @@ -1,4 +1,4 @@ -import { spawn } from 'child_process'; +import { spawn, ChildProcess } from 'child_process'; import { DevData, DevModel } from '../route/dev/model.ts'; import { fileIsExist } from '@kevisual/use-config'; import { transformToMJS } from './build/convert.ts'; @@ -156,4 +156,25 @@ export class DevManater { async getList() { return this.devList; } + async runScript(loadScript: string, dev: DevModel) { + const data = dev.data; + const { cwd } = data; + const childProcess = spawn(loadScript, [], { + stdio: 'inherit', + shell: true, + cwd: cwd, + detached: true, + env: { + ...process.env, + ...data.env, + }, + }); + return childProcess; + } } + +export const runScript = (dev: DevModel) => { + const data = dev.data; + const { type, code } = data; + // +}; diff --git a/src/route/dev/dev-list.ts b/src/route/dev/dev-list.ts index 69b9d73..f27423f 100644 --- a/src/route/dev/dev-list.ts +++ b/src/route/dev/dev-list.ts @@ -103,3 +103,5 @@ app ctx.body = 'ok'; }) .addTo(app); + + diff --git a/src/route/dev/model.ts b/src/route/dev/model.ts index 8495c5a..3639240 100644 --- a/src/route/dev/model.ts +++ b/src/route/dev/model.ts @@ -13,6 +13,9 @@ export type DevData = { codeStatus?: 'success' | 'error'; // 转换状态 status: 'active' | 'inactive'; // 状态, 是否生成codePath path?: string; // esbuild 打包路径,用于 node 启动, vite 配置 esbuild 配置,rollup配置路径等 + scripts?: { + [key: string]: string; + }; }; export type Dev = Partial>; diff --git a/src/route/dev/operate.ts b/src/route/dev/operate.ts index e5d0f20..e0a5c6a 100644 --- a/src/route/dev/operate.ts +++ b/src/route/dev/operate.ts @@ -113,3 +113,35 @@ app ctx.body = dev; }) .addTo(app); + +app + .route({ + path: 'dev-app', + key: 'runScript', + }) + .define(async (ctx) => { + const id = ctx.query.id; + const script = ctx.query.script; + if (!id) { + ctx.throw(400, 'id is required'); + return; + } + if (!script) { + ctx.throw(400, 'script is required'); + return; + } + const dev = await DevModel.findByPk(id); + if (!dev) { + ctx.throw(404, 'dev not found'); + return; + } + const scripts = dev.data.scripts || {}; + const loadScript = scripts[script]; + if (!loadScript) { + ctx.throw(404, 'script not found'); + return; + } + const result = await devManager.runScript(loadScript, dev); + ctx.body = result; + }) + .addTo(app); diff --git a/src/shared/external.ts b/src/shared/external.ts new file mode 100644 index 0000000..095dd5d --- /dev/null +++ b/src/shared/external.ts @@ -0,0 +1,14 @@ +export const nodeExternals = [ + /@kevisual\/router(\/.*)?/, //, // 路由 + /@kevisual\/use-config(\/.*)?/, // + + 'sequelize', // 数据库 orm + 'ioredis', // redis + 'socket.io', // socket.io + 'minio', // minio + + 'pg', // pg + 'pino', // pino + 'pino-pretty', // pino-pretty + '@msgpack/msgpack', // msgpack +];