Skip to content
Myr

Separate promise-related logic into their own files (#325)

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 cba87e1 · Browse files · 4 files+108 added−95 removed
This commit

State

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

Diff stats

4 files+108 added−95 removed

  • tests:+52 added−47 removed
  • other:+56 added−48 removed

4 files+108 added−95 removed

added:lib/promise.js+54 added
added:test/promise.js+52 added
modified:test/test.js-47 removed
223 unmodified lines
224225 process.kill(pid, 'SIGKILL');226});227228test('promise methods are not enumerable', t => {229 const descriptors = Object.getOwnPropertyDescriptors(execa('noop'));230 // eslint-disable-next-line promise/prefer-await-to-then231 t.false(descriptors.then.enumerable);232 t.false(descriptors.catch.enumerable);233 // TOOD: Remove the `if`-guard when targeting Node.js 10234 if (Promise.prototype.finally) {235 t.false(descriptors.finally.enumerable);236 }237});238239// TOOD: Remove the `if`-guard when targeting Node.js 10240if (Promise.prototype.finally) {241 test('finally function is executed on success', async t => {242 let isCalled = false;243 const {stdout} = await execa('noop', ['foo']).finally(() => {244 isCalled = true;245 });246 t.is(isCalled, true);247 t.is(stdout, 'foo');248 });249250 test('finally function is executed on failure', async t => {251 let isError = false;252 const {stdout, stderr} = await t.throwsAsync(execa('exit', ['2']).finally(() => {253 isError = true;254 }));255 t.is(isError, true);256 t.is(typeof stdout, 'string');257 t.is(typeof stderr, 'string');258 });259260 test('throw in finally function bubbles up on success', async t => {261 const {message} = await t.throwsAsync(execa('noop', ['foo']).finally(() => {262 throw new Error('called');263 }));264 t.is(message, 'called');265 });266267 test('throw in finally bubbles up on error', async t => {268 const {message} = await t.throwsAsync(execa('exit', ['2']).finally(() => {269 throw new Error('called');270 }));271 t.is(message, 'called');272 });273}274228test('allow commands with spaces and no array arguments', async t => {229 const {stdout} = await execa('command with space');230 t.is(stdout, '');231});
40 unmodified lines
modified:index.js+2 added-48 removed
7 unmodified lines
8const makeError = require('./lib/error');9const normalizeStdio = require('./lib/stdio');10const {spawnedKill, spawnedCancel, setupTimeout, setExitHandler, cleanup} = require('./lib/kill');11const {handleInput, getSpawnedResult, makeAllStream, validateInputSync} = require('./lib/stream.js');12const {mergePromise, getSpawnedPromise} = require('./lib/promise.js');1314const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;1516const SPACES_REGEXP = / +/g;
61 unmodified lines
7879 return [file, ...args].join(' ');80};8181const mergePromiseProperty = (spawned, getPromise, property) => {82 Object.defineProperty(spawned, property, {83 value(...args) {84 return getPromise()[property](...args);85 },86 writable: true,87 enumerable: false,88 configurable: true89 });90};9192// The return value is a mixin of `childProcess` and `Promise`93const mergePromise = (spawned, getPromise) => {94 mergePromiseProperty(spawned, getPromise, 'then');95 mergePromiseProperty(spawned, getPromise, 'catch');9697 // TODO: Remove the `if`-guard when targeting Node.js 1098 if (Promise.prototype.finally) {99 mergePromiseProperty(spawned, getPromise, 'finally');100 }101102 return spawned;103};104105const handleSpawned = (spawned, context) => {106 return new Promise((resolve, reject) => {107 spawned.on('exit', (code, signal) => {108 if (context.timedOut) {109 reject(Object.assign(new Error('Timed out'), {code, signal}));110 return;111 }112113 resolve({code, signal});114 });115116 spawned.on('error', error => {117 reject(error);118 });119120 if (spawned.stdin) {121 spawned.stdin.on('error', error => {122 reject(error);123 });124 }125 });126};12782const execa = (file, args, options) => {83 const parsed = handleArgs(file, args, options);84 const command = joinCommand(file, args);85
24 unmodified lines
110 const timeoutId = setupTimeout(spawned, parsed.options, context);111 const removeExitHandler = setExitHandler(spawned, parsed.options);112113 // TODO: Use native "finally" syntax when targeting Node.js 10160 const processDone = pFinally(handleSpawned(spawned, context), () => {114 const processDone = pFinally(getSpawnedPromise(spawned, context), () => {115 cleanup(timeoutId, removeExitHandler);116 });117118 const handlePromise = async () => {
163 unmodified lines