Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: getLastGitTag with pattern #96

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions src/commands/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
generateMarkDown,
BumpVersionOptions,
} from "..";
import { getCommitMessage, getTagBody, getTagMessage } from "../template";
import { npmPublish, renamePackage } from "../package";
import { githubRelease } from "./github";

Expand Down Expand Up @@ -109,21 +110,12 @@ export default async function defaultMain(args: Argv) {
(f) => f && typeof f === "string"
) as string[];
await execa("git", ["add", ...filesToAdd], { cwd });
const msg = config.templates.commitMessage.replaceAll(
"{{newVersion}}",
config.newVersion
);
const msg = getCommitMessage(config);
await execa("git", ["commit", "-m", msg], { cwd });
}
if (args.tag !== false) {
const msg = config.templates.tagMessage.replaceAll(
"{{newVersion}}",
config.newVersion
);
const body = config.templates.tagBody.replaceAll(
"{{newVersion}}",
config.newVersion
);
const msg = getTagMessage(config);
const body = getTagBody(config);
await execa("git", ["tag", "-am", msg, body], { cwd });
}
if (args.push === true) {
Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getLastGitTag, getCurrentGitRef } from "./git";
import { resolveRepoConfig, RepoProvider } from "./repo";
import type { SemverBumpType } from "./semver";
import type { RepoConfig } from "./repo";
import { getTagMessagePattern } from "./template";

export interface ChangelogConfig {
cwd: string;
Expand Down Expand Up @@ -84,7 +85,7 @@ export async function loadChangelogConfig(
});

if (!config.from) {
config.from = await getLastGitTag();
config.from = await getLastGitTag(getTagMessagePattern(config));
}

if (!config.to) {
Expand Down
9 changes: 7 additions & 2 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ export interface GitCommit extends RawGitCommit {
isBreaking: boolean;
}

export async function getLastGitTag() {
const r = await execCommand("git", ["describe", "--tags", "--abbrev=0"])
export async function getLastGitTag(pattern?: string) {
const args = ["describe", "--tags", "--abbrev=0"];
if (pattern) {
args.push("--match", pattern);
}

const r = await execCommand("git", args)
.then((r) => r.split("\n"))
.catch(() => []);
return r.at(-1);
Expand Down
33 changes: 33 additions & 0 deletions src/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ChangelogConfig } from "./config";

function format(template: string, vars: Record<string, string>) {
let result = template;
for (const [key, value] of Object.entries(vars)) {
result = result.replaceAll(key, value);
}
return result;
}

export function getCommitMessage(config: ChangelogConfig) {
return format(config.templates.commitMessage, {
"{{newVersion}}": config.newVersion,
});
}

export function getTagMessage(config: ChangelogConfig) {
return format(config.templates.tagMessage, {
"{{newVersion}}": config.newVersion,
});
}

export function getTagBody(config: ChangelogConfig) {
return format(config.templates.tagBody, {
"{{newVersion}}": config.newVersion,
});
}

export function getTagMessagePattern(config: ChangelogConfig) {
return format(config.templates.tagMessage, {
"{{newVersion}}": "*",
});
}
51 changes: 51 additions & 0 deletions test/template.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, test, expect } from "vitest";
import {
getCommitMessage,
getTagMessage,
getTagBody,
getTagMessagePattern,
} from "../src/template";
import { ChangelogConfig } from "../src";

describe("template", () => {
test("getCommitMessage should work", () => {
const result = getCommitMessage({
templates: {
commitMessage: "v{{newVersion}}",
},
newVersion: "1.0.0",
} as ChangelogConfig);

expect(result).toBe("v1.0.0");
});
test("getTagMessage should work", () => {
const result = getTagMessage({
templates: {
tagMessage: "v{{newVersion}}",
},
newVersion: "1.0.0",
} as ChangelogConfig);

expect(result).toBe("v1.0.0");
});
test("getTagBody should work", () => {
const result = getTagBody({
templates: {
tagBody: "Release new version: v{{newVersion}}",
},
newVersion: "1.0.0",
} as ChangelogConfig);

expect(result).toBe("Release new version: v1.0.0");
});
test("getTagMessagePattern should work", () => {
const result = getTagMessagePattern({
templates: {
tagMessage: "v{{newVersion}}",
},
newVersion: "1.0.0",
} as ChangelogConfig);

expect(result).toBe("v*");
});
});