Skip to content

Instantly share code, notes, and snippets.

@vitalbone
Created December 5, 2018 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitalbone/c58508159b1d98bd5be941649a5857eb to your computer and use it in GitHub Desktop.
Save vitalbone/c58508159b1d98bd5be941649a5857eb to your computer and use it in GitHub Desktop.
Pass args into an NPM script

Passing args into an NPM script

(According to the gospel of Tim Leslie)

Example

$ "start": "/bin/sh -c 'cd projects/${1:-$0} && yarn start' basic"

Usage

$ npm run start basic

Notes

  • ${1:-$0} <-- This says “use $1 as the project directory if given, otherwise use $0”.
  • Adding basic to the end of the command ensure that $0 is always basic and then $1 will always be whatever the user types on the command line. e.g. bolt start login => $1 = login.
  • We can’t use ${0:-basic} to get the defaults, because if the user doesn’t supply an argument, rather than $0 being unset, it gets set to /bin/sh, which doesn’t particularly help.

Some light reading: http://wiki.bash-hackers.org/syntax/pe#parameter_expansion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment