Skip to content

Instantly share code, notes, and snippets.

@wandroll
Last active December 13, 2017 20:27
Show Gist options
  • Save wandroll/b30bf579707b55b53f125efc685a79b2 to your computer and use it in GitHub Desktop.
Save wandroll/b30bf579707b55b53f125efc685a79b2 to your computer and use it in GitHub Desktop.
How to pass additional flags to NPM scripts
const DEFAULT_FLAG = '--options='
/**
* Allow to pass additional flag in command line
* @param {String} flag - optionnal flag used in command line
*
* Flags can be passed :
* - either when defining the script in package.json :
* "myscript": "somecommand param --options=opt1"
* - when calling the script adding '--'
* > yarn run myscript -- --options=opt1,opt2
*/
function getOptions(flag = DEFAULT_FLAG) {
return process.argv.slice(2)
.filter(arg => arg.startsWith(flag))
.map(arg => arg.replace(flag, '').split(','))
}
module.exports = {
getOptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment