Skip to content
Myr

Allow `execa` return values in `$` template expressions (#528)

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

authored by Aaron Casanova · committed by GitHub · parent a6d1141 · Browse files · 5 files+191 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

5 files+191 added−8 removed

  • tests:+104 added−5 removed
  • other:+87 added−3 removed

5 files+191 added−8 removed

modified:lib/command.js+38 added-2 removed
modified:test/command.js+104 added-5 removed
1import {inspect} from 'node:util';2import test from 'ava';3import {execa, execaSync, execaCommand, execaCommandSync, $} from '../index.js';4import {setFixtureDir} from './helpers/fixtures-dir.js';5
95 unmodified lines
101 const {stdout} = await $`node test/fixtures/echo.js foo ${'bar'}`;102 t.is(stdout, 'foo\nbar');103});104105test('$ allows number interpolation', async t => {106 const {stdout} = await $`node test/fixtures/echo.js 1 ${2}`;107 t.is(stdout, '1\n2');108});109110test('$ allows array interpolation', async t => {111 const {stdout} = await $`node test/fixtures/echo.js ${['foo', 'bar']}`;112 t.is(stdout, 'foo\nbar');113});114115test('$ allows execa return value interpolation', async t => {116 const foo = await $`node test/fixtures/echo.js foo`;117 const {stdout} = await $`node test/fixtures/echo.js ${foo} bar`;118 t.is(stdout, 'foo\nbar');119});120121test('$ allows execa return value array interpolation', async t => {122 const foo = await $`node test/fixtures/echo.js foo`;123 const {stdout} = await $`node test/fixtures/echo.js ${[foo, 'bar']}`;124 t.is(stdout, 'foo\nbar');125});126127test('$ allows execa return value buffer interpolation', async t => {128 const foo = await $({encoding: null})`node test/fixtures/echo.js foo`;129 const {stdout} = await $`node test/fixtures/echo.js ${foo} bar`;130 t.is(stdout, 'foo\nbar');131});132133test('$ allows execa return value buffer array interpolation', async t => {134 const foo = await $({encoding: null})`node test/fixtures/echo.js foo`;135 const {stdout} = await $`node test/fixtures/echo.js ${[foo, 'bar']}`;136 t.is(stdout, 'foo\nbar');137});138139test('$ ignores consecutive spaces', async t => {140 const {stdout} = await $`node test/fixtures/echo.js foo bar`;141 t.is(stdout, 'foo\nbar');142});
27 unmodified lines
170 const {stdout} = await $`node test/fixtures/echo.js \u`;171 t.is(stdout, '\\u');172});173144test('$ coerces all interpolated expressions to strings', async t => {145 const {stdout} = await $`node test/fixtures/echo.js ${0} ${undefined} ${null}`;146 t.is(stdout, '0\nundefined\nnull');147});148174test('$ allows escaping spaces in commands with interpolation', async t => {175 const {stdout} = await $`${'command with space.js'} foo bar`;176 t.is(stdout, 'foo\nbar');177});
16 unmodified lines
194test('$.sync accepts options', t => {195 const {stdout} = $({stripFinalNewline: true}).sync`noop.js foo`;196 t.is(stdout, 'foo');197});198199test('$.sync allows execa return value interpolation', t => {200 const foo = $.sync`node test/fixtures/echo.js foo`;201 const {stdout} = $.sync`node test/fixtures/echo.js ${foo} bar`;202 t.is(stdout, 'foo\nbar');203});204205test('$.sync allows execa return value array interpolation', t => {206 const foo = $.sync`node test/fixtures/echo.js foo`;207 const {stdout} = $.sync`node test/fixtures/echo.js ${[foo, 'bar']}`;208 t.is(stdout, 'foo\nbar');209});210211test('$.sync allows execa return value buffer interpolation', t => {212 const foo = $({encoding: null}).sync`node test/fixtures/echo.js foo`;213 const {stdout} = $.sync`node test/fixtures/echo.js ${foo} bar`;214 t.is(stdout, 'foo\nbar');215});216217test('$.sync allows execa return value buffer array interpolation', t => {218 const foo = $({encoding: null}).sync`node test/fixtures/echo.js foo`;219 const {stdout} = $.sync`node test/fixtures/echo.js ${[foo, 'bar']}`;220 t.is(stdout, 'foo\nbar');221});222223const invalidExpression = test.macro({224 async exec(t, input, expected) {225 await t.throwsAsync(226 async () => $`node test/fixtures/echo.js ${input}`,227 {instanceOf: TypeError, message: expected},228 );229230 t.throws(231 () => $.sync`node test/fixtures/echo.js ${input}`,232 {instanceOf: TypeError, message: expected},233 );234 },235 title(prettyInput, input, expected) {236 return `$ APIs throw on invalid '${prettyInput ?? inspect(input)}' expression with '${expected}'`;237 },238});239240test(invalidExpression, undefined, 'Unexpected "undefined" in template expression');241test(invalidExpression, [undefined], 'Unexpected "undefined" in template expression');242243test(invalidExpression, null, 'Unexpected "object" in template expression');244test(invalidExpression, [null], 'Unexpected "object" in template expression');245246test(invalidExpression, true, 'Unexpected "boolean" in template expression');247test(invalidExpression, [true], 'Unexpected "boolean" in template expression');248249test(invalidExpression, {}, 'Unexpected "object" in template expression');250test(invalidExpression, [{}], 'Unexpected "object" in template expression');251252test(invalidExpression, {foo: 'bar'}, 'Unexpected "object" in template expression');253test(invalidExpression, [{foo: 'bar'}], 'Unexpected "object" in template expression');254255test(invalidExpression, {stdout: undefined}, 'Unexpected "undefined" stdout in template expression');256test(invalidExpression, [{stdout: undefined}], 'Unexpected "undefined" stdout in template expression');257258test(invalidExpression, {stdout: 1}, 'Unexpected "number" stdout in template expression');259test(invalidExpression, [{stdout: 1}], 'Unexpected "number" stdout in template expression');260261test(invalidExpression, Promise.resolve(), 'Unexpected "object" in template expression');262test(invalidExpression, [Promise.resolve()], 'Unexpected "object" in template expression');263264test(invalidExpression, Promise.resolve({stdout: 'foo'}), 'Unexpected "object" in template expression');265test(invalidExpression, [Promise.resolve({stdout: 'foo'})], 'Unexpected "object" in template expression');266267test('$`noop.js`', invalidExpression, $`noop.js`, 'Unexpected "object" in template expression');268test('[ $`noop.js` ]', invalidExpression, [$`noop.js`], 'Unexpected "object" in template expression');269270test('$({stdio: \'inherit\'}).sync`noop.js`', invalidExpression, $({stdio: 'inherit'}).sync`noop.js`, 'Unexpected "undefined" stdout in template expression');271test('[ $({stdio: \'inherit\'}).sync`noop.js` ]', invalidExpression, [$({stdio: 'inherit'}).sync`noop.js`], 'Unexpected "undefined" stdout in template expression');
modified:index.d.ts+30 added-1 removed
539 unmodified lines
540*/541export function execaCommand(command: string, options?: Options): ExecaChildProcess;542export function execaCommand(command: string, options?: Options<null>): ExecaChildProcess<Buffer>;543544type TemplateExpression = string | number | Array<string | number>;544type TemplateExpression =545 | string546 | number547 | ExecaReturnValue<string | Buffer>548 | ExecaSyncReturnValue<string | Buffer>549 | Array<string | number | ExecaReturnValue<string | Buffer> | ExecaSyncReturnValue<string | Buffer>>;550551type Execa$<StdoutStderrType = string> = {552 /**553 Same as `execa()` except both file and arguments are specified in a single tagged template string. For example, `` $`echo unicorns` `` is the same as `execa('echo', ['unicorns'])`.
1 unmodified line
555 It's important to note that quotes, backslashes, and spaces are automatically escaped and have no special meaning unless the `shell` option is used. This escaping behavior also applies to interpolated expressions such as strings (`` $`echo ${'string'}` ``), arrays of strings (`` $`echo ${['array', 'of strings']}` ``), and so on.556557 The `shell` option must be used if the `command` uses shell-specific features (for example, `&&` or `||`), as opposed to being a simple `file` followed by its `arguments`.558559 As a convenience, the result from previous `` $`command` `` or `` $.sync`command` `` calls can be used as template expressions in subsequent commands and `$`/`$.sync` will use the `stdout` value. See the example below `` with results from `$` or `$.sync` `` for more details.560561 @returns A [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess), which is enhanced to also be a `Promise` for a result `Object` with `stdout` and `stderr` properties.562563 @example <caption>Basic</caption>564 ```
37 unmodified lines
602603 $({stdio: 'inherit'}).sync`echo rainbows`;604 //=> 'rainbows'605 ```606607 @example <caption>With results from `$` or `$.sync`</caption>608 ```609 import {$} from 'execa';610611 const unicorns = await $`echo unicorns`;612613 $({stdio: 'inherit'}).sync`echo ${unicorns} rainbows`;614 //=> 'unicorns rainbows'615 ```616 */617 (options: Options<undefined>): Execa$<StdoutStderrType>;618 (options: Options): Execa$;619 (options: Options<null>): Execa$<Buffer>;
31 unmodified lines
651It's important to note that quotes, backslashes, and spaces are automatically escaped and have no special meaning unless the `shell` option is used. This escaping behavior also applies to interpolated expressions such as strings (`` $`echo ${'string'}` ``), arrays of strings (`` $`echo ${['array', 'of strings']}` ``), and so on.652653The `shell` option must be used if the `command` uses shell-specific features (for example, `&&` or `||`), as opposed to being a simple `file` followed by its `arguments`.654655As a convenience, the result from previous `` $`command` `` or `` $.sync`command` `` calls can be used as template expressions in subsequent commands and `$`/`$.sync` will use the `stdout` value. See the example below `` with results from `$` or `$.sync` `` for more details.656657@returns A [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess), which is enhanced to also be a `Promise` for a result `Object` with `stdout` and `stderr` properties.658659@example <caption>Basic</caption>660```
37 unmodified lines
698699$({stdio: 'inherit'}).sync`echo rainbows`;700//=> 'rainbows'701```702703@example <caption>With results from `$` or `$.sync`</caption>704```705import {$} from 'execa';706707const unicorns = await $`echo unicorns`;708709$({stdio: 'inherit'}).sync`echo ${unicorns} rainbows`;710//=> 'unicorns rainbows'711```712*/713export const $: Execa$;714715/**
32 unmodified lines
modified:index.test-d.ts+6 added
263 unmodified lines
264expectType<ExecaReturnValue>(await $`unicorns ${['foo', 'bar']}`);265expectType<ExecaSyncReturnValue>($.sync`unicorns ${['foo', 'bar']}`);266expectType<ExecaReturnValue>(await $`unicorns ${[1, 2]}`);267expectType<ExecaSyncReturnValue>($.sync`unicorns ${[1, 2]}`);268expectType<ExecaReturnValue>(await $`unicorns ${await $`echo foo`}`);269expectError<ExecaReturnValue>(await $`unicorns ${$`echo foo`}`);270expectType<ExecaSyncReturnValue>($.sync`unicorns ${$.sync`echo foo`}`);271expectType<ExecaReturnValue>(await $`unicorns ${[await $`echo foo`, 'bar']}`);272expectError<ExecaReturnValue>(await $`unicorns ${[$`echo foo`, 'bar']}`);273expectType<ExecaSyncReturnValue>($.sync`unicorns ${[$.sync`echo foo`, 'bar']}`);274expectType<ExecaReturnValue>(await $`unicorns ${true.toString()}`);275expectType<ExecaSyncReturnValue>($.sync`unicorns ${false.toString()}`);276expectError<ExecaReturnValue>(await $`unicorns ${true}`);277expectError<ExecaSyncReturnValue>($.sync`unicorns ${false}`);
modified:readme.md+13 added
88 unmodified lines
89$({stdio: 'inherit'}).sync`echo rainbows`;90//=> 'rainbows'91```9293#### With results from `$` or `$.sync`9495```js96import {$} from 'execa';9798const unicorns = await $`echo unicorns`;99100$({stdio: 'inherit'}).sync`echo ${unicorns} rainbows`;101//=> 'unicorns rainbows'102```103104### Pipe the child process stdout to the parent105106```js107import {execa} from 'execa';
152 unmodified lines
260It's important to note that quotes, backslashes, and spaces are automatically escaped and have no special meaning unless the [`shell` option](#shell) is used. This escaping behavior also applies to interpolated expressions such as strings (`` $`echo ${'string'}` ``), arrays of strings (`` $`echo ${['array', 'of strings']}` ``), and so on.261262The [`shell` option](#shell) must be used if the `command` uses shell-specific features (for example, `&&` or `||`), as opposed to being a simple `file` followed by its `arguments`.263264As a convenience, the result from previous [`` $`command` ``](#command) or [`` $.sync`command` ``](#synccommand) calls can be used as template expressions in subsequent commands and `$`/`$.sync` will use the `stdout` value. See the example above [with results from `$` or `$.sync`](#with-results-from--or-sync) for more details.265266Returns a `Promise` that resolves or rejects with a [`childProcessResult`](#childProcessResult).267268### $.sync\`command\`269
481 unmodified lines