1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const child_process = require("child_process");
|
4 | const util_1 = require("util");
|
5 | const errors_1 = require("../errors");
|
6 | exports.execFilep = util_1.promisify(child_process.execFile);
|
7 | function runcmd({ outlog, errlog }, cmd, args, opts) {
|
8 | if (!errlog) {
|
9 | errlog = outlog;
|
10 | }
|
11 | const cmdoutlog = outlog.child({ cmd: `${cmd}` });
|
12 | const cmderrlog = errlog.child({ cmd: `${cmd}` });
|
13 | const p = child_process.spawn(cmd, args, opts);
|
14 | let stdoutChunk = '';
|
15 | let stderrChunk = '';
|
16 | p.stdout.on('data', chunk => {
|
17 | stdoutChunk += chunk.toString();
|
18 | if (stdoutChunk.endsWith('\n')) {
|
19 | cmdoutlog.info(stdoutChunk.replace(/\n$/g, ''));
|
20 | stdoutChunk = '';
|
21 | }
|
22 | });
|
23 | p.stderr.on('data', chunk => {
|
24 | stderrChunk += chunk.toString();
|
25 | if (stderrChunk.endsWith('\n')) {
|
26 | cmderrlog.info(stderrChunk.replace(/\n$/g, ''));
|
27 | stderrChunk = '';
|
28 | }
|
29 | });
|
30 | return new Promise((resolve, reject) => {
|
31 | p.on('close', code => {
|
32 | if (code > 0) {
|
33 | reject(new errors_1.ShellException('Subcommand returned a non-zero exit code.', 'server', code));
|
34 | }
|
35 | else {
|
36 | resolve(code);
|
37 | }
|
38 | });
|
39 | p.on('error', err => {
|
40 | reject(err);
|
41 | });
|
42 | });
|
43 | }
|
44 | exports.runcmd = runcmd;
|
45 | function prettyArgv(argv) {
|
46 | return argv.length > 0 ? argv.map(a => a.includes(' ') ? `"${a}"` : a).join(' ') : '';
|
47 | }
|
48 | exports.prettyArgv = prettyArgv;
|