Ask users to input value for npm script

Viewed 2165

I have an npm script, which is run like that:

npm run start:local -- -target.location https://192.1.1.1:8052/

The URL param is the local IP of a user.

What I would love to have is to ask users to input this value, because it's different for everybody.

Is it possible? Would be great to do it with vanila npm scripts.

5 Answers

Simply speaking, an npm script will run the desired command in your shell environment.

In a shell script, the arguments passed can be accessed using $N where N = Position of the argument.

Talking about your case, the command you want to run is npm run start:local -- -target.location USER_INPUT USER_INPUT needs to replaced with the argument that the user has passed. Assuming that the user will pass location as the first argument to the script, it can be accessed using $1.

I have created this gist to demonstrate the same.

enter image description here

As you can clearly see, I have defined start:local to access the first argument and then, pass it to the start script which then echoes out the passed in argument.

enter image description here

UPDATE: Here is the script for ASKING a value from a user in a prompt format. enter image description here

Basically, first I am asking for user input then, storing the same in a variable and passing the variable as an argument to npm start

enter image description here

References

Use readline to get the ip value then use exec to spawn this process. This is a pure JS solution, and OS agnostic.

Example:

package.json
"scripts": {
    "start": "npm run start:local -- -target.location",
    "prompt": "node prompt.js"
},
prompt.js
const { spawn, execSync } = require('child_process');
const exec = commands => {
  execSync(commands, { stdio: 'inherit', shell: true });
};
const spawnProcess = commands => {
  spawn(commands, { stdio: 'inherit', shell: true });
};
   const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('What is your current ip? example: https://192.168.1.10:9009 ', (ip) => {
  console.log(`Starting server on: ${ip}`);
  exec(`npm run start -- ${ip}`);
  rl.close();
});

Example: If we want to run below three commands in sequence with userinput

git add .
git commit -m "With git commit message at run time"
git push

Add below command in your package.json file under the scripts

"gitPush": "git add . && echo 'Enter Commit Message' && read message && git commit -m \"$message\" && git push"

enter image description here

And run commands via npm run gitPush

enter image description here

References: ask-users-to-input-value-for-npm-script

Use Node's readline? it has methods for interactive IO.

If you're trying to ask for users to input ther response you could do something like this.

const readline = require("readline");

const reader = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    error: process.stderr
});

const ask = (message, default_value = null) => new Promise(resolve => {
    reader.question(message, (response)=>{
        return resolve(response.length >= 1 ? response : default_value);
    });
});

(()=>{
    let ip = await ask(`Please enter your public ip: `);
})();
Related