From 1054593024d6f7e50ad47feb3b61d52c9506c274 Mon Sep 17 00:00:00 2001 From: Evan Tahler Date: Mon, 16 Mar 2020 11:47:45 -0700 Subject: [PATCH] replace `process.exit()` with throwing an error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using process.exit() does not allow us to use this tool programmatically, as we cannot then handle the error in a custom way (ie: formatting the response for CI, logging it, somewhere, etc). This PR replaces logging to `console.error` + using `process.exit` with throwing an error. This will have the same effect in CLI usage by immediately terminating the process with a message, but can also be `try/catch` handled in a parent program. --- lib/index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/index.js b/lib/index.js index 2eca8e8..07fda7c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -437,8 +437,7 @@ exports.init = function(options, callback) { Object.keys(restricted).forEach(function(item) { if (toCheckforFailOn.length > 0) { if (toCheckforFailOn.indexOf(restricted[item].licenses) > -1) { - console.error('Found license defined by the --failOn flag: "' + restricted[item].licenses + '". Exiting.'); - process.exit(1); + throw new Error('Found license defined by the --failOn flag: "' + restricted[item].licenses + '". Exiting.'); } } if (toCheckforOnlyAllow.length > 0) { @@ -451,8 +450,7 @@ exports.init = function(options, callback) { } }); if (!good) { - console.error('Package "' + item + '" is licensed under "' + restricted[item].licenses + '" which is not permitted by the --onlyAllow flag. Exiting.'); - process.exit(1); + throw new Error('Package "' + item + '" is licensed under "' + restricted[item].licenses + '" which is not permitted by the --onlyAllow flag. Exiting.'); } } });