From 00738e9ab2a9f3b5656419205bd7ddb1669e4193 Mon Sep 17 00:00:00 2001 From: Jonathan Zempel Date: Sun, 24 May 2020 15:54:33 -0700 Subject: [PATCH] feat(version): add `--force-git-tag` option (#2594) Co-authored-by: Daniel Stockman <5605+evocateur@users.noreply.github.com> --- commands/version/README.md | 5 +++++ commands/version/__tests__/git-tag.test.js | 9 +++++++++ commands/version/command.js | 4 ++++ commands/version/index.js | 2 ++ commands/version/lib/git-tag.js | 6 +++++- 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/commands/version/README.md b/commands/version/README.md index a9d00d0877..e720488e2d 100644 --- a/commands/version/README.md +++ b/commands/version/README.md @@ -65,6 +65,7 @@ Running `lerna version --conventional-commits` without the above flags will rele - [`--preid`](#--preid) - [`--sign-git-commit`](#--sign-git-commit) - [`--sign-git-tag`](#--sign-git-tag) +- [`--force-git-tag`](#--force-git-tag) - [`--tag-version-prefix`](#--tag-version-prefix) - [`--yes`](#--yes) @@ -372,6 +373,10 @@ This option is analogous to the `npm version` [option](https://docs.npmjs.com/mi This option is analogous to the `npm version` [option](https://docs.npmjs.com/misc/config#sign-git-tag) of the same name. +### `--force-git-tag` + +This option replaces any existing tag instead of failing. + ### `--tag-version-prefix` This option allows to provide custom prefix instead of the default one: `v`. diff --git a/commands/version/__tests__/git-tag.test.js b/commands/version/__tests__/git-tag.test.js index a8bf33143d..4c06cab46d 100644 --- a/commands/version/__tests__/git-tag.test.js +++ b/commands/version/__tests__/git-tag.test.js @@ -25,4 +25,13 @@ describe("gitTag", () => { expect(mockExec).toHaveBeenLastCalledWith("git", ["tag", tag, "-m", tag, "--sign"], opts); }); + + it("forces the tag when configured", async () => { + const tag = "v1.1.1"; + const opts = { cwd: "forced" }; + + await gitTag(tag, { forceGitTag: true }, opts); + + expect(mockExec).toHaveBeenLastCalledWith("git", ["tag", tag, "-m", tag, "--force"], opts); + }); }); diff --git a/commands/version/command.js b/commands/version/command.js index cf288ad72a..f2e8ea0185 100644 --- a/commands/version/command.js +++ b/commands/version/command.js @@ -139,6 +139,10 @@ exports.builder = (yargs, composed) => { describe: "Pass the `--sign` flag to `git tag`.", type: "boolean", }, + "force-git-tag": { + describe: "Pass the `--force` flag to `git tag`.", + type: "boolean", + }, "tag-version-prefix": { describe: "Customize the tag prefix. To remove entirely, pass an empty string.", type: "string", diff --git a/commands/version/index.js b/commands/version/index.js index a517e6ec1b..5707a9b4c4 100644 --- a/commands/version/index.js +++ b/commands/version/index.js @@ -67,6 +67,7 @@ class VersionCommand extends Command { push = true, signGitCommit, signGitTag, + forceGitTag, tagVersionPrefix = "v", } = this.options; @@ -92,6 +93,7 @@ class VersionCommand extends Command { commitHooks, signGitCommit, signGitTag, + forceGitTag, }; // https://docs.npmjs.com/misc/config#save-prefix diff --git a/commands/version/lib/git-tag.js b/commands/version/lib/git-tag.js index 5e404f5485..b08970a8fa 100644 --- a/commands/version/lib/git-tag.js +++ b/commands/version/lib/git-tag.js @@ -5,11 +5,15 @@ const childProcess = require("@lerna/child-process"); module.exports = gitTag; -function gitTag(tag, { signGitTag }, opts) { +function gitTag(tag, { forceGitTag, signGitTag }, opts) { log.silly("gitTag", tag); const args = ["tag", tag, "-m", tag]; + if (forceGitTag) { + args.push("--force"); + } + if (signGitTag) { args.push("--sign"); }