18

I understand that it's possible to chain NPM scripts with &&, pre- and post- hooks, but is it possible to simply separate lengthy script lines into a single, concatenated line?

For example, I'd like to convert this:

"script": {
  "build": "--many --commands --are --required --on --a --single --line"
}

into this:

"script": {
  "part1": "--many --commands",
  "part2": "--are --required",
  "part3": "--on --a",
  "part4": "--single --line",
  "build": "part1 + part2 + part3 + part4"
}

So when I enter npm run build it will merge all the parts of the command on one line.

I'm also familiar with config variables, but they are not a cross platform solution so I will avoid using them.

2
  • 3
    I think the recommended way would be to move it into an external file. A single line Bash file and Batch (or PowerShell) file would be one way, though you would need to "maintain" the two files separately. Commented May 9, 2017 at 18:16
  • Yeah, I was thinking it wasn't really possible. Too bad, but I decided to just setup some Gulp tasks with Node's child_pocess.exec Commented May 10, 2017 at 0:05

4 Answers 4

17

A common approach to do it is like:

{
  "scripts:": {
    "script1": "cmd1",
    "script2": "cmd2",
    "script3": "cmd3",
    "build": "npm run script1 && npm run script2 && npm run script3",
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Along with this strategy, one can also use the npm-run-all package for a little cleaner syntax.
I don't know why this answer has so many upvotes but its completely wrong. The question is asking for concatenating not running multiple commands. imagine I want to run "echo hello world" using 3 scripts with echo, hello and world. npm run echo && npm run hello && npm run world is clearly incorrect
3

You can use the config parameter in your package.json like so, Although it's longer!

You could reuse your config at least with this method.

There is probably a way to concat multiple environmental variables based on a regex.

{

  "config": {
    "part1": "--many --commands",
    "part2": "--are --required",
    "part3": "--on --a",
    "part4": "--single --line",
  },
  "script": {
    "build": "cmd $npm_package_config_part1 $npm_package_config_part2 $npm_package_config_part3 $npm_package_config_part4"
  }
}

3 Comments

The only working solution to this question
This is brilliant. Can't believe i didn't know this.
This does not work for yarn only for npm.
3

Similar to TheDarkIn1978's answer but shorter:

"script": {
  "build:js": "--many --commands",
  "build:css": "--are --required",
  "build:images": "--on --a",
  "build": "run-s build:*"
}

1 Comment

Note, this requires npm-run-all package: github.com/mysticatea/npm-run-all
2

You can use npm install -g concurrently

Simple usage :

concurrently "command1 arg" "command2 arg"

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.