Is it feasible to retrieve a key from yargs when utilizing as an npm script argument?
A user inputs in the OSX terminal:
npm run scaffold --name=blah
which triggers in package.json:
"scaffold" : "node ./scaffold/index.js -- "
This leads to
const yargs = require('yargs').argv
if (yargs) {
console.log(yargs);
console.log(yargs.name);
process.exit(1)
}
...
result:
{ _: [], '$0': 'scaffold/index.js' }
undefined
This method only functions if I embed in package.json
"scaffold" : "node scaffold/index.js --name=blah"
, but I prefer this to be adaptable.
As mentioned, I am using args because it seems to simplify retrieving keys by name (as opposed to an array). Open to suggestions.
What is eluding me?
update 11-07-2017 Related: Sending command line arguments to npm script
However, providing the command line 1: npm run scaffold name=hello
OR 2: npm run scaffold --name=hello
results in:
1: { _: [], '$0': 'scaffold/index.js' }
2: { _: [ 'name=hello' ], '$0': 'scaffold/index.js' }
Still unable to access the yargs.name
property. Remains undefined.
Update 13-07-2017
For now, I have abandoned the effort. It feels impossible. I execute the script manually in the terminal. E.g.
node ./scaffold/index.js --name=blah
The image below displays running of a node script directly instead of executing via npm scripts. I have included https://www.npmjs.com/package/nopt node module to test if it aids (it does not). process.argv.name
remains undefined when run through npm scripts.
https://i.stack.imgur.com/a3QQv.gif
Update 18-07-2017
Added github example: https://github.com/sidouglas/stackoverflow-node-arguments
Update 24-07-2017
Prior declaration of variables before initiating the command proves effective
myvar="hello npm run scaffold
rather than
npm run scaffold myvar="hello world"