15

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.

8
  • Does this answer your question? Sending command line arguments to npm script Commented Apr 18, 2021 at 16:34
  • Nope, it does not (or at least I can't find how it suggests to ask users for inputting params). Commented Apr 22, 2021 at 7:13
  • Your can call a bash script from your npm script that reads in a parameter from the user. There's lots of examples of interactive bash scripts out there. Commented Apr 22, 2021 at 7:41
  • However in node land I'd expect to have to run this as URL=localhost npm run start Commented Apr 22, 2021 at 7:42
  • 2
    What is the contents of the "scripts" property of your package.json file? Commented Apr 22, 2021 at 19:17

5 Answers 5

13

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

Sign up to request clarification or add additional context in comments.

5 Comments

As far as I understand the code doesn't ASK users to input some values. Am I missing smth?
I see. So, you want a prompt rather than passing the location as an argument, right?
The answer has been updated to include the "ASK" functionality.
It looks like your solution requires some bash scripts (does the "read location" command runs some sort of a bash script?). I would love to avoid it and use vanila npm (if possible).
Nope, it is not a bash script. It is a bash syntax to accept the input from the stdin. But, I believe your question was regarding the concern with windows user. In that case, I recommend running all the npm scripts in a bash environment accessible using git-bash.
11
+100

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();
});

Comments

5

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

Comments

0

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: `);
})();

Comments

0

I think the above answers are good, but if you just need a little bit of data from the user they seem like overkill. For simple user input you might try the following and avoid additional files or third party libraries.

{
    "step1": "#!/usr/bin/env bash \n read -p \"What is your name? \" name \n npm run step2 $name",
    "step2": "echo Hello, $1",
}

Here is an example of how you might use it to start a http server on a user specified port:

{
    "http1": "#!/usr/bin/env bash \n read -p \"What port do you want to use? \" port \n npm run http2 $port",
    "http2": "http-server . -p $1",
}

For a slightly more complex example you might use npm-run-all. For example this will have the user type a specific string before continuing.

{
    "reset": "run-s --print-label reset:*",
    "reset:confirm": "#!/usr/bin/env bash \n set -e \n read -p \"Are you sure you want to reset everything? Type YES to continue : \" confirm \n if \n [ \"$confirm\" != \"YES\" ] ; then \n exit 1 \n fi",
    "reset:db": "..."
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.