From 7076fd3e68d60f26cd1e7f4ea076497ea329e452 Mon Sep 17 00:00:00 2001 From: Bogdan Kolesnyk Date: Wed, 1 Mar 2023 14:10:11 +0100 Subject: [PATCH] feat: make exit 0 possible when commits required but missing (#984) * feat: make exit 0 possible when commits required but missing * fix: removed dummy code --------- Co-authored-by: Lars Kappert --- bin/release-it.js | 2 +- config/release-it.json | 1 + lib/plugin/git/Git.js | 2 +- lib/util.js | 6 +++++- test/git.init.js | 14 ++++++++++++++ 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/bin/release-it.js b/bin/release-it.js index c5af1ff8..3fc40bc2 100755 --- a/bin/release-it.js +++ b/bin/release-it.js @@ -38,5 +38,5 @@ const options = parseCliArguments([].slice.call(process.argv, 2)); updater({ pkg: pkg }).notify(); release(options).then( () => process.exit(0), - () => process.exit(1) + ({ code }) => process.exit(Number.isInteger(code) ? code : 1) ); diff --git a/config/release-it.json b/config/release-it.json index fc20ac43..c365a48b 100644 --- a/config/release-it.json +++ b/config/release-it.json @@ -6,6 +6,7 @@ "requireBranch": false, "requireUpstream": true, "requireCommits": false, + "requireCommitsFail": true, "commitsPath": "", "addUntrackedFiles": false, "commit": true, diff --git a/lib/plugin/git/Git.js b/lib/plugin/git/Git.js index c9cdd4ce..13c8973b 100644 --- a/lib/plugin/git/Git.js +++ b/lib/plugin/git/Git.js @@ -47,7 +47,7 @@ class Git extends GitBase { throw e(`No upstream configured for current branch.${EOL}Please set an upstream branch.`, docs); } if (this.options.requireCommits && (await this.getCommitsSinceLatestTag(this.options.commitsPath)) === 0) { - throw e(`There are no commits since the latest tag.`, docs); + throw e(`There are no commits since the latest tag.`, docs, this.options.requireCommitsFail); } } diff --git a/lib/util.js b/lib/util.js index 7b74d293..11d33413 100644 --- a/lib/util.js +++ b/lib/util.js @@ -89,7 +89,11 @@ const parseVersion = raw => { }; }; -const e = (message, docs) => new Error(docs ? `${message}${EOL}Documentation: ${docs}${EOL}` : message); +const e = (message, docs, fail = true) => { + const error = new Error(docs ? `${message}${EOL}Documentation: ${docs}${EOL}` : message); + error.code = fail ? 1 : 0; + return error; +}; export { getSystemInfo, diff --git a/test/git.init.js b/test/git.init.js index 3c42589d..8f87fe87 100644 --- a/test/git.init.js +++ b/test/git.init.js @@ -72,6 +72,20 @@ test.serial('should not throw if there are commits', async t => { await t.notThrowsAsync(gitClient.init()); }); +test.serial('should fail (exit code 1) if there are no commits', async t => { + const options = { git: { requireCommits: true } }; + const gitClient = factory(Git, { options }); + sh.exec('git tag 1.0.0'); + await t.throwsAsync(gitClient.init(), { code: 1 }); +}); + +test.serial('should not fail (exit code 0) if there are no commits', async t => { + const options = { git: { requireCommits: true, requireCommitsFail: false } }; + const gitClient = factory(Git, { options }); + sh.exec('git tag 1.0.0'); + await t.throwsAsync(gitClient.init(), { code: 0 }); +}); + test.serial('should throw if there are no commits in specified path', async t => { const options = { git: { requireCommits: true, commitsPath: 'dir' } }; const gitClient = factory(Git, { options });