Skip to content
Myr

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

7 files+101 added−14 removed

modified:docs/scripts.md+1 added-1 removed
633 unmodified lines
634```635636```js637// Execa638await $({input: fs.createReadStream('file.txt')})`cat`638await $({inputFile: 'file.txt'})`cat`639```640641### Silent stderr642
14 unmodified lines
modified:lib/stream.js+40 added-8 removed
modified:test/stream.js+26 added
69 unmodified lines
70 const {stdout} = await execa('stdin.js', {input: stream});71 t.is(stdout, 'howdy');72});7374test('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});8081test('inputFile and input cannot be both set', t => {82 t.throws(() => execa('stdin.js', {inputFile: '', input: ''}), {83 message: /cannot be both set/,84 });85});8687test('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 lines
117 {message: /The `input` option cannot be a stream in sync mode/},118 );119});120121test('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});127128test('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});133134test('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 lines
modified:index.d.ts+18 added
256 unmodified lines
257258export type Options<EncodingType = string> = {259 /**260 Write some input to the `stdin` of your binary.261262 If the input is a file, use the `inputFile` option instead.263 */264 readonly input?: string | Buffer | ReadableStream;265266 /**267 Use a file as input to the the `stdin` of your binary.268269 If the input is not a file, use the `input` option instead.270 */271 readonly inputFile?: string;272} & CommonOptions<EncodingType>;273274export type SyncOptions<EncodingType = string> = {275 /**276 Write some input to the `stdin` of your binary.277278 If the input is a file, use the `inputFile` option instead.279 */280 readonly input?: string | Buffer;281282 /**283 Use a file as input to the the `stdin` of your binary.284285 If the input is not a file, use the `input` option instead.286 */287 readonly inputFile?: string;288} & CommonOptions<EncodingType>;289290export type NodeOptions<EncodingType = string> = {291 /**
530 unmodified lines
modified:index.js+4 added-4 removed
8 unmodified lines
9import {makeError} from './lib/error.js';10import {normalizeStdio, normalizeStdioNode} from './lib/stdio.js';11import {spawnedKill, spawnedCancel, setupTimeout, validateTimeout, setExitHandler} from './lib/kill.js';12import {addPipeMethods} from './lib/pipe.js';13import {handleInput, getSpawnedResult, makeAllStream, validateInputSync} from './lib/stream.js';13import {handleInput, getSpawnedResult, makeAllStream, handleInputSync} 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
140 unmodified lines
158 };159160 const handlePromiseOnce = onetime(handlePromise);161162 handleInput(spawned, parsed.options.input);162 handleInput(spawned, parsed.options);163164 spawned.all = makeAllStream(spawned, parsed.options);165166 addPipeMethods(spawned);
6 unmodified lines
173 const command = joinCommand(file, args);174 const escapedCommand = getEscapedCommand(file, args);175 logCommand(escapedCommand, parsed.options);176177 validateInputSync(parsed.options);177 const input = handleInputSync(parsed.options);178179 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 lines
modified:index.test-d.ts+1 added
132 unmodified lines
133execa('unicorns', {buffer: false});134execa('unicorns', {input: ''});135execa('unicorns', {input: Buffer.from('')});136execa('unicorns', {input: process.stdin});137execa('unicorns', {inputFile: ''});138execa('unicorns', {stdin: 'pipe'});139execa('unicorns', {stdin: 'overlapped'});140execa('unicorns', {stdin: 'ipc'});141execa('unicorns', {stdin: 'ignore'});
175 unmodified lines
modified:readme.md+11 added-1 removed
126 unmodified lines
127```js128import {execa} from 'execa';129130// Similar to `cat < stdin.txt` in Bash131const {stdout} = await execa('cat', {input:fs.createReadStream('stdin.txt')});131const {stdout} = await execa('cat', {inputFile:'stdin.txt'});132console.log(stdout);133//=> 'unicorns'134```135
360 unmodified lines
496497Write some input to the `stdin` of your binary.\498Streams are not allowed when using the synchronous methods.499500If the input is a file, use the [`inputFile` option](#inputfile) instead.501502#### inputFile503504Type: `string`505506Use a file as input to the the `stdin` of your binary.507508If the input is not a file, use the [`input` option](#input) instead.509510#### stdin511512Type: `string | number | Stream | undefined`\513Default: `pipe`
262 unmodified lines