Skip to content
Myr

Handle `inputFile` missing file (#609)

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

authored by ehmicky · committed by GitHub · parent ec380db · Browse files · 4 files+149 added−84 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+149 added−84 removed

  • tests:+30 added−2 removed
  • other:+119 added−82 removed

4 files+149 added−84 removed

modified:lib/stdio.js+106 added-13 removed
modified:lib/stream.js+2 added-55 removed
1import {once} from 'node:events';2import {createReadStream, readFileSync} from 'node:fs';2import {setTimeout} from 'node:timers/promises';4import {isStream} from 'is-stream';3import getStream, {getStreamAsBuffer} from 'get-stream';4import mergeStreams from '@sindresorhus/merge-streams';58export const validateInputOptions = ({input, inputFile}, {stdinIterableStream}) => {9 if (input !== undefined && inputFile !== undefined) {10 throw new TypeError('The `input` and `inputFile` options cannot be both set.');11 }1213 if (stdinIterableStream !== undefined) {14 if (input !== undefined) {15 throw new TypeError('The `stdin` option cannot be an iterable when the `input` option is set.');16 }1718 if (inputFile !== undefined) {19 throw new TypeError('The `stdin` option cannot be an iterable when the `inputFile` option is set.');20 }21 }22};2324// `input` and `inputFile` option in sync mode25export const handleInputSync = ({input, inputFile}, {stdinIterableStream}) => {26 if (stdinIterableStream !== undefined) {27 throw new TypeError('The `stdin` option cannot be an iterable in sync mode');28 }2930 const inputOption = typeof inputFile === 'string' ? readFileSync(inputFile) : input;3132 if (isStream(inputOption)) {33 throw new TypeError('The `input` option cannot be a stream in sync mode');34 }3536 return inputOption;37};3839// `input` and `inputFile` option in async mode40export const handleInput = (spawned, {input, inputFile}, {stdinIterableStream}) => {41 if (stdinIterableStream !== undefined) {42 stdinIterableStream.pipe(spawned.stdin);43 return;44 }4546 const inputOption = typeof inputFile === 'string' ? createReadStream(inputFile) : input;4748 if (inputOption === undefined) {49 return;50 }5152 if (isStream(inputOption)) {53 inputOption.pipe(spawned.stdin);54 } else {55 spawned.stdin.end(inputOption);56 }57};586// `all` interleaves `stdout` and `stderr`7export const makeAllStream = (spawned, {all}) => {8 if (!all || (!spawned.stdout && !spawned.stderr)) {9 return;
52 unmodified lines
62 throw error;63};6465// Retrieve result of child process: exit code, signal, error, streams (stdout/stderr/all)119export const getSpawnedResult = async (spawned, {encoding, buffer, maxBuffer}, {stdinIterableStream}, processDone) => {66export const getSpawnedResult = async (spawned, {encoding, buffer, maxBuffer}, {stdinStream}, processDone) => {67 const stdoutPromise = getStreamPromise(spawned.stdout, {encoding, buffer, maxBuffer});68 const stderrPromise = getStreamPromise(spawned.stderr, {encoding, buffer, maxBuffer});69 const allPromise = getStreamPromise(spawned.all, {encoding, buffer, maxBuffer: maxBuffer * 2});123 const processDoneOrStreamsError = Promise.race([processDone, ...throwOnStreamsError([stdinIterableStream])]);70 const processDoneOrStreamsError = Promise.race([processDone, ...throwOnStreamsError([stdinStream])]);7172 try {73 return await Promise.all([processDoneOrStreamsError, stdoutPromise, stderrPromise, allPromise]);74 } catch (error) {
9 unmodified lines
modified:test/stream.js+30 added-2 removed
147 unmodified lines
148149test('stdin option cannot be an iterable when "input" is used', t => {150 t.throws(() => {151 execa('stdin.js', {stdin: ['foo', 'bar'], input: 'foobar'});152 }, {message: /when the `input` option/});152 }, {message: /`input` and `stdin` options/});153});154155test('stdin option cannot be an iterable when "inputFile" is used', t => {156 t.throws(() => {157 execa('stdin.js', {stdin: ['foo', 'bar'], inputFile: 'dummy.txt'});158 }, {message: /when the `inputFile` option/});158 }, {message: /`inputFile` and `stdin` options/});159});160161test('stdin option cannot be a generic iterable string', async t => {162 await t.throwsAsync(() => execa('stdin.js', {stdin: 'foobar'}), {code: 'ERR_INVALID_SYNC_FORK_INPUT'});
8 unmodified lines
171 const {stdout} = await execa('stdin.js', {input: 'foobar'});172 t.is(stdout, 'foobar');173});174175test('input option cannot be a String when stdin is set', t => {176 t.throws(() => {177 execa('stdin.js', {input: 'foobar', stdin: 'ignore'});178 }, {message: /`input` and `stdin` options/});179});180181test('input option cannot be a String when stdio is set', t => {182 t.throws(() => {183 execa('stdin.js', {input: 'foobar', stdio: 'ignore'});184 }, {message: /`input` and `stdin` options/});185});186187test('input option can be a Buffer', async t => {188 const {stdout} = await execa('stdin.js', {input: 'testing12'});189 t.is(stdout, 'testing12');190});
5 unmodified lines
196 const {stdout} = await execa('stdin.js', {input: stream});197 t.is(stdout, 'howdy');198});199200test('input option cannot be a Stream when stdin is set', t => {201 t.throws(() => {202 execa('stdin.js', {input: new Stream.PassThrough(), stdin: 'ignore'});203 }, {message: /`input` and `stdin` options/});204});205206test('input option can be used with $', async t => {207 const {stdout} = await $({input: 'foobar'})`stdin.js`;208 t.is(stdout, 'foobar');209});
17 unmodified lines
227 message: /cannot be both set/,228 });229});230231test('inputFile option cannot be set when stdin is set', t => {232 t.throws(() => {233 execa('stdin.js', {inputFile: '', stdin: 'ignore'});234 }, {message: /`inputFile` and `stdin` options/});235});236237test('inputFile errors should be handled', async t => {238 await t.throwsAsync(execa('stdin.js', {inputFile: 'unknown'}), {code: 'ENOENT'});239});240241test('you can write to child.stdin', async t => {242 const subprocess = execa('stdin.js');243 subprocess.stdin.end('unicorns');244 const {stdout} = await subprocess;
160 unmodified lines
modified:index.js+11 added-14 removed
5 unmodified lines
6import stripFinalNewline from 'strip-final-newline';7import {npmRunPathEnv} from 'npm-run-path';8import onetime from 'onetime';9import {makeError} from './lib/error.js';10import {handleStdioOption, normalizeStdioNode} from './lib/stdio.js';10import {handleStdioOption, handleInputOption, pipeStdioOption, normalizeStdioNode} from './lib/stdio.js';11import {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} from './lib/kill.js';12import {addPipeMethods} from './lib/pipe.js';13import {validateInputOptions, handleInput, getSpawnedResult, makeAllStream, handleInputSync} from './lib/stream.js';13import {getSpawnedResult, makeAllStream} from './lib/stream.js';14import {mergePromise, getSpawnedPromise} from './lib/promise.js';15import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';16import {logCommand, verboseDefault} from './lib/verbose.js';17
33 unmodified lines
51 };5253 options.env = getEnv(options);5455 const stdioStreams = handleStdioOption(options);5655 if (process.platform === 'win32' && path.basename(file, '.exe') === 'cmd') {56 // #11657 args.unshift('/q');58 }5962 return {file, args, options, parsed, stdioStreams};60 return {file, args, options};61};6263const handleOutput = (options, value, error) => {64 if (typeof value !== 'string' && !Buffer.isBuffer(value)) {
9 unmodified lines
74};7576export function execa(file, args, options) {77 const parsed = handleArguments(file, args, options);78 const stdioStreams = handleStdioOption(parsed.options);79 validateTimeout(parsed.options);8081 const command = joinCommand(file, args);82 const escapedCommand = getEscapedCommand(file, args);83 logCommand(escapedCommand, parsed.options);8484 validateTimeout(parsed.options);85 validateInputOptions(parsed.options, parsed.stdioStreams);8685 let spawned;86 try {87 spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options);88 } catch (error) {
24 unmodified lines
113 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));114 spawned.cancel = spawnedCancel.bind(null, spawned, context);115116 const handlePromise = async () => {119 const [{error, exitCode, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, parsed.stdioStreams, processDone);117 const [{error, exitCode, signal, timedOut}, stdoutResult, stderrResult, allResult] = await getSpawnedResult(spawned, parsed.options, stdioStreams, processDone);118 const stdout = handleOutput(parsed.options, stdoutResult);119 const stderr = handleOutput(parsed.options, stderrResult);120 const all = handleOutput(parsed.options, allResult);121
35 unmodified lines
157 };158159 const handlePromiseOnce = onetime(handlePromise);160163 handleInput(spawned, parsed.options, parsed.stdioStreams);161 pipeStdioOption(spawned, stdioStreams);162163 spawned.all = makeAllStream(spawned, parsed.options);164165 addPipeMethods(spawned);
2 unmodified lines
168}169170export function execaSync(file, args, options) {171 const parsed = handleArguments(file, args, options);172 handleInputOption(parsed.options);173174 const command = joinCommand(file, args);175 const escapedCommand = getEscapedCommand(file, args);176 logCommand(escapedCommand, parsed.options);177178 validateInputOptions(parsed.options, parsed.stdioStreams);179 const inputOption = handleInputSync(parsed.options, parsed.stdioStreams);180178 let result;179 try {183 result = childProcess.spawnSync(parsed.file, parsed.args, {...parsed.options, input: inputOption});180 result = childProcess.spawnSync(parsed.file, parsed.args, parsed.options);181 } catch (error) {182 throw makeError({183 error,184 stdout: '',
126 unmodified lines