Chunky Chunk
Chunky Chunk

Reputation: 17237

Concatenate NPM Script Commands?

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.

Upvotes: 18

Views: 12820

Answers (4)

F. Lumley
F. Lumley

Reputation: 765

Similar to TheDarkIn1978's answer but shorter:

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

Upvotes: 3

Morta1
Morta1

Reputation: 619

You can use npm install -g concurrently

Simple usage :

concurrently "command1 arg" "command2 arg"

Upvotes: 2

denixtry
denixtry

Reputation: 3098

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"
  }
}

Upvotes: 3

Nurbol Alpysbayev
Nurbol Alpysbayev

Reputation: 21951

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",
  }
}

Upvotes: 17

Related Questions