feat: add install params for install denpendency

This commit is contained in:
xion 2025-03-06 19:40:31 +08:00
parent 1ae123e63a
commit e7e3e2b140
2 changed files with 43 additions and 4 deletions

View File

@ -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') {

View File

@ -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 });
}
};