Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow template strings with all methods #933

Merged
merged 1 commit into from Mar 27, 2024
Merged

Conversation

ehmicky
Copy link
Collaborator

@ehmicky ehmicky commented Mar 27, 2024

Fixes #891.

Template strings with all methods

The template string syntax can now be used with all methods.

import {execa} from 'execa';

await execa`echo foobar`;

Non-template strings with $

Also, $ can now use the non-template string syntax.

import {$} from 'execa';

await $('echo', ['foobar']);

Default options

Therefore, the only difference between execa, $ and execaNode are now their default options:

  • execa: normal default options, general use case.
  • $: {stdin: 'inherit', preferLocal: true} default options, intended for scripts.
  • execaNode: {node: true} default options, intended to run Node files.

execaCommand()

execaCommand() is slightly different from the other methods, and is only intended for cases like a REPL. In other cases, the other methods are better.

import {$, execa, execaCommand} from 'execa'; 

const runRepl = async command => {
  await execa(command);
  await $`${command}`;
  await execa`${command}`; // Those three commands do not work. This is parsed as 'echo foo bar' file, with no arguments
  await execaCommand(command); // This works.
};

await runRepl('echo foo bar');

Options binding

The template string syntax uses the following convention to set options:

await $(options)`command`;

Therefore, it requires adding $(options) binding. So this PR also enables options binding with the same syntax as $. This is quite nice as options binding with Execa was somewhat tricky. For example, to set global options:

import {execa as execa_} from 'execa';

const execa = execa_(options);

Which allows doing:

await execa(options)`command`;

@sindresorhus sindresorhus merged commit 8074d85 into main Mar 27, 2024
16 checks passed
@sindresorhus sindresorhus deleted the template-string branch March 27, 2024 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow template strings with all methods
2 participants