Add `inputFile` option (#542)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 5320fef · Browse files · 7 files·+101 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+101 added−14 removed
- tests:+26 added−0 removed
- other:+75 added−14 removed
Provenance
- Commit
0726126- Tree
7f27072- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
5320fef
7 files changed+101 added−14 removed
modified:docs/scripts.md+1 added-1 removed
633 unmodified lines634 ```635 636 ```js637 // Execa638-await $({input: fs.createReadStream('file.txt')})`cat`638+await $({inputFile: 'file.txt'})`cat`639 ```640 641 ### Silent stderr642 14 unmodified linesmodified:lib/stream.js+40 added-8 removed
1234567891011121314151617181920212223242526272829303132333435363738394056414243444546474867 unmodified lines116117118119828384858687import {createReadStream, readFileSync} from 'node:fs';import {isStream} from 'is-stream';import getStream from 'get-stream';import mergeStream from 'merge-stream';
const validateInputOptions = input => { if (input !== undefined) { throw new TypeError('The `input` and `inputFile` options cannot be both set.'); }};
const getInputSync = ({input, inputFile}) => { if (typeof inputFile !== 'string') { return input; }
validateInputOptions(input); return readFileSync(inputFile);};
// `input` and `inputFile` option in sync modeexport const handleInputSync = options => { const input = getInputSync(options);
if (isStream(input)) { throw new TypeError('The `input` option cannot be a stream in sync mode'); }
return input;};
const getInput = ({input, inputFile}) => { if (typeof inputFile !== 'string') { return input; }
validateInputOptions(input); return createReadStream(inputFile);};
// `input` optionexport const handleInput = (spawned, input) => {// `input` and `inputFile` option in async modeexport const handleInput = (spawned, options) => { const input = getInput(options);
if (input === undefined) { return; }
67 unmodified lines getBufferedData(all, allPromise), ]); }};
export const validateInputSync = ({input}) => { if (isStream(input)) { throw new TypeError('The `input` option cannot be a stream in sync mode'); }};modified:test/stream.js+26 added
69 unmodified lines70 const {stdout} = await execa('stdin.js', {input: stream});71 t.is(stdout, 'howdy');72 });73 74+test('inputFile can be set', async t => {75+ const inputFile = tempfile();76+ fs.writeFileSync(inputFile, 'howdy');77+ const {stdout} = await execa('stdin.js', {inputFile});78+ t.is(stdout, 'howdy');79+});80+81+test('inputFile and input cannot be both set', t => {82+ t.throws(() => execa('stdin.js', {inputFile: '', input: ''}), {83+ message: /cannot be both set/,84+ });85+});86+87 test('you can write to child.stdin', async t => {88 const subprocess = execa('stdin.js');89 subprocess.stdin.end('unicorns');90 const {stdout} = await subprocess;26 unmodified lines117 {message: /The `input` option cannot be a stream in sync mode/},118 );119 });120 121+test('inputFile can be set - sync', t => {122+ const inputFile = tempfile();123+ fs.writeFileSync(inputFile, 'howdy');124+ const {stdout} = execaSync('stdin.js', {inputFile});125+ t.is(stdout, 'howdy');126+});127+128+test('inputFile and input cannot be both set - sync', t => {129+ t.throws(() => execaSync('stdin.js', {inputFile: '', input: ''}), {130+ message: /cannot be both set/,131+ });132+});133+134 test('maxBuffer affects stdout', async t => {135 await t.notThrowsAsync(execa('max-buffer.js', ['stdout', '10'], {maxBuffer: 10}));136 const {stdout, all} = await t.throwsAsync(execa('max-buffer.js', ['stdout', '11'], {maxBuffer: 10, all: true}), {message: /max-buffer.js stdout/});137 t.is(stdout, '.'.repeat(10));95 unmodified linesmodified:index.d.ts+18 added
256 unmodified lines257 258 export type Options<EncodingType = string> = {259 /**260 Write some input to the `stdin` of your binary.261+262+ If the input is a file, use the `inputFile` option instead.263 */264 readonly input?: string | Buffer | ReadableStream;265+266+ /**267+ Use a file as input to the the `stdin` of your binary.268+269+ If the input is not a file, use the `input` option instead.270+ */271+ readonly inputFile?: string;272 } & CommonOptions<EncodingType>;273 274 export type SyncOptions<EncodingType = string> = {275 /**276 Write some input to the `stdin` of your binary.277+278+ If the input is a file, use the `inputFile` option instead.279 */280 readonly input?: string | Buffer;281+282+ /**283+ Use a file as input to the the `stdin` of your binary.284+285+ If the input is not a file, use the `input` option instead.286+ */287+ readonly inputFile?: string;288 } & CommonOptions<EncodingType>;289 290 export type NodeOptions<EncodingType = string> = {291 /**530 unmodified linesmodified:index.js+4 added-4 removed
8 unmodified lines9 import {makeError} from './lib/error.js';10 import {normalizeStdio, normalizeStdioNode} from './lib/stdio.js';11 import {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} from './lib/kill.js';12 import {addPipeMethods} from './lib/pipe.js';13-import {handleInput, getSpawnedResult, makeAllStream, validateInputSync} from './lib/stream.js';13+import {handleInput, getSpawnedResult, makeAllStream, handleInputSync} from './lib/stream.js';14 import {mergePromise, getSpawnedPromise} from './lib/promise.js';15 import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';16 import {logCommand, verboseDefault} from './lib/verbose.js';17 140 unmodified lines158 };159 160 const handlePromiseOnce = onetime(handlePromise);161 162- handleInput(spawned, parsed.options.input);162+ handleInput(spawned, parsed.options);163 164 spawned.all = makeAllStream(spawned, parsed.options);165 166 addPipeMethods(spawned);6 unmodified lines173 const command = joinCommand(file, args);174 const escapedCommand = getEscapedCommand(file, args);175 logCommand(escapedCommand, parsed.options);176 177- validateInputSync(parsed.options);177+ const input = handleInputSync(parsed.options);178 179 let result;180 try {181- result = childProcess.spawnSync(parsed.file, parsed.args, parsed.options);181+ result = childProcess.spawnSync(parsed.file, parsed.args, {...parsed.options, input});182 } catch (error) {183 throw makeError({184 error,185 stdout: '',110 unmodified linesmodified:index.test-d.ts+1 added
132 unmodified lines133 execa('unicorns', {buffer: false});134 execa('unicorns', {input: ''});135 execa('unicorns', {input: Buffer.from('')});136 execa('unicorns', {input: process.stdin});137+execa('unicorns', {inputFile: ''});138 execa('unicorns', {stdin: 'pipe'});139 execa('unicorns', {stdin: 'overlapped'});140 execa('unicorns', {stdin: 'ipc'});141 execa('unicorns', {stdin: 'ignore'});175 unmodified linesmodified:readme.md+11 added-1 removed
126 unmodified lines127 ```js128 import {execa} from 'execa';129 130 // Similar to `cat < stdin.txt` in Bash131-const {stdout} = await execa('cat', {input:fs.createReadStream('stdin.txt')});131+const {stdout} = await execa('cat', {inputFile:'stdin.txt'});132 console.log(stdout);133 //=> 'unicorns'134 ```135 360 unmodified lines496 497 Write some input to the `stdin` of your binary.\498 Streams are not allowed when using the synchronous methods.499 500+If the input is a file, use the [`inputFile` option](#inputfile) instead.501+502+#### inputFile503+504+Type: `string`505+506+Use a file as input to the the `stdin` of your binary.507+508+If the input is not a file, use the [`input` option](#input) instead.509+510 #### stdin511 512 Type: `string | number | Stream | undefined`\513 Default: `pipe`262 unmodified lines