Fix calling script `.pipe` multiple times (#846)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent b965682 · Browse files · 4 files·+66 added−8 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+66 added−8 removed
- tests:+49 added−0 removed
- other:+17 added−8 removed
Provenance
- Commit
bd547e4- Tree
996c07e- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
b965682
4 files changed+66 added−8 removed
modified:lib/script.js+12 added-6 removed
13 unmodified lines141516171819181920212214 unmodified lines373839404142434445464742484950515253485455565752585960616263646560666768697084 unmodified lines13 unmodified lines }
const [file, ...args] = parseTemplates(templatesOrOptions, expressions); const childProcess = execa(file, args, normalizeScriptOptions(options)); childProcess.pipe = scriptPipe.bind(undefined, childProcess.pipe.bind(childProcess), {}); return childProcess; return scriptWithPipe(childProcess); }
$.sync = (templates, ...expressions) => { if (isPlainObject(templates)) {14 unmodified lines};
export const $ = create$();
const scriptWithPipe = returnValue => { const originalPipe = returnValue.pipe.bind(returnValue); const pipeMethod = (...args) => scriptWithPipe(originalPipe(...args)); returnValue.pipe = scriptPipe.bind(undefined, pipeMethod, {}); return returnValue;};
const scriptPipe = (originalPipe, options, firstArgument, ...args) => {const scriptPipe = (pipeMethod, options, firstArgument, ...args) => { if (isExecaChildProcess(firstArgument)) { if (Object.keys(options).length > 0) { throw new TypeError('Please use .pipe(options)`command` or .pipe($(options)`command`) instead of .pipe(options)($`command`).'); }
return originalPipe(firstArgument, ...args); return pipeMethod(firstArgument, ...args); }
if (isPlainObject(firstArgument)) { return scriptPipe.bind(undefined, originalPipe, {...options, ...firstArgument}); return scriptPipe.bind(undefined, pipeMethod, {...options, ...firstArgument}); }
if (!Array.isArray(firstArgument)) { throw new TypeError('The first argument must be a template string, an options object, or an Execa child process.'); }
const childProcess = create$({...options, stdin: 'pipe'})(firstArgument, ...args); return originalPipe(childProcess, options); return pipeMethod(childProcess, options);};
const parseTemplates = (templates, expressions) => { let tokens = [];84 unmodified linesmodified:test/script.js+49 added
270 unmodified lines271 const {stdout} = await $`noop.js ${foobarString}`.pipe($({stdin: 'pipe'})`stdin.js`);272 t.is(stdout, foobarString);273 });274 275+test('$.pipe.pipe(childProcess)', async t => {276+ const {stdout} = await $`noop.js ${foobarString}`277+ .pipe($({stdin: 'pipe'})`stdin.js`)278+ .pipe($({stdin: 'pipe'})`stdin.js`);279+ t.is(stdout, foobarString);280+});281+282 test('$.pipe`command`', async t => {283 const {stdout} = await $`noop.js ${foobarString}`.pipe`stdin.js`;284 t.is(stdout, foobarString);285 });286 287+test('$.pipe.pipe`command`', async t => {288+ const {stdout} = await $`noop.js ${foobarString}`289+ .pipe`stdin.js`290+ .pipe`stdin.js`;291+ t.is(stdout, foobarString);292+});293+294 test('$.pipe(childProcess, pipeOptions)', async t => {295 const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'});296 t.is(stdout, foobarString);297 });298 299+test('$.pipe.pipe(childProcess, pipeOptions)', async t => {300+ const {stdout} = await $`noop-fd.js 2 ${foobarString}`301+ .pipe($({stdin: 'pipe'})`noop-stdin-fd.js 2`, {from: 'stderr'})302+ .pipe($({stdin: 'pipe'})`stdin.js`, {from: 'stderr'});303+ t.is(stdout, foobarString);304+});305+306 test('$.pipe(pipeOptions)`command`', async t => {307 const {stdout} = await $`noop-fd.js 2 ${foobarString}`.pipe({from: 'stderr'})`stdin.js`;308 t.is(stdout, foobarString);309 });310 311+test('$.pipe.pipe(pipeOptions)`command`', async t => {312+ const {stdout} = await $`noop-fd.js 2 ${foobarString}`313+ .pipe({from: 'stderr'})`noop-stdin-fd.js 2`314+ .pipe({from: 'stderr'})`stdin.js`;315+ t.is(stdout, foobarString);316+});317+318 test('$.pipe(options)`command`', async t => {319 const {stdout} = await $`noop.js ${foobarString}`.pipe({stripFinalNewline: false})`stdin.js`;320 t.is(stdout, `${foobarString}\n`);321 });322 323+test('$.pipe.pipe(options)`command`', async t => {324+ const {stdout} = await $`noop.js ${foobarString}`325+ .pipe({})`stdin.js`326+ .pipe({stripFinalNewline: false})`stdin.js`;327+ t.is(stdout, `${foobarString}\n`);328+});329+330 test('$.pipe(pipeAndProcessOptions)`command`', async t => {331 const {stdout} = await $`noop-fd.js 2 ${foobarString}\n`.pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`;332 t.is(stdout, `${foobarString}\n`);333 });334 335+test('$.pipe.pipe(pipeAndProcessOptions)`command`', async t => {336+ const {stdout} = await $`noop-fd.js 2 ${foobarString}\n`337+ .pipe({from: 'stderr'})`noop-stdin-fd.js 2`338+ .pipe({from: 'stderr', stripFinalNewline: false})`stdin.js`;339+ t.is(stdout, `${foobarString}\n`);340+});341+342 test('$.pipe(options)(secondOptions)`command`', async t => {343 const {stdout} = await $`noop.js ${foobarString}`.pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`;344 t.is(stdout, foobarString);345 });346 347+test('$.pipe.pipe(options)(secondOptions)`command`', async t => {348+ const {stdout} = await $`noop.js ${foobarString}`349+ .pipe({})({})`stdin.js`350+ .pipe({stripFinalNewline: false})({stripFinalNewline: true})`stdin.js`;351+ t.is(stdout, foobarString);352+});353+354 test('$.pipe(options)(childProcess) fails', t => {355 t.throws(() => {356 $`empty.js`.pipe({stdout: 'pipe'})($`empty.js`);357 }, {message: /Please use \.pipe/});10 unmodified linesmodified:index.d.ts+3 added-2 removed
854 unmodified lines855 */856 pipe<Destination extends ExecaChildProcess>(destination: Destination, options?: PipeOptions): Promise<Awaited<Destination>> & PipableProcess;857 };858 859-type ScriptPipableProcess = PipableProcess & {859+type ScriptPipableProcess = {860 /**861 [Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the child process' `stdout` to a second Execa child process' `stdin`. This resolves with that second process' result. If either process is rejected, this is rejected with that process' error instead.862 863 This can be called multiple times to chain a series of processes.7 unmodified lines871 // To pass either child process options or pipe options872 await $`command`.pipe(options)`secondCommand`;873 ```874 */875+ pipe<Destination extends ExecaChildProcess>(destination: Destination, options?: PipeOptions): Promise<Awaited<Destination>> & ScriptPipableProcess;876 pipe(templates: TemplateStringsArray, ...expressions: TemplateExpression[]): Promise<ExecaReturnValue<{}>> & ScriptPipableProcess;877 pipe<OptionsType extends Options & PipeOptions = {}>(options: OptionsType):878 (templates: TemplateStringsArray, ...expressions: TemplateExpression[])879 => Promise<ExecaReturnValue<OptionsType>> & ScriptPipableProcess;302 unmodified lines1182 <NewOptionsType extends CommonOptions = {}>1183 (options: NewOptionsType): Execa$<OptionsType & NewOptionsType>;1184 1185 (templates: TemplateStringsArray, ...expressions: TemplateExpression[]):1185- ExecaChildProcess<StricterOptions<OptionsType, Options>> & ScriptPipableProcess;1186+ Omit<ExecaChildProcess<StricterOptions<OptionsType, Options>>, 'pipe'> & ScriptPipableProcess;1187 1188 /**1189 Same as $\`command\` but synchronous.1190 189 unmodified linesmodified:index.test-d.ts+2 added
95 unmodified lines96 expectType <ExecaReturnValue<{}>>(await scriptPromise.pipe`stdin`.pipe`stdin`);97 await execaPromise.pipe(execaPromise).pipe(execaBufferPromise, {from: 'stdout'});98 await scriptPromise.pipe(execaPromise).pipe(execaBufferPromise, {from: 'stdout'});99 await scriptPromise.pipe`stdin`.pipe({from: 'stdout'})`stdin`;100+ await scriptPromise.pipe`stdin`.pipe(execaBufferPromise, {from: 'stdout'});101+ await scriptPromise.pipe(execaBufferPromise, {from: 'stdout'}).pipe`stdin`;102 expectError(execaPromise.pipe(execaBufferPromise).stdout);103 expectError(scriptPromise.pipe(execaBufferPromise).stdout);104 expectError(scriptPromise.pipe`stdin`.stdout);105 expectError(execaPromise.pipe(createWriteStream('output.txt')));1360 unmodified lines