Runtime Terror
Runtime Terror

Reputation: 6752

Abort npm build script from node.js script

I created a node script which checks if my project contains lock file or not. If it doesn't then I want to abort my npm build. Any idea how to do that?

lock-check.js

const path = require('path');
const fs = require("fs");

const lockFiles = ["package-lock.json", "npm-shrinkwrap.json", "yarn.lock"];

let exists = 0;

function checkIfExists() {
    lockFiles.forEach(
        (lf) => {
            if (fs.existsSync(lf)) {
                exists++;
            }
        });

    return exists > 0;
}

package.json

...

"scripts": {
    "prestart": "node ./lock-check.js" // Abort the task
    "start": "webpack-dev-server --config config/webpack.dev.js --hot --inline"
}

...

Upvotes: 2

Views: 698

Answers (1)

warl0ck
warl0ck

Reputation: 3464

To abort the build process you just have to call process.exit(1),

Here I have used 1 but you can use any non-zero exit code to tell it wasn't a successful build as 0 means successful.

You can read more on official nodejs docs

Upvotes: 1

Related Questions