From e7e3e2b140b2c4e56228d6e54129b332a26ee7b9 Mon Sep 17 00:00:00 2001 From: xion Date: Thu, 6 Mar 2025 19:40:31 +0800 Subject: [PATCH] feat: add install params for install denpendency --- src/routes/micro-app/list.ts | 4 +- src/routes/micro-app/module/install-app.ts | 43 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/routes/micro-app/list.ts b/src/routes/micro-app/list.ts index 6cf7b06..53a1a15 100644 --- a/src/routes/micro-app/list.ts +++ b/src/routes/micro-app/list.ts @@ -88,7 +88,7 @@ app description: 'Deploy micro app in server', }) .define(async (ctx) => { - const { id, key, force } = ctx.query?.data; + const { id, key, force, install } = ctx.query?.data; // const id = '10f03411-85fc-4d37-a4d3-e32b15566a6c'; // const key = 'envision-cli'; // const id = '7c54a6de-9171-4093-926d-67a035042c6c'; @@ -115,7 +115,7 @@ app await manager.removeApp(key); } } - const installAppData = await installApp({ path, key }); + const installAppData = await installApp({ path, key, needInstallDeps: !!install }); await manager.add(installAppData.showAppInfo); ctx.body = installAppData; if (appType === 'system-app') { diff --git a/src/routes/micro-app/module/install-app.ts b/src/routes/micro-app/module/install-app.ts index 9945b1d..03375af 100644 --- a/src/routes/micro-app/module/install-app.ts +++ b/src/routes/micro-app/module/install-app.ts @@ -10,6 +10,7 @@ import { installAppFromKey } from './manager.ts'; export type InstallAppOpts = { path?: string; key?: string; + needInstallDeps?: boolean; }; /** * 检测路径是否存在 @@ -25,7 +26,7 @@ export const appPathCheck = async (opts: InstallAppOpts) => { return false; }; export const installApp = async (opts: InstallAppOpts) => { - const { key } = opts; + const { key, needInstallDeps } = opts; const fileStream = await minioClient.getObject(bucketName, opts.path); const pathName = opts.path.split('/').pop(); const directory = path.join(appsPath, key); @@ -38,7 +39,7 @@ export const installApp = async (opts: InstallAppOpts) => { fileStream.pipe(writeStream); await new Promise((resolve, reject) => { - writeStream.on('finish', resolve); + writeStream.on('finish', () => resolve(true)); writeStream.on('error', reject); }); // 解压 tgz文件 @@ -47,5 +48,43 @@ export const installApp = async (opts: InstallAppOpts) => { file: filePath, cwd: extractPath, }); + if (needInstallDeps) { + try { + installDeps({ appPath: directory, isProduction: true, sync: true }); + } catch (e) { + console.log('installDeps error, [need manual install deps]', e); + } + } return installAppFromKey(key); }; +import { spawn, spawnSync } from 'child_process'; + +export const checkPnpm = () => { + try { + spawnSync('pnpm', ['--version']); + return true; + } catch (e) { + return false; + } +}; + +type InstallDepsOptions = { + appPath: string; + isProduction?: boolean; + sync?: boolean; +}; +export const installDeps = async (opts: InstallDepsOptions) => { + const { appPath } = opts; + const isProduction = opts.isProduction ?? true; + const params = ['i']; + if (isProduction) { + params.push('--production'); + } + console.log('installDeps', appPath, params); + const syncSpawn = opts.sync ? spawnSync : spawn; + if (checkPnpm()) { + syncSpawn('pnpm', params, { cwd: appPath, stdio: 'inherit', env: process.env }); + } else { + syncSpawn('npm', params, { cwd: appPath, stdio: 'inherit', env: process.env }); + } +};