diff --git a/src/commands/default.ts b/src/commands/default.ts index 745e42f..cf81ed1 100644 --- a/src/commands/default.ts +++ b/src/commands/default.ts @@ -11,6 +11,7 @@ import { generateMarkDown, BumpVersionOptions, } from ".."; +import { getCommitMessage, getTagBody, getTagMessage } from "../template"; import { npmPublish, renamePackage } from "../package"; import { githubRelease } from "./github"; @@ -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) { diff --git a/src/config.ts b/src/config.ts index 4b6c43c..8f319a7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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; @@ -84,7 +85,7 @@ export async function loadChangelogConfig( }); if (!config.from) { - config.from = await getLastGitTag(); + config.from = await getLastGitTag(getTagMessagePattern(config)); } if (!config.to) { diff --git a/src/git.ts b/src/git.ts index ef3b4b4..0a9065f 100644 --- a/src/git.ts +++ b/src/git.ts @@ -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); diff --git a/src/template.ts b/src/template.ts new file mode 100644 index 0000000..df52ad1 --- /dev/null +++ b/src/template.ts @@ -0,0 +1,33 @@ +import { ChangelogConfig } from "./config"; + +function format(template: string, vars: Record) { + 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}}": "*", + }); +} diff --git a/test/template.test.ts b/test/template.test.ts new file mode 100644 index 0000000..d1b14c4 --- /dev/null +++ b/test/template.test.ts @@ -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*"); + }); +});