feat: add install params for install denpendency

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

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