0

I want to do semi automation to create user for my api, so I have this script

//getUserInfo.js
const argv = require('yargs-parser')(process.argv.slice(2))

module.exports = (async () => {
    let [ userId ] = argv._ //parsing...

    if(!userId){
        console.log('userId is not defined')
        return
    } 

    userId = userId.split('=')[1] ////parsing, again :(

    console.log(userId) //123
    //proceed with automation steps
    ...
    ...
})()

The script is working, so in my package.json I have this

"scripts": {
  "admin:getUserInfo": "node server/scripts/getUserInfo.js"
}

All I need to do is to run npm run admin:getUserInfo userId=123 and I can get 123 in my terminal.

But the problem is I have to do so many step just to get the userId value.

Madeline Ries
  • 599
  • 1
  • 8
  • 18
  • Maybe this might help https://stackoverflow.com/questions/11580961/sending-command-line-arguments-to-npm-script – Suraj Jadhav May 05 '18 at 10:01
  • @SurajJadhav couldn't find the answer there – Madeline Ries May 05 '18 at 10:25
  • 1
    My sincere apologies, I thought you were not able to get `userId` from the passed options. Use `args-parser` instead of `yargs-parser`. [see docs](https://www.npmjs.com/package/args-parser) That would save you a few lines of code. – Suraj Jadhav May 05 '18 at 11:34

0 Answers0