execa icon indicating copy to clipboard operation
execa copied to clipboard

Allow newlines inside `$` command

Open ehmicky opened this issue 1 year ago • 4 comments

Allowing newlines inside $ might be nice for long commands. For example:

await $`command
  --flag-one
  --flag-two
  --flag-three
  ...
  --lots-of-flags`

At the moment, arguments are delimited by spaces. We should use \s instead.

https://github.com/sindresorhus/execa/blob/8ef8e90e13dc2ab8cc69ed360ba3485e464d39f0/lib/command.js#L26

Injections are not possible with $ since dynamic values require using ${...}, in which case \s will not be considered delimiters.

This should apply to execaCommand() too, which uses the same underlying logic. execaCommand() is meant for passing trusted dynamic input as is, for example for a REPL. Allowing newlines for a REPL is useful too. If the input is not trusted, execa() or $ should be used instead.

What do you think @sindresorhus?

ehmicky avatar Jan 23 '24 21:01 ehmicky

I guess we could, but maybe we should keep it open a bit more to see if people can come up with any issues with it. It's not an important feature, but it will be harder to take it away later on.

The only downside I see is that you can no longer copy the command and run it in a real shell for debugging.

sindresorhus avatar Jan 23 '24 21:01 sindresorhus

Good point about copying. One would need to add trailing backslashes, or join all lines into one before copying.

Let's keep this on the back burner and wait. :+1:

ehmicky avatar Jan 24 '24 00:01 ehmicky

I like the idea 👍 I currently use arrays in $ expressions to clean up long commands:

await $`command ${[
  '--flag-one',
  '--flag-two',
  '--flag-three',
  '...',
  '--lots-of-flags',
]}
`

aaronccasanova avatar Jan 30 '24 20:01 aaronccasanova

I just came across this: someone also asked for this feature with zx and it got 6 thumbs ups. That's their 6th most upvoted issue out of 63. There are some good points in favor of it being made. The main cons seem to be zx-specific (shell quoting-related) and does not impact Execa.

ehmicky avatar Feb 08 '24 07:02 ehmicky

@sindresorhus What are your thoughts using the following issue from zx? Especially the fact that many users seem to want that feature and zx is currently missing it.

ehmicky avatar Feb 22 '24 21:02 ehmicky

Alright, lets do it.

sindresorhus avatar Feb 23 '24 03:02 sindresorhus