From cce35f3c460ac8f64bcd1fa952a7975c0b448b26 Mon Sep 17 00:00:00 2001 From: Juan Carlos Blanco Delgado <36451129+juancarlosjr97@users.noreply.github.com> Date: Fri, 26 Apr 2024 06:56:58 +0100 Subject: [PATCH] Exit without error message when requireCommits is true but requireCommitsFail is false and no commits are available (#1101) * feat: exit without error message when requireCommits is true but requireCommitsFail is false and no commits are available Fixes #1095 * refactor: pass state using error object cause Fixes #1095 --- lib/index.js | 7 ++++++- lib/util.js | 1 + test/git.init.js | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9321875b..71921b6c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -138,7 +138,12 @@ const runTasks = async (opts, di) => { }; } catch (err) { const { log } = container; - log ? log.error(err.message || err) : console.error(err); // eslint-disable-line no-console + + const errorMessage = err.message || err; + const logger = log || console; + + err.cause === 'INFO' ? logger.info(errorMessage) : logger.error(errorMessage); + throw err; } }; diff --git a/lib/util.js b/lib/util.js index 11d33413..fed55d8f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -92,6 +92,7 @@ const parseVersion = raw => { const e = (message, docs, fail = true) => { const error = new Error(docs ? `${message}${EOL}Documentation: ${docs}${EOL}` : message); error.code = fail ? 1 : 0; + error.cause = fail ? 'ERROR' : 'INFO'; return error; }; diff --git a/test/git.init.js b/test/git.init.js index 8e598a93..0f8f892c 100644 --- a/test/git.init.js +++ b/test/git.init.js @@ -76,7 +76,7 @@ test.serial('should not throw if there are commits', async t => { const gitClient = factory(Git, { options }); sh.exec('git tag 1.0.0'); gitAdd('line', 'file', 'Add file'); - await t.notThrowsAsync(gitClient.init()); + await t.notThrowsAsync(gitClient.init(), 'There are no commits since the latest tag'); }); test.serial('should fail (exit code 1) if there are no commits', async t => {