0

I would like to insert arguments somewhere inside the command. Somehow to interpolate it. For instance:

{
    "scripts": {
        "foo": "git commit -am $message && git push"
    }
}

Then run:

$ npm run foo -- --message "Baz"

Or something like this:

"foo": "git commit -am {0} && git push"

Then run:

$ npm run foo -- "Lorem"

Please note the git command here are just for demo purpose, we have different multiple commands. So Git Aliases are not the solution.

Is there is any way to achieve this behaviour with npm?

tenbits
  • 7,568
  • 5
  • 34
  • 53
  • A solution for your requirement to pass an argument to the middle of an npm script is provided in my answer [here](https://stackoverflow.com/questions/51388921/pass-command-line-args-to-npm-scripts-in-package-json/51401577#answer-51401577). also similarly [here](https://stackoverflow.com/questions/52656882/cross-platform-way-to-pass-environment-variables-as-arguments-to-npm-scripts/52672999#52672999) – RobC Jan 29 '19 at 16:36
  • Also [here](https://stackoverflow.com/questions/51590079/passing-arguments-to-combined-npm-script/51595953#51595953) for another example. – RobC Jan 29 '19 at 16:55

1 Answers1

1

Thanks to @RobC, it turns out that we can specify the shell for npm scripts, so on windows I could just set the git bash for it and use @RobC solution here

$ npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"

In package.json

"foo": "func() { echo \"$1\"; }; func"

Then run:

$ npm start foo "lorem ipsum"
tenbits
  • 7,568
  • 5
  • 34
  • 53
  • Thanks! Interesting.Yes that’s an alternative solution and I’m sure it meets your use case. However it’s not very portable across platforms (unlike a nodejs solution), as it’s reliant on specifying a user config and older versions of Windows having git bash. – RobC Jan 31 '19 at 00:30