1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
19 | return new (P || (P = Promise))(function (resolve, reject) {
|
20 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
21 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
22 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
23 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
24 | });
|
25 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | const spawn = require("cross-spawn");
|
28 | const path = require("path");
|
29 | const sprintf_js_1 = require("sprintf-js");
|
30 | const strip_ansi = require("strip-ansi");
|
31 | const logger_1 = require("../internal/util/logger");
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | exports.SuccessIsReturn0ErrorFinder = code => code !== 0;
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function spawnAndWatch(spawnCommand, options, log, spOpts = {}) {
|
48 | return __awaiter(this, void 0, void 0, function* () {
|
49 | const childProcess = spawn(spawnCommand.command, spawnCommand.args || [], options);
|
50 | if (spOpts.logCommand === false) {
|
51 | logger_1.logger.debug(`${options.cwd || path.resolve(".")} > ${stringifySpawnCommand(spawnCommand)} (pid '${childProcess.pid}')`);
|
52 | }
|
53 | else {
|
54 | log.write(`---\n`);
|
55 | log.write(`${options.cwd || path.resolve(".")} > ${stringifySpawnCommand(spawnCommand)} (pid '${childProcess.pid}')\n`);
|
56 | log.write(`---\n`);
|
57 | }
|
58 | return watchSpawned(childProcess, log, spOpts);
|
59 | });
|
60 | }
|
61 | exports.spawnAndWatch = spawnAndWatch;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function watchSpawned(childProcess, log, opts = {}) {
|
71 | let timer;
|
72 | let running = true;
|
73 | if (opts.timeout) {
|
74 | timer = setTimeout(() => {
|
75 | if (running) {
|
76 | logger_1.logger.warn("Spawn timeout expired. Killing command with pid '%s'", childProcess.pid);
|
77 | childProcess.kill();
|
78 | }
|
79 | }, opts.timeout);
|
80 | }
|
81 | return new Promise((resolve, reject) => {
|
82 | const optsToUse = Object.assign({ errorFinder: exports.SuccessIsReturn0ErrorFinder, stripAnsi: false }, opts);
|
83 | if (!optsToUse.errorFinder) {
|
84 |
|
85 | optsToUse.errorFinder = exports.SuccessIsReturn0ErrorFinder;
|
86 | }
|
87 | function sendToLog(data) {
|
88 | const formatted = optsToUse.stripAnsi ? strip_ansi(data.toString()) : data.toString();
|
89 | return log.write(formatted);
|
90 | }
|
91 | childProcess.stdout.on("data", sendToLog);
|
92 | childProcess.stderr.on("data", sendToLog);
|
93 | childProcess.addListener("exit", (code, signal) => {
|
94 | running = false;
|
95 | logger_1.logger.info("Spawn exit with pid '%d': code '%d', signal '%d'", childProcess.pid, code, signal);
|
96 | clearTimeout(timer);
|
97 | resolve({
|
98 | error: optsToUse.errorFinder(code, signal, log),
|
99 | code,
|
100 | childProcess,
|
101 | });
|
102 | });
|
103 | childProcess.addListener("error", err => {
|
104 | running = false;
|
105 |
|
106 | logger_1.logger.warn("Spawn failure: %s", err);
|
107 | clearTimeout(timer);
|
108 | reject(err);
|
109 | });
|
110 | });
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | function stringifySpawnCommand(sc) {
|
118 | return sprintf_js_1.sprintf("%s %s", sc.command, !!sc.args ? sc.args.join(" ") : "");
|
119 | }
|
120 | exports.stringifySpawnCommand = stringifySpawnCommand;
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | function asSpawnCommand(sentence, options = {}) {
|
129 | const split = sentence.split(" ");
|
130 | return {
|
131 | command: split[0],
|
132 | args: split.slice(1),
|
133 | options,
|
134 | };
|
135 | }
|
136 | exports.asSpawnCommand = asSpawnCommand;
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | function poisonAndWait(childProcess) {
|
144 | childProcess.kill();
|
145 | return new Promise((resolve, reject) => childProcess.on("close", () => {
|
146 | resolve();
|
147 | }));
|
148 | }
|
149 | exports.poisonAndWait = poisonAndWait;
|
150 |
|
\ | No newline at end of file |