diff --git a/src/routes/micro-app/list.ts b/src/routes/micro-app/list.ts index 39fa5be..3d88792 100644 --- a/src/routes/micro-app/list.ts +++ b/src/routes/micro-app/list.ts @@ -2,6 +2,7 @@ import { app } from '@/app.ts'; import { MicroAppModel } from './models.ts'; import { appPathCheck, installApp } from './module/install-app.ts'; import { loadApp } from './module/load-app.ts'; +import { manager } from './manager-app.ts'; // 应用上传到 应用管理 的平台 app @@ -68,7 +69,9 @@ app if (check) { ctx.throw(400, 'App already exists, please remove it first'); } - await installApp({ path, key }); + const installAppData = await installApp({ path, key }); + await manager.add(installAppData.showAppInfo); + ctx.body = installAppData; }) .addTo(app); @@ -94,3 +97,21 @@ app } }) .addTo(app); + +// curl http://localhost:4002/api/router?path=micro-app&key=unload +app + .route({ + path: 'micro-app', + key: 'unload', + }) + .define(async (ctx) => { + // const { key } = ctx.query?.data; + const key = 'mark'; + const main = manager.getAppShowInfo(key); + if (!main) { + ctx.throw(400, 'Invalid app'); + } + manager.removeApp(key); + ctx.body = main; + }) + .addTo(app); diff --git a/src/routes/micro-app/module/install-app.ts b/src/routes/micro-app/module/install-app.ts index 9ae9364..7854795 100644 --- a/src/routes/micro-app/module/install-app.ts +++ b/src/routes/micro-app/module/install-app.ts @@ -57,8 +57,25 @@ export const installApp = async (opts: InstallAppOpts) => { if (!name || !version || !app) { throw new Error('Invalid package.json'); } + + const readmeFile = path.join(extractPath, 'README.md'); + let readmeDesc = ''; + if (fileIsExist(readmeFile)) { + readmeDesc = fs.readFileSync(readmeFile, 'utf-8'); + } + let showAppInfo = { + key, + status: 'inactive', + type: app?.type || 'system-app', + description: readmeDesc || '', + version, + // + entry: app?.entry || '', + path: extractPath, + origin: app, + }; app.key = key; fs.writeFileSync(pkgs, JSON.stringify(pkg, null, 2)); // fs.unlinkSync(filePath); - return { path: filePath, pkg }; + return { path: filePath, pkg, showAppInfo }; }; diff --git a/src/routes/micro-app/module/manager.ts b/src/routes/micro-app/module/manager.ts index d5b8ff4..4a90528 100644 --- a/src/routes/micro-app/module/manager.ts +++ b/src/routes/micro-app/module/manager.ts @@ -24,13 +24,15 @@ export enum AppType { export type AppInfo = { key: string; status?: 'inactive' | 'running' | 'stop' | 'error'; // 运行状态 + version?: string; // 版本 type?: AppType; // 默认类型 - entry?: string; // 入口文件 - path?: string; // 文件路径 + description?: string; // 描述 timestamp?: number; // 时间戳, 每次更新更新时间戳 process?: any; // 进程 - description?: string; // 描述 - version?: string; // 版本 + + origin?: Record; // 原始数据 + entry?: string; // 入口文件 + path?: string; // 文件路径 }; export const onAppShowInfo = (app: AppInfo) => { return { @@ -41,6 +43,15 @@ export const onAppShowInfo = (app: AppInfo) => { version: app.version, }; }; +export const createAppShowInfo = (app: any) => { + return { + key: app.key, + status: app.status, + type: app.type, + description: app.description, + version: app.version, + }; +}; type managerOptions = { mainApp: App; };