Refactor `all` option (#975)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 4940511 · Browse files · 10 files·+23 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+23 added−23 removed
- other:+23 added−23 removed
Provenance
- Commit
7faa8f5- Tree
d869e10- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
4940511
10 files changed+23 added−23 removed
modified:lib/return/output.js+2 added-2 removed
123445678891011128 unmodified linesimport stripFinalNewlineFunction from 'strip-final-newline';import {logFinalResult} from '../verbose/complete.js';
export const stripNewline = (value, {stripFinalNewline}, isAll, fdNumber) => getStripFinalNewline(stripFinalNewline, isAll, fdNumber) && value !== undefined && !Array.isArray(value)export const stripNewline = (value, {stripFinalNewline}, fdNumber) => getStripFinalNewline(stripFinalNewline, fdNumber) && value !== undefined && !Array.isArray(value) ? stripFinalNewlineFunction(value) : value;
export const getStripFinalNewline = (stripFinalNewline, isAll, fdNumber) => isAllexport const getStripFinalNewline = (stripFinalNewline, fdNumber) => fdNumber === 'all' ? stripFinalNewline[1] || stripFinalNewline[2] : stripFinalNewline[fdNumber];
export const handleResult = (result, verboseInfo, {reject}) => {8 unmodified linesmodified:lib/stdio/output-sync.js+2 added-2 removed
105 unmodified linesExpand all1061071081091101101111121131141141151161171184 unmodified lines105 unmodified linesExpand all
if (Array.isArray(stdout)) { return Array.isArray(stderr) ? [...stdout, ...stderr] : [...stdout, stripNewline(stderr, options, true, 2)]; : [...stdout, stripNewline(stderr, options, 'all')]; }
if (Array.isArray(stderr)) { return [stripNewline(stdout, options, true, 1), ...stderr]; return [stripNewline(stdout, options, 'all'), ...stderr]; }
if (isUint8Array(stdout) && isUint8Array(stderr)) { return concatUint8Arrays([stdout, stderr]);4 unmodified linesmodified:lib/stream/all.js+1 added-2 removed
7 unmodified lines8910111212131415161617181927 unmodified lines7 unmodified lines
// Read the contents of `subprocess.all` and|or wait for its completionexport const waitForAllStream = ({subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, verboseInfo, streamInfo}) => waitForSubprocessStream({ ...getAllStream(subprocess, buffer), fdNumber: 1, fdNumber: 'all', encoding, maxBuffer: maxBuffer[1] + maxBuffer[2], lines: lines[1] || lines[2], isAll: true, allMixed: getAllMixed(subprocess), stripFinalNewline, verboseInfo, streamInfo,27 unmodified linesmodified:lib/stream/max-buffer.js+2 added-2 removed
1 import {MaxBufferError} from 'get-stream';2 import {getStreamName} from '../utils.js';3 4-export const handleMaxBuffer = ({error, stream, readableObjectMode, lines, encoding, fdNumber, isAll}) => {4+export const handleMaxBuffer = ({error, stream, readableObjectMode, lines, encoding, fdNumber}) => {5 if (!(error instanceof MaxBufferError)) {6 throw error;7 }8 9- if (isAll) {9+ if (fdNumber === 'all') {10 return error;11 }12 13 const unit = getMaxBufferUnit(readableObjectMode, lines, encoding);42 unmodified linesmodified:lib/stream/resolve.js+1 added-1 removed
54 unmodified lines55 };56 57 // Read the contents of `subprocess.std*` and|or wait for its completion58 const waitForSubprocessStreams = ({subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, verboseInfo, streamInfo}) =>59- subprocess.stdio.map((stream, fdNumber) => waitForSubprocessStream({stream, fdNumber, encoding, buffer: buffer[fdNumber], maxBuffer: maxBuffer[fdNumber], lines: lines[fdNumber], isAll: false, allMixed: false, stripFinalNewline, verboseInfo, streamInfo}));59+ subprocess.stdio.map((stream, fdNumber) => waitForSubprocessStream({stream, fdNumber, encoding, buffer: buffer[fdNumber], maxBuffer: maxBuffer[fdNumber], lines: lines[fdNumber], allMixed: false, stripFinalNewline, verboseInfo, streamInfo}));60 61 // Transforms replace `subprocess.std*`, which means they are not exposed to users.62 // However, we still want to wait for their completion.63 const waitForOriginalStreams = (originalStreams, subprocess, streamInfo) =>23 unmodified linesmodified:lib/stream/subprocess.js+8 added-8 removed
5 unmodified lines6 import {getStripFinalNewline} from '../return/output.js';7 import {handleMaxBuffer} from './max-buffer.js';8 import {waitForStream, isInputFileDescriptor} from './wait.js';9 10-export const waitForSubprocessStream = async ({stream, fdNumber, encoding, buffer, maxBuffer, lines, isAll, allMixed, stripFinalNewline, verboseInfo, streamInfo}) => {10+export const waitForSubprocessStream = async ({stream, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, verboseInfo, streamInfo}) => {11 if (!stream) {12 return;13 }14 15 const onStreamEnd = waitForStream(stream, fdNumber, streamInfo);16 const [output] = await Promise.all([17- waitForDefinedStream({stream, onStreamEnd, fdNumber, encoding, buffer, maxBuffer, lines, isAll, allMixed, stripFinalNewline, verboseInfo, streamInfo}),17+ waitForDefinedStream({stream, onStreamEnd, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, verboseInfo, streamInfo}),18 onStreamEnd,19 ]);20 return output;21 };22 23-const waitForDefinedStream = async ({stream, onStreamEnd, fdNumber, encoding, buffer, maxBuffer, lines, isAll, allMixed, stripFinalNewline, verboseInfo, streamInfo, streamInfo: {fileDescriptors}}) => {23+const waitForDefinedStream = async ({stream, onStreamEnd, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, verboseInfo, streamInfo, streamInfo: {fileDescriptors}}) => {24 if (isInputFileDescriptor(streamInfo, fdNumber)) {25 return;26 }27 28- if (!isAll && shouldLogOutput({stdioItems: fileDescriptors[fdNumber].stdioItems, encoding, verboseInfo, fdNumber})) {28+ if (shouldLogOutput({stdioItems: fileDescriptors[fdNumber]?.stdioItems, encoding, verboseInfo, fdNumber})) {29 const linesIterable = iterateForResult({stream, onStreamEnd, lines: true, encoding, stripFinalNewline: true, allMixed});30 logLines(linesIterable, stream, verboseInfo);31 }32 1 unmodified line34 await resumeStream(stream);35 return;36 }37 38- const stripFinalNewlineValue = getStripFinalNewline(stripFinalNewline, isAll, fdNumber);38+ const stripFinalNewlineValue = getStripFinalNewline(stripFinalNewline, fdNumber);39 const iterable = iterateForResult({stream, onStreamEnd, lines, encoding, stripFinalNewline: stripFinalNewlineValue, allMixed});40- return getStreamContents({stream, iterable, fdNumber, encoding, maxBuffer, lines, isAll});40+ return getStreamContents({stream, iterable, fdNumber, encoding, maxBuffer, lines});41 };42 43 // When using `buffer: false`, users need to read `subprocess.stdout|stderr|all` right away44 // See https://github.com/sindresorhus/execa/issues/730 and https://github.com/sindresorhus/execa/pull/729#discussion_r14654963103 unmodified lines48 stream.resume();49 }50 };51 52-const getStreamContents = async ({stream, stream: {readableObjectMode}, iterable, fdNumber, encoding, maxBuffer, lines, isAll}) => {52+const getStreamContents = async ({stream, stream: {readableObjectMode}, iterable, fdNumber, encoding, maxBuffer, lines}) => {53 try {54 if (readableObjectMode || lines) {55 return await getStreamAsArray(iterable, {maxBuffer});56 }3 unmodified lines60 }61 62 return await getStream(iterable, {maxBuffer});63 } catch (error) {64- return handleBufferedData(handleMaxBuffer({error, stream, readableObjectMode, lines, encoding, fdNumber, isAll}));64+ return handleBufferedData(handleMaxBuffer({error, stream, readableObjectMode, lines, encoding, fdNumber}));65 }66 };67 68 // On failure, `result.stdout|stderr|all` should contain the currently buffered stream14 unmodified linesmodified:lib/stream/wait.js+1 added-1 removed
79 unmodified lines80 // For example, `subprocess.stdin` is technically a Duplex, but can only be used as a writable.81 // Therefore, we need to use the file descriptor's direction (`stdin` is input, `stdout` is output, etc.).82 // However, while `subprocess.std*` and transforms follow that direction, any stream passed the `std*` option has the opposite direction.83 // For example, `subprocess.stdin` is a writable, but the `stdin` option is a readable.84-export const isInputFileDescriptor = ({fileDescriptors}, fdNumber) => fileDescriptors[fdNumber].direction === 'input';84+export const isInputFileDescriptor = ({fileDescriptors}, fdNumber) => fdNumber !== 'all' && fileDescriptors[fdNumber].direction === 'input';85 86 // When `stream.destroy()` is called without an `error` argument, stream is aborted.87 // This is the only way to abort a readable stream, which can be useful in some instances.88 // Therefore, we ignore this error on readable streams.8 unmodified linesmodified:lib/verbose/output.js+2 added-1 removed
7 unmodified lines8 // `ipc` cannot use piping.9 // `inherit` would result in double printing.10 // They can also lead to double printing when passing file descriptor integers or `process.std*`.11 // This only leaves with `pipe` and `overlapped`.12-export const shouldLogOutput = ({stdioItems, encoding, verboseInfo: {verbose}, fdNumber}) => verbose[fdNumber] === 'full'12+export const shouldLogOutput = ({stdioItems, encoding, verboseInfo: {verbose}, fdNumber}) => fdNumber !== 'all'13+ && verbose[fdNumber] === 'full'14 && !BINARY_ENCODINGS.has(encoding)15 && fdUsesVerbose(fdNumber)16 && (stdioItems.some(({type, value}) => type === 'native' && PIPED_STDIO_VALUES.has(value))17 || stdioItems.every(({type}) => TRANSFORM_TYPES.has(type)));42 unmodified linesmodified:lib/async.js+2 added-2 removed
80 unmodified lines81 allResult,82 ] = await getSubprocessResult({subprocess, options, context, verboseInfo, fileDescriptors, originalStreams, controller});83 controller.abort();84 85- const stdio = stdioResults.map((stdioResult, fdNumber) => stripNewline(stdioResult, options, false, fdNumber));86- const all = stripNewline(allResult, options, true);85+ const stdio = stdioResults.map((stdioResult, fdNumber) => stripNewline(stdioResult, options, fdNumber));86+ const all = stripNewline(allResult, options, 'all');87 const result = getAsyncResult({errorInfo, exitCode, signal, stdio, all, context, options, command, escapedCommand, startTime});88 return handleResult(result, verboseInfo, options);89 };90 17 unmodified linesmodified:lib/sync.js+2 added-2 removed
56 unmodified lines57 }58 59 const {resultError, exitCode, signal, timedOut, isMaxBuffer} = getSyncExitResult(syncResult, options);60 const {output, error = resultError} = transformOutputSync({fileDescriptors, syncResult, options, isMaxBuffer, verboseInfo});61- const stdio = output.map((stdioOutput, fdNumber) => stripNewline(stdioOutput, options, false, fdNumber));62- const all = stripNewline(getAllSync(output, options), options, true);61+ const stdio = output.map((stdioOutput, fdNumber) => stripNewline(stdioOutput, options, fdNumber));62+ const all = stripNewline(getAllSync(output, options), options, 'all');63 return getSyncResult({error, exitCode, signal, timedOut, isMaxBuffer, stdio, all, options, command, escapedCommand, startTime});64 };65 66 const runSubprocessSync = ({file, args, options, command, escapedCommand, fileDescriptors, startTime}) => {28 unmodified lines