1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.promisifyChildProcess = promisifyChildProcess;
|
9 | exports.spawn = spawn;
|
10 | exports.fork = fork;
|
11 | exports.execFile = exports.exec = void 0;
|
12 |
|
13 | var _child_process = _interopRequireDefault(require("child_process"));
|
14 |
|
15 | function joinChunks(chunks, encoding) {
|
16 | if (chunks[0] instanceof Buffer) {
|
17 | var buffer = Buffer.concat(chunks);
|
18 | if (encoding) return buffer.toString(encoding);
|
19 | return buffer;
|
20 | }
|
21 |
|
22 | return chunks.join('');
|
23 | }
|
24 |
|
25 | function promisifyChildProcess(child) {
|
26 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
27 |
|
28 | var _promise = new Promise(function (resolve, reject) {
|
29 | var encoding = options.encoding,
|
30 | killSignal = options.killSignal;
|
31 | var captureStdio = encoding != null || options.maxBuffer != null;
|
32 | var maxBuffer = options.maxBuffer || 200 * 1024;
|
33 | var error;
|
34 | var bufferSize = 0;
|
35 | var stdoutChunks = [];
|
36 | var stderrChunks = [];
|
37 |
|
38 | var capture = function capture(chunks) {
|
39 | return function (data) {
|
40 | var remaining = maxBuffer - bufferSize;
|
41 |
|
42 | if (data.length > remaining) {
|
43 | error = new Error("maxBuffer size exceeded");
|
44 |
|
45 | child.kill(killSignal ? killSignal : 'SIGTERM');
|
46 | data = data.slice(0, remaining);
|
47 | }
|
48 |
|
49 | bufferSize += data.length;
|
50 | chunks.push(data);
|
51 | };
|
52 | };
|
53 |
|
54 | if (captureStdio) {
|
55 | if (child.stdout) child.stdout.on('data', capture(stdoutChunks));
|
56 | if (child.stderr) child.stderr.on('data', capture(stderrChunks));
|
57 | }
|
58 |
|
59 | child.on('error', reject);
|
60 |
|
61 | function done(code, signal) {
|
62 | if (!error) {
|
63 | if (code != null && code !== 0) {
|
64 | error = new Error("Process exited with code ".concat(code));
|
65 | } else if (signal != null) {
|
66 | error = new Error("Process was killed with ".concat(signal));
|
67 | }
|
68 | }
|
69 |
|
70 | function defineOutputs(obj) {
|
71 | if (captureStdio) {
|
72 | obj.stdout = joinChunks(stdoutChunks, encoding);
|
73 | obj.stderr = joinChunks(stderrChunks, encoding);
|
74 | } else {
|
75 |
|
76 | Object.defineProperties(obj, {
|
77 | stdout: {
|
78 | configurable: true,
|
79 | enumerable: true,
|
80 | get: function get() {
|
81 | console.error(new Error("To get stdout from a spawned or forked process, set the `encoding` or `maxBuffer` option").stack.replace(/^Error/, 'Warning'));
|
82 | return null;
|
83 | }
|
84 | },
|
85 | stderr: {
|
86 | configurable: true,
|
87 | enumerable: true,
|
88 | get: function get() {
|
89 | console.error(new Error("To get stderr from a spawned or forked process, set the `encoding` or `maxBuffer` option").stack.replace(/^Error/, 'Warning'));
|
90 | return null;
|
91 | }
|
92 | }
|
93 | });
|
94 |
|
95 | }
|
96 | }
|
97 |
|
98 | var output = {};
|
99 | defineOutputs(output);
|
100 | var finalError = error;
|
101 |
|
102 | if (finalError) {
|
103 | finalError.code = code;
|
104 | finalError.signal = signal;
|
105 | defineOutputs(finalError);
|
106 | reject(finalError);
|
107 | } else {
|
108 | resolve(output);
|
109 | }
|
110 | }
|
111 |
|
112 | child.on('close', done);
|
113 | child.on('exit', done);
|
114 | });
|
115 |
|
116 | return Object.create(child, {
|
117 | then: {
|
118 | value: _promise.then.bind(_promise)
|
119 | },
|
120 | catch: {
|
121 | value: _promise.catch.bind(_promise)
|
122 | }
|
123 | });
|
124 | }
|
125 |
|
126 | function spawn(command, args, options) {
|
127 | return promisifyChildProcess(_child_process.default.spawn(command, args, options), Array.isArray(args) ? options : args);
|
128 | }
|
129 |
|
130 | function fork(module, args, options) {
|
131 | return promisifyChildProcess(_child_process.default.fork(module, args, options), Array.isArray(args) ? options : args);
|
132 | }
|
133 |
|
134 | function promisifyExecMethod(method) {
|
135 | return function () {
|
136 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
137 | args[_key] = arguments[_key];
|
138 | }
|
139 |
|
140 | var child;
|
141 |
|
142 | var _promise = new Promise(function (resolve, reject) {
|
143 | child = method.apply(void 0, args.concat([function (err, stdout, stderr) {
|
144 | if (err) {
|
145 | err.stdout = stdout;
|
146 | err.stderr = stderr;
|
147 | reject(err);
|
148 | } else {
|
149 | resolve({
|
150 | stdout: stdout,
|
151 | stderr: stderr
|
152 | });
|
153 | }
|
154 | }]));
|
155 | });
|
156 |
|
157 | if (!child) {
|
158 | throw new Error('unexpected error: child has not been initialized');
|
159 | }
|
160 |
|
161 | return Object.create(child, {
|
162 | then: {
|
163 | value: _promise.then.bind(_promise)
|
164 | },
|
165 | catch: {
|
166 | value: _promise.catch.bind(_promise)
|
167 | }
|
168 | });
|
169 | };
|
170 | }
|
171 |
|
172 | var exec = promisifyExecMethod(_child_process.default.exec);
|
173 | exports.exec = exec;
|
174 | var execFile = promisifyExecMethod(_child_process.default.execFile);
|
175 | exports.execFile = execFile; |
\ | No newline at end of file |