Refactor `finalize()` method (#288)
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 acb9195 · Browse files · 1 file·+42 added−46 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+42 added−46 removed
- other:+42 added−46 removed
Provenance
- Commit
ed87275- Tree
84dd79b- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
acb9195
1 file changed+42 added−46 removed
modified:index.js+42 added-46 removed
358 unmodified lines359 });360 }361 }), cleanup);362 363- const handlePromise = () => {363+ const handlePromise = async () => {364 const stdoutPromise = getStreamPromise(spawned.stdout, {encoding, buffer, maxBuffer});365 const stderrPromise = getStreamPromise(spawned.stderr, {encoding, buffer, maxBuffer});366 const allPromise = getStreamPromise(spawned.all, {encoding, buffer, maxBuffer: maxBuffer * 2});367 368- const finalize = async () => {369- let results;370- try {371- results = await Promise.all([processDone, stdoutPromise, stderrPromise, allPromise]);372- } catch (error) {373- const {code, signal} = error;374- results = await Promise.all([375- {error, code, signal},376- getBufferedData(spawned.stdout, stdoutPromise),377- getBufferedData(spawned.stderr, stderrPromise),378- getBufferedData(spawned.all, allPromise)379- ]);380- }368+ let results;369+ try {370+ results = await Promise.all([processDone, stdoutPromise, stderrPromise, allPromise]);371+ } catch (error) {372+ const {code, signal} = error;373+ results = await Promise.all([374+ {error, code, signal},375+ getBufferedData(spawned.stdout, stdoutPromise),376+ getBufferedData(spawned.stderr, stderrPromise),377+ getBufferedData(spawned.all, allPromise)378+ ]);379+ }380 382- const [result, stdout, stderr, all] = results;383- result.stdout = handleOutput(parsed.options, stdout);384- result.stderr = handleOutput(parsed.options, stderr);385- result.all = handleOutput(parsed.options, all);381+ const [result, stdout, stderr, all] = results;382+ result.stdout = handleOutput(parsed.options, stdout);383+ result.stderr = handleOutput(parsed.options, stderr);384+ result.all = handleOutput(parsed.options, all);385 387- if (result.error || result.code !== 0 || result.signal !== null) {388- const error = makeError(result, {389- code: result.code,390- command,391- parsed,392- timedOut,393- isCanceled,394- killed: spawned.killed395- });386+ if (result.error || result.code !== 0 || result.signal !== null) {387+ const error = makeError(result, {388+ code: result.code,389+ command,390+ parsed,391+ timedOut,392+ isCanceled,393+ killed: spawned.killed394+ });395 397- if (!parsed.options.reject) {398- return error;399- }400-401- throw error;396+ if (!parsed.options.reject) {397+ return error;398 }399 404- return {405- command,406- exitCode: 0,407- exitCodeName: 'SUCCESS',408- stdout: result.stdout,409- stderr: result.stderr,410- all: result.all,411- failed: false,412- timedOut: false,413- isCanceled: false,414- killed: false415- };416- };400+ throw error;401+ }402 418- return finalize();403+ return {404+ command,405+ exitCode: 0,406+ exitCodeName: 'SUCCESS',407+ stdout: result.stdout,408+ stderr: result.stderr,409+ all: result.all,410+ failed: false,411+ timedOut: false,412+ isCanceled: false,413+ killed: false414+ };415 };416 417 crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);418 100 unmodified lines