UNPKG

1.98 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.logExec = exports.execShell = exports.execWithArgs = exports.execCommand = exports.proxyCommand = void 0;
4const execa = require("execa");
5const colors_1 = require("../colors");
6async function proxyCommand(cmd, args = [], opt = {}) {
7 const processArgs = process.argv.slice(2);
8 await execWithArgs(cmd, [...args, ...processArgs], {
9 ...opt,
10 });
11}
12exports.proxyCommand = proxyCommand;
13async function execCommand(cmd, opt = {}) {
14 logExec(cmd, [], opt);
15 await execa
16 .command(cmd, {
17 stdio: 'inherit',
18 preferLocal: true,
19 ...opt,
20 })
21 .catch(err => handleError(err, cmd, opt));
22}
23exports.execCommand = execCommand;
24async function execWithArgs(cmd, args = [], opt = {}) {
25 logExec(cmd, args, opt);
26 await execa(cmd, args, {
27 stdio: 'inherit',
28 preferLocal: true,
29 ...opt,
30 }).catch(err => handleError(err, cmd, opt));
31}
32exports.execWithArgs = execWithArgs;
33async function execShell(cmd, opt = {}) {
34 await execCommand(cmd, {
35 shell: true,
36 ...opt,
37 });
38}
39exports.execShell = execShell;
40function handleError(err, cmd, opt = {}) {
41 if (opt.noProcessExit) {
42 throw err || new Error(`execCommand failed: ${cmd}`);
43 }
44 if (err) {
45 if (err.originalMessage) {
46 console.log(colors_1.dimGrey(err.originalMessage));
47 }
48 else if (err.shortMessage) {
49 console.log(colors_1.dimGrey(err.shortMessage));
50 }
51 else {
52 console.error(err);
53 }
54 if (err.exitCode) {
55 process.exit(err.exitCode);
56 }
57 }
58 process.exit(1);
59}
60function logExec(cmd, args = [], opt = {}) {
61 const cmdline = [
62 ...Object.entries(opt.env || {}).map(([k, v]) => [k, v].join('=')),
63 cmd,
64 ...args,
65 ].join(' ');
66 console.log(colors_1.grey(cmdline));
67}
68exports.logExec = logExec;