Last active
December 13, 2017 20:27
-
-
Save wandroll/b30bf579707b55b53f125efc685a79b2 to your computer and use it in GitHub Desktop.
How to pass additional flags to NPM scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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