berry icon indicating copy to clipboard operation
berry copied to clipboard

[Feature] Subset of positional args in shared scripts

Open zachbryant opened this issue 2 years ago • 1 comments

  • [x] I'd be willing to implement this feature (contributing guide)
  • [ ] This feature is important to have in this repository; a contrib plugin wouldn't do

Describe the user story

I want to create extensible scripts that act as a foundation for other scripts. Yarn offers positional arguments like $1,$2,$N, and access to the array of all args via $@ but does not offer support for passing part of the arguments on to another script.

Suppose I want a script to use its first value as an argument for one command, then pass the rest onto another command. This might look like:

cross-env NODE_OPTIONS=--max_old_space_size=$0 someOtherBinary ${@:1}

${@:1} being bash syntax for array slicing.

Describe the solution you'd like

A plugin would do, but considering other bash-like syntax is part of the core, I think this would also belong there. Wherever yarn is checking for these positional arguments, we can interpret the array slicing syntax.

Describe the drawbacks of your solution

Array slicing has a stable API, but the parsing may be less trivial than patterns like \$\d+.

Describe alternatives you've considered

The only alternative I've considered at this point is not having the extensibility desired and repeating myself in the scripts.

zachbryant avatar Jan 04 '24 09:01 zachbryant

It'd make sense for that to be implemented using the bash syntax in our shell. That'd involve updating the grammar: https://github.com/yarnpkg/berry/blob/master/packages/yarnpkg-parsers/sources/grammars/shell.pegjs#L163

Then adding support to the interpolation code: https://github.com/yarnpkg/berry/blob/master/packages/yarnpkg-shell/sources/index.ts#L383

And of course some tests: https://github.com/yarnpkg/berry/blob/master/packages/yarnpkg-shell/tests/shell.test.ts

arcanis avatar Jan 04 '24 09:01 arcanis