11

Ok, I have a project where I use npm. I need it to compile several .coffee scripts.

I need to execute multiple prestart scripts, like:

  "scripts": {
     "prestart": "coffee -c ./file1.coffee",
     "prestart": "coffee -c ./file2.coffee",
     "prestart": "coffee -c ./file3.coffee",
     "prestart": "coffee -c ./file4.coffee",
    "start": "node ./file1.js"
  },

But it only seems to execute the last one, and it doesn't let me append many scripts on one like:

"prestart": "coffee -c ./file1.coffee; coffee -c./file2.coffee"

What can I do?

dquijada
  • 1,697
  • 3
  • 14
  • 19

2 Answers2

13

wrap them in a parent script

"scripts": {
  "prestart": "sh ./make-coffee.sh",
  "start": "node ./file1.js"
},

//make-coffee.sh
#!/bin/bash
coffee -c ./file1.coffee
coffee -c ./file2.coffee
coffee -c ./file3.coffee
coffee -c ./file4.coffee

or another (unix-only) solution is to run multiple commands. I don't know what happens if the early commands fail.

"scripts": {
  "prestart": "coffee -c ./file1.coffee; coffee -c ./file2.coffee; coffee -c ./file3.coffee; coffee -c ./file4.coffee",
  "start": "node ./file1.js"
},
Plato
  • 10,812
  • 2
  • 41
  • 61
  • separing them with ; didn't work, it looked for the file "file1.coffee;" – dquijada Apr 13 '15 at 15:46
  • 2
    BTW If anyone else knows a good cross-platform solution to this I am interested! – Plato Apr 13 '15 at 15:56
  • 1
    try this: "prestart": "coffee -c ./file1.coffee ./file2.coffee ./file3.coffee ./file4.coffee", – dquijada Apr 13 '15 at 18:19
  • 1
    thx @Swiollvfer I am actually looking for any generic solution to run an arbitrary number of commands in a single cross platform npm script... this specific scenario works because you can pass multiple files to the coffee binary – Plato Apr 13 '15 at 18:20
  • 5
    @Plato Contrary to `;` as command separator `&&` is supported in most shells (including Windows), it also fails fast and doesn't try to continue running your script a command in the middle blew up. – TWiStErRob Mar 03 '16 at 14:53
6

you can use && like this

"scripts": {
  "prestart": "coffee -c ./file1.coffee && coffee -c ./file2.coffee && coffee -c ./file3.coffee && coffee -c ./file4.coffee",
  "start": "node ./file1.js"
},
Mehran Motiee
  • 3,547
  • 1
  • 13
  • 16