1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const cross_spawn_1 = __importDefault(require("cross-spawn"));
|
7 | function spawnAsync(command, args, options = {}) {
|
8 | const fakeErr = new Error('fake error just to preserve stacktrace');
|
9 | const previousStack = fakeErr.stack && fakeErr.stack.split('\n').splice(1);
|
10 | const previousStackString = previousStack && [' ...', ...previousStack].join('\n');
|
11 | let child;
|
12 | let promise = new Promise((resolve, reject) => {
|
13 | let { ignoreStdio, ...nodeOptions } = options;
|
14 |
|
15 | child = (0, cross_spawn_1.default)(command, args, nodeOptions);
|
16 | let stdout = '';
|
17 | let stderr = '';
|
18 | if (!ignoreStdio) {
|
19 | if (child.stdout) {
|
20 | child.stdout.on('data', (data) => {
|
21 | stdout += data;
|
22 | });
|
23 | }
|
24 | if (child.stderr) {
|
25 | child.stderr.on('data', (data) => {
|
26 | stderr += data;
|
27 | });
|
28 | }
|
29 | }
|
30 | let completionListener = (code, signal) => {
|
31 | child.removeListener('error', errorListener);
|
32 | let result = {
|
33 | pid: child.pid,
|
34 | output: [stdout, stderr],
|
35 | stdout,
|
36 | stderr,
|
37 | status: code,
|
38 | signal,
|
39 | };
|
40 | if (code !== 0) {
|
41 | let error = signal
|
42 | ? new Error(`${command} exited with signal: ${signal}`)
|
43 | : new Error(`${command} exited with non-zero code: ${code}`);
|
44 | if (error.stack && previousStackString) {
|
45 | error.stack += `\n${previousStackString}`;
|
46 | }
|
47 | Object.assign(error, result);
|
48 | reject(error);
|
49 | }
|
50 | else {
|
51 | resolve(result);
|
52 | }
|
53 | };
|
54 | let errorListener = (error) => {
|
55 | if (ignoreStdio) {
|
56 | child.removeListener('exit', completionListener);
|
57 | }
|
58 | else {
|
59 | child.removeListener('close', completionListener);
|
60 | }
|
61 | Object.assign(error, {
|
62 | pid: child.pid,
|
63 | output: [stdout, stderr],
|
64 | stdout,
|
65 | stderr,
|
66 | status: null,
|
67 | signal: null,
|
68 | });
|
69 | reject(error);
|
70 | };
|
71 | if (ignoreStdio) {
|
72 | child.once('exit', completionListener);
|
73 | }
|
74 | else {
|
75 | child.once('close', completionListener);
|
76 | }
|
77 | child.once('error', errorListener);
|
78 | });
|
79 |
|
80 |
|
81 | promise.child = child;
|
82 | return promise;
|
83 | }
|
84 | exports.default = spawnAsync;
|
85 |
|
\ | No newline at end of file |