Skip to content
Myr

Refactor `handleSpawned()` (#290)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by Sindre Sorhus · parent fb754cf · Browse files · 1 file+27 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+27 added−22 removed

  • other:+27 added−22 removed

1 file+27 added−22 removed

modified:index.js+27 added-22 removed
300 unmodified lines
301302 return forceKillAfterTimeout;303}304305function handleSpawned(spawned, context) {306 return new Promise((resolve, reject) => {307 spawned.on('exit', (code, signal) => {308 if (context.timedOut) {309 reject(Object.assign(new Error('Timed out'), {code, signal}));310 return;311 }312313 resolve({code, signal});314 });315316 spawned.on('error', error => {317 reject(error);318 });319320 if (spawned.stdin) {321 spawned.stdin.on('error', error => {322 reject(error);323 });324 }325 });326}327328const execa = (file, args, options) => {329 const parsed = handleArgs(file, args, options);330 const command = joinCommand(file, args);331
23 unmodified lines
355 });356 }357358 let timeoutId;336 let timedOut = false;359 const context = {timedOut: false};360 let isCanceled = false;361362 const cleanup = () => {363 if (timeoutId !== undefined) {
8 unmodified lines
372373 if (parsed.options.timeout > 0) {374 timeoutId = setTimeout(() => {375 timeoutId = undefined;353 timedOut = true;376 context.timedOut = true;377 spawned.kill(parsed.options.killSignal);378 }, parsed.options.timeout);379 }380381 // TODO: Use native "finally" syntax when targeting Node.js 10359 const processDone = pFinally(new Promise((resolve, reject) => {360 spawned.on('exit', (code, signal) => {361 if (timedOut) {362 return reject(Object.assign(new Error('Timed out'), {code, signal}));363 }382 const processDone = pFinally(handleSpawned(spawned, context), cleanup);383365 resolve({code, signal});366 });367368 spawned.on('error', error => {369 reject(error);370 });371372 if (spawned.stdin) {373 spawned.stdin.on('error', error => {374 reject(error);375 });376 }377 }), cleanup);378384 const handlePromise = async () => {385 const [result, stdout, stderr, all] = await getPromiseResult(spawned, parsed.options, processDone);386 result.stdout = handleOutput(parsed.options, stdout);387 result.stderr = handleOutput(parsed.options, stderr);
3 unmodified lines
391 const error = makeError(result, {392 code: result.code,393 command,394 parsed,390 timedOut,395 timedOut: context.timedOut,396 isCanceled,397 killed: spawned.killed398 });399
123 unmodified lines