Skip to content
Myr

result.js

This file

Path

lib/return/result.js

155 lines · 3.0 KiB · JavaScript

Last changed by

Do not set `undefined` properties on the return value (#992)ehmickybefore adoption0d33b6d

History of this file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–5why, one step back
1import {signalsByName} from 'human-signals';
2import {getDurationMs} from './duration.js';3import {getFinalError} from './final-error.js';4import {createMessages} from './message.js';5

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 6why, one step back
6// Object returned on subprocess success

Rename files (#977)ehmickybefore adoption4b2e316

lines 7–13why, one step back
7export const makeSuccessResult = ({
8 command,9 escapedCommand,10 stdio,11 all,12 options: {cwd},13 startTime,

Do not set `undefined` properties on the return value (#992)ehmickybefore adoption0d33b6d

line 14why, one step back
14}) => omitUndefinedProperties({

Rename files (#977)ehmickybefore adoption4b2e316

lines 15–31why, one step back
15 command,
16 escapedCommand,17 cwd,18 durationMs: getDurationMs(startTime),19 failed: false,20 timedOut: false,21 isCanceled: false,22 isTerminated: false,23 isMaxBuffer: false,24 exitCode: 0,25 stdout: stdio[1],26 stderr: stdio[2],27 all,28 stdio,29 pipedFrom: [],30});31

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 32why, one step back
32// Object returned on subprocess failure before spawning

Rename files (#977)ehmickybefore adoption4b2e316

lines 33–53why, one step back
33export const makeEarlyError = ({
34 error,35 command,36 escapedCommand,37 fileDescriptors,38 options,39 startTime,40 isSync,41}) => makeError({42 error,43 command,44 escapedCommand,45 startTime,46 timedOut: false,47 isCanceled: false,48 isMaxBuffer: false,49 stdio: Array.from({length: fileDescriptors.length}),50 options,51 isSync,52});53

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 54why, one step back
54// Object returned on subprocess failure

Rename files (#977)ehmickybefore adoption4b2e316

lines 55–86why, one step back
55export const makeError = ({
56 error: originalError,57 command,58 escapedCommand,59 startTime,60 timedOut,61 isCanceled,62 isMaxBuffer,63 exitCode: rawExitCode,64 signal: rawSignal,65 stdio,66 all,67 options: {timeoutDuration, timeout = timeoutDuration, cwd, maxBuffer},68 isSync,69}) => {70 const {exitCode, signal, signalDescription} = normalizeExitPayload(rawExitCode, rawSignal);71 const {originalMessage, shortMessage, message} = createMessages({72 stdio,73 all,74 originalError,75 signal,76 signalDescription,77 exitCode,78 escapedCommand,79 timedOut,80 isCanceled,81 isMaxBuffer,82 maxBuffer,83 timeout,84 cwd,85 });86 const error = getFinalError(originalError, message, isSync);

Do not set `undefined` properties on the return value (#992)ehmickybefore adoption0d33b6d

lines 87–103why, one step back
87 Object.assign(error, getErrorProperties({
88 error,89 command,90 escapedCommand,91 startTime,92 timedOut,93 isCanceled,94 isMaxBuffer,95 exitCode,96 signal,97 signalDescription,98 stdio,99 all,100 cwd,101 originalMessage,102 shortMessage,103 }));

Rename files (#977)ehmickybefore adoption4b2e316

lines 104–105why, one step back
104 return error;
105};

Do not set `undefined` properties on the return value (#992)ehmickybefore adoption0d33b6d

lines 106–146why, one step back
106
107const getErrorProperties = ({108 error,109 command,110 escapedCommand,111 startTime,112 timedOut,113 isCanceled,114 isMaxBuffer,115 exitCode,116 signal,117 signalDescription,118 stdio,119 all,120 cwd,121 originalMessage,122 shortMessage,123}) => omitUndefinedProperties({124 shortMessage,125 originalMessage,126 command,127 escapedCommand,128 cwd,129 durationMs: getDurationMs(startTime),130 failed: true,131 timedOut,132 isCanceled,133 isTerminated: signal !== undefined,134 isMaxBuffer,135 exitCode,136 signal,137 signalDescription,138 code: error.cause?.code,139 stdout: stdio[1],140 stderr: stdio[2],141 all,142 stdio,143 pipedFrom: [],144});145146const omitUndefinedProperties = result => Object.fromEntries(Object.entries(result).filter(([, value]) => value !== undefined));

Rename files (#977)ehmickybefore adoption4b2e316

lines 147–155why, one step back
147
148// `signal` and `exitCode` emitted on `subprocess.on('exit')` event can be `null`.149// We normalize them to `undefined`150const normalizeExitPayload = (rawExitCode, rawSignal) => {151 const exitCode = rawExitCode === null ? undefined : rawExitCode;152 const signal = rawSignal === null ? undefined : rawSignal;153 const signalDescription = signal === undefined ? undefined : signalsByName[rawSignal].description;154 return {exitCode, signal, signalDescription};155};