简体   繁体   中英

package.json script conditional

I have a node script that checks for some conditions. Lets call it condition.js . I have another script which runs my program, lets call it runner.js . runner.js is referenced by a command in my package.json file under scripts as well condition.js with their own commands. I want the condition.js to run before runner.js . If a certain condition is true (it checks a string for something) in the condition.js when it runs, I want the second script to run after it. If it is not true, I don't want the second script to run. How can I do this in package.json ?

In condition.js you need to exit() the Node process with a non-zero exit code. Then in your package.json file you can check whether to execute the second script based on the exit code (zero or not):

In your condition.js file:

if (true) {  // use your actual condition
  process.exit(128);
}

Then, in your package.json :

{
  "scripts": {
    "special": "node condition.js && node runner.js"
  }
}

Now you can run npm run special from the command line, the runner.js file will only run if condition.js exits with a zero code (which is the default if the node execution ends normally).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM