UNPKG

1.92 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var child_process_1 = require("child_process");
4var d = require('debug')('electron-windows-installer:spawn');
5// Public: Maps a process's output into an {Observable}
6//
7// exe - The program to execute
8// params - Arguments passed to the process
9// opts - Options that will be passed to child_process.spawn
10//
11// Returns an {Observable} with a single value, that is the output of the
12// spawned process
13function spawn(exe, params, opts) {
14 return new Promise(function (resolve, reject) {
15 var proc = null;
16 d("Spawning " + exe + " " + params.join(' '));
17 if (!opts) {
18 proc = child_process_1.spawn(exe, params);
19 }
20 else {
21 proc = child_process_1.spawn(exe, params, opts);
22 }
23 // We need to wait until all three events have happened:
24 // * stdout's pipe is closed
25 // * stderr's pipe is closed
26 // * We've got an exit code
27 var rejected = false;
28 var refCount = 3;
29 var stdout = '';
30 var release = function () {
31 if (--refCount <= 0 && !rejected)
32 resolve(stdout);
33 };
34 var bufHandler = function (b) {
35 var chunk = b.toString();
36 stdout += chunk;
37 };
38 proc.stdout.on('data', bufHandler);
39 proc.stdout.once('close', release);
40 proc.stderr.on('data', bufHandler);
41 proc.stderr.once('close', release);
42 proc.on('error', function (e) { return reject(e); });
43 proc.on('close', function (code) {
44 if (code === 0) {
45 release();
46 }
47 else {
48 rejected = true;
49 reject(new Error("Failed with exit code: " + code + "\nOutput:\n" + stdout));
50 }
51 });
52 });
53}
54exports.default = spawn;
55//# sourceMappingURL=spawn-promise.js.map
\No newline at end of file