Skip to content

Commit

Permalink
Custom command for git-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Iwersen committed May 27, 2021
1 parent 6cb8ab2 commit 204b328
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -30,3 +30,4 @@ coverage/
.nyc_output/
.eslintcache
.env
yarn.lock
21 changes: 21 additions & 0 deletions commands/version/README.md
Expand Up @@ -58,6 +58,7 @@ Running `lerna version --conventional-commits` without the above flags will rele
- [`--create-release <type>`](#--create-release-type)
- [`--exact`](#--exact)
- [`--force-publish`](#--force-publish)
- [`--git-tag-command <cmd>`](#--git-tag-command-cmd)
- [`--git-remote <name>`](#--git-remote-name)
- [`--ignore-changes`](#--ignore-changes)
- [`--ignore-scripts`](#--ignore-scripts)
Expand Down Expand Up @@ -246,6 +247,26 @@ When run with this flag, `lerna version` will force publish the specified packag

> This will skip the `lerna changed` check for changed packages and forces a package that didn't have a `git diff` change to be updated.
### `--git-tag-command <cmd>`

Allows users to specify a wrapper command in CD/CI pipelines that have no direct write access.

```sh
lerna version --git-tag-command "git gh-tag %s -m %s"
```

This can also be configured in `lerna.json`.

```json
{
"command": {
"version": {
"gitTagCommand": "git gh-tag %s -m %s"
}
}
}
```

### `--git-remote <name>`

```sh
Expand Down
9 changes: 9 additions & 0 deletions commands/version/__tests__/git-tag.test.js
Expand Up @@ -34,4 +34,13 @@ describe("gitTag", () => {

expect(childProcess.exec).toHaveBeenLastCalledWith("git", ["tag", tag, "-m", tag, "--force"], opts);
});

it("creates an annotated git tag using the wrapper arguments", async () => {
const tag = "v1.2.4";
const opts = { cwd: "default" };

await gitTag(tag, {}, opts, "git-wrapper gh-tag %s -m %s");

expect(mockExec).toHaveBeenLastCalledWith("git-wrapper", ["gh-tag", tag, "-m", tag], opts);
});
});
5 changes: 5 additions & 0 deletions commands/version/command.js
Expand Up @@ -160,6 +160,11 @@ exports.builder = (yargs, composed) => {
requiresArg: true,
defaultDescription: "v",
},
"git-tag-command": {
describe:
"Allows users to specify a wrapper command in CD/CI pipelines that have no direct write access.",
type: "string",
},
y: {
describe: "Skip all confirmation prompts.",
alias: "yes",
Expand Down
4 changes: 2 additions & 2 deletions commands/version/index.js
Expand Up @@ -655,7 +655,7 @@ class VersionCommand extends Command {

return Promise.resolve()
.then(() => gitCommit(message, this.gitOpts, this.execOpts))
.then(() => Promise.all(tags.map((tag) => gitTag(tag, this.gitOpts, this.execOpts))))
.then(() => Promise.all(tags.map((tag) => gitTag(tag, this.gitOpts, this.execOpts, this.options.gitTagCommand))))
.then(() => tags);
}

Expand All @@ -668,7 +668,7 @@ class VersionCommand extends Command {

return Promise.resolve()
.then(() => gitCommit(message, this.gitOpts, this.execOpts))
.then(() => gitTag(tag, this.gitOpts, this.execOpts))
.then(() => gitTag(tag, this.gitOpts, this.execOpts, this.options.gitTagCommand))
.then(() => [tag]);
}

Expand Down
16 changes: 9 additions & 7 deletions commands/version/lib/git-tag.js
Expand Up @@ -10,19 +10,21 @@ module.exports.gitTag = gitTag;
* @param {{ forceGitTag: boolean; signGitTag: boolean; }} gitOpts
* @param {import("@lerna/child-process").ExecOpts} opts
*/
function gitTag(tag, { forceGitTag, signGitTag }, opts) {
log.silly("gitTag", tag);
function gitTag(tag, { forceGitTag, signGitTag }, opts, command = "git tag %s -m %s") {
log.silly("gitTag", tag, command);

const args = ["tag", tag, "-m", tag];
const [cmd, ...args] = command.split(" ");

const interpolatedArgs = args.map(arg => arg.replace(/%s/, tag));

if (forceGitTag) {
args.push("--force");
interpolatedArgs.push("--force");
}

if (signGitTag) {
args.push("--sign");
interpolatedArgs.push("--sign");
}

log.verbose("git", args);
return childProcess.exec("git", args, opts);
log.verbose(cmd, interpolatedArgs);
return childProcess.exec(cmd, interpolatedArgs, opts);
}

0 comments on commit 204b328

Please sign in to comment.