add 添加初始化依赖安装
This commit is contained in:
36
assistant/src/module/npm-install.ts
Normal file
36
assistant/src/module/npm-install.ts
Normal 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 });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user