add 添加初始化依赖安装

This commit is contained in:
2025-11-25 00:44:05 +08:00
parent 283fa5c603
commit fdb3a1f4d1
3 changed files with 45 additions and 4 deletions

View File

@@ -0,0 +1,36 @@
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 isPnpm = checkPnpm();
const params = ['i'];
if (isProduction && isPnpm) {
params.push('--production');
} else {
params.push('--omit=dev');
}
console.log('installDeps', appPath, params);
const syncSpawn = opts.sync ? spawnSync : spawn;
if (isPnpm) {
syncSpawn('pnpm', params, { cwd: appPath, stdio: 'inherit', env: process.env });
} else {
syncSpawn('npm', params, { cwd: appPath, stdio: 'inherit', env: process.env });
}
};