| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ChildProcess, IOType, Serializable, SpawnOptions} from 'node:child_process'; |
| b69ab31 | | | 9 | import type {Stream} from 'node:stream'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import getStream from 'get-stream'; |
| b69ab31 | | | 12 | import {spawn} from 'node:child_process'; |
| b69ab31 | | | 13 | import {Readable} from 'node:stream'; |
| b69ab31 | | | 14 | import os from 'os'; |
| b69ab31 | | | 15 | import {truncate} from './utils'; |
| b69ab31 | | | 16 | |
| b69ab31 | | | 17 | const LF = '\n'; |
| b69ab31 | | | 18 | const LF_BINARY = LF.codePointAt(0); |
| b69ab31 | | | 19 | const CR = '\r'; |
| b69ab31 | | | 20 | const CR_BINARY = CR.codePointAt(0); |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | function maybeStripFinalNewline<T extends string | Uint8Array>(input: T, strip: boolean): T { |
| b69ab31 | | | 23 | if (!strip) { |
| b69ab31 | | | 24 | return input; |
| b69ab31 | | | 25 | } |
| b69ab31 | | | 26 | const isString = typeof input === 'string'; |
| b69ab31 | | | 27 | const LF = isString ? '\n' : '\n'.codePointAt(0); |
| b69ab31 | | | 28 | const CR = isString ? '\r' : '\r'.codePointAt(0); |
| b69ab31 | | | 29 | if (typeof input === 'string') { |
| b69ab31 | | | 30 | const stripped = input.at(-1) === LF ? input.slice(0, input.at(-2) === CR ? -2 : -1) : input; |
| b69ab31 | | | 31 | return stripped as T; |
| b69ab31 | | | 32 | } |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | const stripped = |
| b69ab31 | | | 35 | input.at(-1) === LF_BINARY ? input.subarray(0, input.at(-2) === CR_BINARY ? -2 : -1) : input; |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | return stripped as T; |
| b69ab31 | | | 38 | } |
| b69ab31 | | | 39 | |
| b69ab31 | | | 40 | export interface EjecaOptions { |
| b69ab31 | | | 41 | /** |
| b69ab31 | | | 42 | * Current working directory of the child process. |
| b69ab31 | | | 43 | * @default process.cwd() |
| b69ab31 | | | 44 | */ |
| b69ab31 | | | 45 | readonly cwd?: string; |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | /** |
| b69ab31 | | | 48 | * Environment key-value pairs. Extends automatically if `process.extendEnv` is set to true. |
| b69ab31 | | | 49 | * @default process.env |
| b69ab31 | | | 50 | */ |
| b69ab31 | | | 51 | readonly env?: NodeJS.ProcessEnv; |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | /** |
| b69ab31 | | | 54 | * Set to `false` if you don't want to extend the environment variables when providing the `env` property. |
| b69ab31 | | | 55 | * @default true |
| b69ab31 | | | 56 | */ |
| b69ab31 | | | 57 | readonly extendEnv?: boolean; |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | /** |
| b69ab31 | | | 60 | * Feeds its contents as the standard input of the binary. |
| b69ab31 | | | 61 | */ |
| b69ab31 | | | 62 | readonly input?: string | Buffer | ReadableStream; |
| b69ab31 | | | 63 | |
| b69ab31 | | | 64 | /** |
| b69ab31 | | | 65 | * Setting this to `false` resolves the promise with the error instead of rejecting it. |
| b69ab31 | | | 66 | * @default true |
| b69ab31 | | | 67 | */ |
| b69ab31 | | | 68 | readonly reject?: boolean; |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | /** |
| b69ab31 | | | 71 | * Same options as [`stdio`](https://nodejs.org/docs/latest-v18.x/api/child_process.html#optionsstdio). |
| b69ab31 | | | 72 | * @default 'pipe' |
| b69ab31 | | | 73 | */ |
| b69ab31 | | | 74 | readonly stdin?: IOType | Stream | number | null | undefined; |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | /** |
| b69ab31 | | | 77 | * Same options as [`stdio`](https://nodejs.org/docs/latest-v18.x/api/child_process.html#optionsstdio). |
| b69ab31 | | | 78 | * @default 'pipe' |
| b69ab31 | | | 79 | */ |
| b69ab31 | | | 80 | readonly stdout?: IOType | Stream | number | null | undefined; |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | /** |
| b69ab31 | | | 83 | * Same options as [`stdio`](https://nodejs.org/docs/latest-v18.x/api/child_process.html#optionsstdio). |
| b69ab31 | | | 84 | * @default 'pipe' |
| b69ab31 | | | 85 | */ |
| b69ab31 | | | 86 | readonly stderr?: IOType | Stream | number | null | undefined; |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | /** |
| b69ab31 | | | 89 | * Strip the final newline character from the (awaitable) output. |
| b69ab31 | | | 90 | * @default true |
| b69ab31 | | | 91 | */ |
| b69ab31 | | | 92 | readonly stripFinalNewline?: boolean; |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | /** |
| b69ab31 | | | 95 | * Whether a NodeIPC channel should be open. See the docs about [`stdio`](https://nodejs.org/docs/latest-v18.x/api/child_process.html#optionsstdio) for more info. |
| b69ab31 | | | 96 | * @default false |
| b69ab31 | | | 97 | */ |
| b69ab31 | | | 98 | readonly ipc?: boolean; |
| b69ab31 | | | 99 | } |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | interface KillOptions { |
| b69ab31 | | | 102 | /** |
| b69ab31 | | | 103 | * Milliseconds to wait for the child process to terminate before sending `SIGKILL`. |
| b69ab31 | | | 104 | * Can be disabled with `false`. |
| b69ab31 | | | 105 | * @default 5000 |
| b69ab31 | | | 106 | */ |
| b69ab31 | | | 107 | forceKillAfterTimeout?: number | boolean; |
| b69ab31 | | | 108 | } |
| b69ab31 | | | 109 | |
| b69ab31 | | | 110 | type KillParam = number | NodeJS.Signals | undefined; |
| b69ab31 | | | 111 | const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5; |
| b69ab31 | | | 112 | |
| b69ab31 | | | 113 | function spawnedKill( |
| b69ab31 | | | 114 | kill: ChildProcess['kill'], |
| b69ab31 | | | 115 | signal: KillParam = 'SIGTERM', |
| b69ab31 | | | 116 | options: KillOptions = {}, |
| b69ab31 | | | 117 | ): boolean { |
| b69ab31 | | | 118 | const killResult = kill(signal); |
| b69ab31 | | | 119 | |
| b69ab31 | | | 120 | if (shouldForceKill(signal, options, killResult)) { |
| b69ab31 | | | 121 | const timeout = getForceKillAfterTimeout(options); |
| b69ab31 | | | 122 | setTimeout(() => { |
| b69ab31 | | | 123 | kill('SIGKILL'); |
| b69ab31 | | | 124 | }, timeout); |
| b69ab31 | | | 125 | } |
| b69ab31 | | | 126 | |
| b69ab31 | | | 127 | return killResult; |
| b69ab31 | | | 128 | } |
| b69ab31 | | | 129 | |
| b69ab31 | | | 130 | function getForceKillAfterTimeout({forceKillAfterTimeout = true}: KillOptions): number { |
| b69ab31 | | | 131 | if (typeof forceKillAfterTimeout !== 'number') { |
| b69ab31 | | | 132 | return DEFAULT_FORCE_KILL_TIMEOUT; |
| b69ab31 | | | 133 | } |
| b69ab31 | | | 134 | |
| b69ab31 | | | 135 | if (!Number.isFinite(forceKillAfterTimeout) || forceKillAfterTimeout < 0) { |
| b69ab31 | | | 136 | throw new TypeError( |
| b69ab31 | | | 137 | `Expected the \`forceKillAfterTimeout\` option to be a non-negative integer, got \`${forceKillAfterTimeout}\` (${typeof forceKillAfterTimeout})`, |
| b69ab31 | | | 138 | ); |
| b69ab31 | | | 139 | } |
| b69ab31 | | | 140 | |
| b69ab31 | | | 141 | return forceKillAfterTimeout; |
| b69ab31 | | | 142 | } |
| b69ab31 | | | 143 | |
| b69ab31 | | | 144 | function shouldForceKill( |
| b69ab31 | | | 145 | signal: KillParam, |
| b69ab31 | | | 146 | {forceKillAfterTimeout}: KillOptions, |
| b69ab31 | | | 147 | killResult: boolean, |
| b69ab31 | | | 148 | ): boolean { |
| b69ab31 | | | 149 | const isSigTerm = signal === os.constants.signals.SIGTERM || signal == 'SIGTERM'; |
| b69ab31 | | | 150 | return isSigTerm && forceKillAfterTimeout !== false && killResult; |
| b69ab31 | | | 151 | } |
| b69ab31 | | | 152 | |
| b69ab31 | | | 153 | export interface EjecaReturn { |
| b69ab31 | | | 154 | /** |
| b69ab31 | | | 155 | * The exit code if the child exited on its own. |
| b69ab31 | | | 156 | */ |
| b69ab31 | | | 157 | exitCode: number; |
| b69ab31 | | | 158 | |
| b69ab31 | | | 159 | /** |
| b69ab31 | | | 160 | * The signal by which the child process was terminated, `undefined` if the process was not killed. |
| b69ab31 | | | 161 | * |
| b69ab31 | | | 162 | * Essentially obtained through `signal` on the `exit` event from [`ChildProcess`](https://nodejs.org/docs/latest-v18.x/api/child_process.html#event-exit) |
| b69ab31 | | | 163 | */ |
| b69ab31 | | | 164 | signal?: string; |
| b69ab31 | | | 165 | |
| b69ab31 | | | 166 | /** |
| b69ab31 | | | 167 | * The file and arguments that were run, escaped. Useful for logging. |
| b69ab31 | | | 168 | */ |
| b69ab31 | | | 169 | escapedCommand: string; |
| b69ab31 | | | 170 | |
| b69ab31 | | | 171 | /** |
| b69ab31 | | | 172 | * The output of the process on stdout. |
| b69ab31 | | | 173 | */ |
| b69ab31 | | | 174 | stdout: string; |
| b69ab31 | | | 175 | |
| b69ab31 | | | 176 | /** |
| b69ab31 | | | 177 | * The output of the process on stderr. |
| b69ab31 | | | 178 | */ |
| b69ab31 | | | 179 | stderr: string; |
| b69ab31 | | | 180 | |
| b69ab31 | | | 181 | /** |
| b69ab31 | | | 182 | * Whether the process was killed. |
| b69ab31 | | | 183 | */ |
| b69ab31 | | | 184 | killed: boolean; |
| b69ab31 | | | 185 | } |
| b69ab31 | | | 186 | |
| b69ab31 | | | 187 | interface EjecaChildPromise { |
| b69ab31 | | | 188 | catch<ResultType = never>( |
| b69ab31 | | | 189 | onRejected?: (reason: EjecaError) => ResultType | PromiseLike<ResultType>, |
| b69ab31 | | | 190 | ): Promise<EjecaReturn | ResultType>; |
| b69ab31 | | | 191 | |
| b69ab31 | | | 192 | /** |
| b69ab31 | | | 193 | * Essentially the same as [`subprocess.kill`](https://nodejs.org/docs/latest-v18.x/api/child_process.html#subprocesskillsignal), but |
| b69ab31 | | | 194 | * with the caveat of having the processes SIGKILL'ed after a few seconds if the original signal |
| b69ab31 | | | 195 | * didn't successfully terminate the process. This behavior is configurable through the `options` option. |
| b69ab31 | | | 196 | */ |
| b69ab31 | | | 197 | kill(signal?: KillParam, options?: KillOptions): boolean; |
| b69ab31 | | | 198 | |
| b69ab31 | | | 199 | getOneMessage(): Promise<Serializable>; |
| b69ab31 | | | 200 | } |
| b69ab31 | | | 201 | |
| b69ab31 | | | 202 | export type EjecaChildProcess = ChildProcess & EjecaChildPromise & Promise<EjecaReturn>; |
| b69ab31 | | | 203 | |
| b69ab31 | | | 204 | // The return value is a mixin of `childProcess` and `Promise` |
| b69ab31 | | | 205 | function getMergePromise( |
| b69ab31 | | | 206 | spawned: ChildProcess, |
| b69ab31 | | | 207 | promise: Promise<EjecaReturn>, |
| b69ab31 | | | 208 | ): ChildProcess & Promise<EjecaReturn> { |
| b69ab31 | | | 209 | const s2 = Object.create(spawned); |
| b69ab31 | | | 210 | // @ts-expect-error: we are doing some good old monkey patching here |
| b69ab31 | | | 211 | s2.then = (...args) => { |
| b69ab31 | | | 212 | return promise.then(...args); |
| b69ab31 | | | 213 | }; |
| b69ab31 | | | 214 | // @ts-expect-error: we are doing some good old monkey patching here |
| b69ab31 | | | 215 | s2.catch = (...args) => { |
| b69ab31 | | | 216 | return promise.catch(...args); |
| b69ab31 | | | 217 | }; |
| b69ab31 | | | 218 | // @ts-expect-error: we are doing some good old monkey patching here |
| b69ab31 | | | 219 | s2.finally = (...args) => { |
| b69ab31 | | | 220 | return promise.finally(...args); |
| b69ab31 | | | 221 | }; |
| b69ab31 | | | 222 | |
| b69ab31 | | | 223 | return s2 as unknown as ChildProcess & Promise<EjecaReturn>; |
| b69ab31 | | | 224 | } |
| b69ab31 | | | 225 | |
| b69ab31 | | | 226 | function escapedCmd(file: string, args: readonly string[]): string { |
| b69ab31 | | | 227 | const allargs = [file, ...args.map(arg => `"${arg.replace(/"/g, '\\"')}"`)]; |
| b69ab31 | | | 228 | return allargs.join(' '); |
| b69ab31 | | | 229 | } |
| b69ab31 | | | 230 | |
| b69ab31 | | | 231 | // Use promises instead of `child_process` events |
| b69ab31 | | | 232 | function getSpawnedPromise( |
| b69ab31 | | | 233 | spawned: ChildProcess, |
| b69ab31 | | | 234 | escapedCommand: string, |
| b69ab31 | | | 235 | options?: EjecaOptions, |
| b69ab31 | | | 236 | ): Promise<EjecaReturn> { |
| b69ab31 | | | 237 | const {stdout, stderr} = spawned; |
| b69ab31 | | | 238 | const spawnedPromise = new Promise<{exitCode: number; signal?: string}>((resolve, reject) => { |
| b69ab31 | | | 239 | spawned.on('exit', (exitCode, signal) => { |
| b69ab31 | | | 240 | resolve({exitCode: exitCode ?? -1, signal: signal ?? undefined}); |
| b69ab31 | | | 241 | }); |
| b69ab31 | | | 242 | |
| b69ab31 | | | 243 | spawned.on('error', error => { |
| b69ab31 | | | 244 | reject(error); |
| b69ab31 | | | 245 | }); |
| b69ab31 | | | 246 | |
| b69ab31 | | | 247 | if (spawned.stdin) { |
| b69ab31 | | | 248 | spawned.stdin.on('error', error => { |
| b69ab31 | | | 249 | reject(error); |
| b69ab31 | | | 250 | }); |
| b69ab31 | | | 251 | } |
| b69ab31 | | | 252 | }); |
| b69ab31 | | | 253 | |
| b69ab31 | | | 254 | return Promise.all([spawnedPromise, getStreamPromise(stdout), getStreamPromise(stderr)]).then( |
| b69ab31 | | | 255 | values => { |
| b69ab31 | | | 256 | const [{exitCode, signal}, stdout, stderr] = values; |
| b69ab31 | | | 257 | const stripfinalNl = options?.stripFinalNewline ?? true; |
| b69ab31 | | | 258 | const ret: EjecaReturn = { |
| b69ab31 | | | 259 | exitCode, |
| b69ab31 | | | 260 | signal, |
| b69ab31 | | | 261 | stdout: maybeStripFinalNewline(stdout, stripfinalNl), |
| b69ab31 | | | 262 | stderr: maybeStripFinalNewline(stderr, stripfinalNl), |
| b69ab31 | | | 263 | killed: false, |
| b69ab31 | | | 264 | escapedCommand, |
| b69ab31 | | | 265 | }; |
| b69ab31 | | | 266 | if (exitCode !== 0 || signal != undefined) { |
| b69ab31 | | | 267 | throw new EjecaError(ret); |
| b69ab31 | | | 268 | } |
| b69ab31 | | | 269 | return ret; |
| b69ab31 | | | 270 | }, |
| b69ab31 | | | 271 | ); |
| b69ab31 | | | 272 | } |
| b69ab31 | | | 273 | |
| b69ab31 | | | 274 | export class EjecaError extends Error implements EjecaReturn { |
| b69ab31 | | | 275 | escapedCommand: string; |
| b69ab31 | | | 276 | exitCode: number; |
| b69ab31 | | | 277 | signal?: string; |
| b69ab31 | | | 278 | stdout: string; |
| b69ab31 | | | 279 | stderr: string; |
| b69ab31 | | | 280 | killed: boolean; |
| b69ab31 | | | 281 | |
| b69ab31 | | | 282 | constructor(info: EjecaReturn) { |
| b69ab31 | | | 283 | const message = |
| b69ab31 | | | 284 | `Command \`${truncate(info.escapedCommand, 50)}\` ` + |
| b69ab31 | | | 285 | (info.signal != null ? 'was killed' : 'exited with non-zero status') + |
| b69ab31 | | | 286 | (info.signal != null ? ` with signal ${info.signal}` : ` with exit code ${info.exitCode}`); |
| b69ab31 | | | 287 | super(message); |
| b69ab31 | | | 288 | |
| b69ab31 | | | 289 | this.exitCode = info.exitCode; |
| b69ab31 | | | 290 | this.signal = info.signal; |
| b69ab31 | | | 291 | this.stdout = info.stdout; |
| b69ab31 | | | 292 | this.stderr = info.stderr; |
| b69ab31 | | | 293 | this.killed = info.killed; |
| b69ab31 | | | 294 | this.escapedCommand = info.escapedCommand; |
| b69ab31 | | | 295 | } |
| b69ab31 | | | 296 | |
| b69ab31 | | | 297 | toString() { |
| b69ab31 | | | 298 | return `${this.message}\n${JSON.stringify(this, undefined, 2)}\n`; |
| b69ab31 | | | 299 | } |
| b69ab31 | | | 300 | } |
| b69ab31 | | | 301 | |
| b69ab31 | | | 302 | function getStreamPromise(origStream: Stream | null): Promise<string> { |
| b69ab31 | | | 303 | const stream = origStream ?? new Readable({read() {}}); |
| b69ab31 | | | 304 | return getStream(stream, {encoding: 'utf8'}); |
| b69ab31 | | | 305 | } |
| b69ab31 | | | 306 | |
| b69ab31 | | | 307 | function commonToSpawnOptions(options?: EjecaOptions): SpawnOptions { |
| b69ab31 | | | 308 | const env = options?.env |
| b69ab31 | | | 309 | ? (options.extendEnv ?? true) |
| b69ab31 | | | 310 | ? {...process.env, ...options.env} |
| b69ab31 | | | 311 | : options.env |
| b69ab31 | | | 312 | : process.env; |
| b69ab31 | | | 313 | const stdin = options?.stdin ?? 'pipe'; |
| b69ab31 | | | 314 | const stdout = options?.stdout ?? 'pipe'; |
| b69ab31 | | | 315 | const stderr = options?.stderr ?? 'pipe'; |
| b69ab31 | | | 316 | return { |
| b69ab31 | | | 317 | cwd: options?.cwd || process.cwd(), |
| b69ab31 | | | 318 | env, |
| b69ab31 | | | 319 | stdio: options?.ipc ? [stdin, stdout, stderr, 'ipc'] : [stdin, stdout, stderr], |
| b69ab31 | | | 320 | windowsHide: true, |
| b69ab31 | | | 321 | }; |
| b69ab31 | | | 322 | } |
| b69ab31 | | | 323 | |
| b69ab31 | | | 324 | /** |
| b69ab31 | | | 325 | * Essentially a wrapper for [`child_process.spawn`](https://nodejs.org/docs/latest-v18.x/api/child_process.html#child_processspawncommand-args-options), which |
| b69ab31 | | | 326 | * additionally makes the result awaitable through `EjecaChildPromise`. `_file`, `_args` and `_options` |
| b69ab31 | | | 327 | * are essentially the same as the args for `child_process.spawn`. |
| b69ab31 | | | 328 | * |
| b69ab31 | | | 329 | * It also has a couple of additional features: |
| b69ab31 | | | 330 | * - Adds a forced timeout kill for `child_process.kill` through `EjecaChildPromise.kill` |
| b69ab31 | | | 331 | * - Allows feeding to stdin through `_options.input` |
| b69ab31 | | | 332 | */ |
| b69ab31 | | | 333 | export function ejeca( |
| b69ab31 | | | 334 | file: string, |
| b69ab31 | | | 335 | args: readonly string[], |
| b69ab31 | | | 336 | options?: EjecaOptions, |
| b69ab31 | | | 337 | ): EjecaChildProcess { |
| b69ab31 | | | 338 | const spawned = spawn(file, args, commonToSpawnOptions(options)); |
| b69ab31 | | | 339 | const spawnedPromise = getSpawnedPromise(spawned, escapedCmd(file, args), options); |
| b69ab31 | | | 340 | const mergedPromise = getMergePromise(spawned, spawnedPromise); |
| b69ab31 | | | 341 | |
| b69ab31 | | | 342 | // TODO: Handle streams |
| b69ab31 | | | 343 | if (options && options.input) { |
| b69ab31 | | | 344 | mergedPromise.stdin?.end(options.input); |
| b69ab31 | | | 345 | } |
| b69ab31 | | | 346 | |
| b69ab31 | | | 347 | const ecp = Object.create(mergedPromise); |
| b69ab31 | | | 348 | ecp.kill = (p: KillParam, o?: KillOptions) => { |
| b69ab31 | | | 349 | return spawnedKill(s => mergedPromise.kill(s), p, o); |
| b69ab31 | | | 350 | }; |
| b69ab31 | | | 351 | |
| b69ab31 | | | 352 | if (options && options.ipc) { |
| b69ab31 | | | 353 | ecp._ipcMessagesQueue = []; |
| b69ab31 | | | 354 | ecp._ipcPendingPromises = []; |
| b69ab31 | | | 355 | mergedPromise.on('message', message => { |
| b69ab31 | | | 356 | if (ecp._ipcPendingPromises.length > 0) { |
| b69ab31 | | | 357 | const resolve = ecp._ipcPendingPromises.shift()[0]; |
| b69ab31 | | | 358 | resolve(message); |
| b69ab31 | | | 359 | } else { |
| b69ab31 | | | 360 | ecp._ipcMessagesQueue.push(message); |
| b69ab31 | | | 361 | } |
| b69ab31 | | | 362 | }); |
| b69ab31 | | | 363 | mergedPromise.on('error', error => { |
| b69ab31 | | | 364 | while (ecp._ipcPendingPromises.length > 0) { |
| b69ab31 | | | 365 | const reject = ecp._ipcPendingPromises.shift()[1]; |
| b69ab31 | | | 366 | reject(error); |
| b69ab31 | | | 367 | } |
| b69ab31 | | | 368 | }); |
| b69ab31 | | | 369 | mergedPromise.on('exit', (_exitCode, _signal) => { |
| b69ab31 | | | 370 | while (ecp._ipcPendingPromises.length > 0) { |
| b69ab31 | | | 371 | const reject = ecp._ipcPendingPromises.shift()[1]; |
| b69ab31 | | | 372 | reject(new Error('IPC channel closed before receiving a message')); |
| b69ab31 | | | 373 | } |
| b69ab31 | | | 374 | }); |
| b69ab31 | | | 375 | |
| b69ab31 | | | 376 | ecp.getOneMessage = () => { |
| b69ab31 | | | 377 | return new Promise<string>((resolve, reject) => { |
| b69ab31 | | | 378 | if (ecp._ipcMessagesQueue.length > 0) { |
| b69ab31 | | | 379 | resolve(ecp._ipcMessagesQueue.shift()); |
| b69ab31 | | | 380 | } else { |
| b69ab31 | | | 381 | ecp._ipcPendingPromises.push([resolve, reject]); |
| b69ab31 | | | 382 | } |
| b69ab31 | | | 383 | }); |
| b69ab31 | | | 384 | }; |
| b69ab31 | | | 385 | } else { |
| b69ab31 | | | 386 | ecp.getOneMessage = () => { |
| b69ab31 | | | 387 | throw new Error('IPC not enabled'); |
| b69ab31 | | | 388 | }; |
| b69ab31 | | | 389 | } |
| b69ab31 | | | 390 | |
| b69ab31 | | | 391 | return ecp as unknown as EjecaChildProcess; |
| b69ab31 | | | 392 | } |
| b69ab31 | | | 393 | |
| b69ab31 | | | 394 | /** |
| b69ab31 | | | 395 | * Extract the actually useful stderr part of the Ejeca Error, to avoid the long command args being printed first. |
| b69ab31 | | | 396 | */ |
| b69ab31 | | | 397 | export function simplifyEjecaError(error: EjecaError): Error { |
| b69ab31 | | | 398 | return new Error(error.stderr.trim() || error.message); |
| b69ab31 | | | 399 | } |