39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
|
|
const childProcess = require('child_process');
|
|||
|
|
|
|||
|
|
function getExecutable(command) {
|
|||
|
|
return process.platform === 'win32' ? `${command}.cmd` : command;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function commandExists(command) {
|
|||
|
|
const lookupCommand = process.platform === 'win32' ? 'where.exe' : 'which';
|
|||
|
|
const result = childProcess.spawnSync(lookupCommand, [getExecutable(command)], {
|
|||
|
|
stdio: 'ignore',
|
|||
|
|
});
|
|||
|
|
return result.status === 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function ensureYarnAvailable(isCommandAvailable = commandExists) {
|
|||
|
|
if (!isCommandAvailable('yarn')) {
|
|||
|
|
throw new Error('未检测到 Yarn,请先安装 Yarn');
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function runYarn(args, cwd) {
|
|||
|
|
ensureYarnAvailable();
|
|||
|
|
const executable = getExecutable('yarn');
|
|||
|
|
console.log(`执行: yarn ${args.join(' ')}`);
|
|||
|
|
if (process.platform === 'win32') {
|
|||
|
|
childProcess.execFileSync(process.env.ComSpec || 'cmd.exe', [
|
|||
|
|
'/d',
|
|||
|
|
'/s',
|
|||
|
|
'/c',
|
|||
|
|
executable,
|
|||
|
|
...args,
|
|||
|
|
], { cwd, stdio: 'inherit' });
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
childProcess.execFileSync(executable, args, { cwd, stdio: 'inherit' });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
module.exports = { commandExists, ensureYarnAvailable, runYarn };
|