@lcap/builder
Version:
lcap builder utils
59 lines (58 loc) • 2.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exec = exports.execSync = void 0;
const child_process_1 = require("child_process");
const logger_1 = __importDefault(require("./logger"));
/**
* 使用 spawnSync 的 shell inherit 模式,直接对接主进程的 stdio
* @param args 命令参数,每一项可以为字符串或是字符串数组
* @example
* execSync('rm', '-rf', 'node_modules')
* execSync('git clone', 'xxx')
*/
function execSync(...args) {
const command = args.join(' ');
logger_1.default.info('execute command: ', command);
const result = (0, child_process_1.spawnSync)(command, { shell: true, stdio: 'inherit' });
if (result.status !== null && result.status > 0) {
// 异常退出
throw new Error(`execute command error: ${command}`);
}
return result;
}
exports.execSync = execSync;
/**
* 使用 spawn 的 shell inherit 模式,直接对接主进程的 stdio
* @param args 命令参数,每一项可以为字符串或是字符串数组
* await exec('rm', '-rf', 'node_modules')
* await exec('git clone', 'xxx')
*/
function exec(...args) {
const command = args.join(' ');
let ended = false;
return new Promise(((resolve, reject) => {
const result = (0, child_process_1.spawn)(command, { shell: true, stdio: 'inherit' });
result.on('error', (e) => {
if (!ended) {
ended = true;
reject(e);
}
});
result.on('exit', (code) => {
if (!ended) {
ended = true;
(code === 0 ? resolve(true) : reject());
}
});
result.on('close', (code) => {
if (!ended) {
ended = true;
(code === 0 ? resolve(true) : reject());
}
});
}));
}
exports.exec = exec;