From fa7800acb8aced36a99a0d0826ab32e69ebed114 Mon Sep 17 00:00:00 2001 From: Eser Ozvataf Date: Wed, 17 Jul 2024 15:43:04 +0300 Subject: [PATCH] feat: bumping modules with wildcard --- mod.ts | 8 ++++++-- util.ts | 13 +++++++++---- util_test.ts | 47 ++++++++++++++++++++++++++++++----------------- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/mod.ts b/mod.ts index 7878c4f..b837a86 100644 --- a/mod.ts +++ b/mod.ts @@ -38,6 +38,7 @@ import { summarizeVersionBumpsByModule, type VersionBump, type VersionUpdateResult, + type WorkspaceModule, } from "./util.ts"; // A random separator that is unlikely to be in a commit message. @@ -49,7 +50,10 @@ export type BumpWorkspaceOptions = { start?: string; /** The base branch name to compare commits. The default is the current branch. */ base?: string; - parseCommitMessage?: (commit: Commit) => VersionBump[] | Diagnostic; + parseCommitMessage?: ( + commit: Commit, + workspaceModules: WorkspaceModule[], + ) => VersionBump[] | Diagnostic; /** The root directory of the workspace. */ root?: string; /** The git user name which is used for making a commit */ @@ -147,7 +151,7 @@ export async function bumpWorkspaces( // Skip if the commit subject is release continue; } - const parsed = parseCommitMessage(commit); + const parsed = parseCommitMessage(commit, modules); if (Array.isArray(parsed)) { for (const versionBump of parsed) { const diagnostic = checkModuleName(versionBump, modules); diff --git a/util.ts b/util.ts index 9f57f9c..25660dd 100644 --- a/util.ts +++ b/util.ts @@ -105,7 +105,7 @@ const TAG_TO_VERSION: Record = { const TAG_PRIORITY = Object.keys(TAG_TO_VERSION); -export const DEFAULT_RANGE_REQUIED = [ +export const DEFAULT_RANGE_REQUIRED = [ "BREAKING", "feat", "fix", @@ -115,6 +115,7 @@ export const DEFAULT_RANGE_REQUIED = [ export function defaultParseCommitMessage( commit: Commit, + workspaceModules: WorkspaceModule[], ): VersionBump[] | Diagnostic { const match = RE_DEFAULT_PATTERN.exec(commit.subject); if (match === null) { @@ -125,9 +126,13 @@ export function defaultParseCommitMessage( }; } const [, tag, module, _message] = match; - const modules = module ? module.split(/\s*,\s*/) : []; + const modules = module === "*" + ? workspaceModules.map((x) => x.name) + : module + ? module.split(/\s*,\s*/) + : []; if (modules.length === 0) { - if (DEFAULT_RANGE_REQUIED.includes(tag)) { + if (DEFAULT_RANGE_REQUIRED.includes(tag)) { return { type: "missing_range", commit, @@ -341,7 +346,7 @@ export async function applyVersionBump( if (oldModule.version !== module.version) { // The version is manually updated console.info( - `Manaul version update detected for ${module.name}: ${oldModule.version} -> ${module.version}`, + `Manual version update detected for ${module.name}: ${oldModule.version} -> ${module.version}`, ); const diff = calcVersionDiff(module.version, oldModule.version); diff --git a/util_test.ts b/util_test.ts index c8d7817..0c6738a 100644 --- a/util_test.ts +++ b/util_test.ts @@ -18,9 +18,9 @@ import { pathProp, summarizeVersionBumpsByModule, type VersionBump, + type WorkspaceModule, } from "./util.ts"; import { tryGetDenoConfig } from "./util.ts"; -import type { WorkspaceModule } from "./util.ts"; const emptyCommit = { subject: "", @@ -30,12 +30,20 @@ const emptyCommit = { const hash = "0000000000000000000000000000000000000000"; -function parse(subject: string) { - return defaultParseCommitMessage({ subject, body: "", hash }); +function parse(subject: string, workspaceModules: WorkspaceModule[]) { + return defaultParseCommitMessage( + { subject, body: "", hash }, + workspaceModules, + ); } Deno.test("defaultParseCommitMessage()", () => { - assertEquals(parse("feat(foo): add a feature"), [ + const modules: WorkspaceModule[] = [ + { name: "foo", version: "0.0.0", [pathProp]: "" }, + { name: "bar", version: "0.0.0", [pathProp]: "" }, + ]; + + assertEquals(parse("feat(foo): add a feature", modules), [ { module: "foo", tag: "feat", @@ -48,7 +56,7 @@ Deno.test("defaultParseCommitMessage()", () => { }, ]); - assertEquals(parse("fix(foo,bar): add a feature"), [ + assertEquals(parse("fix(foo,bar): add a feature", modules), [ { module: "foo", tag: "fix", @@ -71,7 +79,7 @@ Deno.test("defaultParseCommitMessage()", () => { }, ]); - assertEquals(parse("BREAKING(foo): some breaking change"), [ + assertEquals(parse("BREAKING(foo): some breaking change", modules), [ { module: "foo", tag: "BREAKING", @@ -84,7 +92,7 @@ Deno.test("defaultParseCommitMessage()", () => { }, ]); - assertEquals(parse("perf(foo): update"), [ + assertEquals(parse("perf(foo): update", modules), [ { module: "foo", tag: "perf", @@ -97,7 +105,7 @@ Deno.test("defaultParseCommitMessage()", () => { }, ]); - assertEquals(parse("docs(foo): update"), [ + assertEquals(parse("docs(foo): update", modules), [ { module: "foo", tag: "docs", @@ -110,7 +118,7 @@ Deno.test("defaultParseCommitMessage()", () => { }, ]); - assertEquals(parse("style(foo): update"), [ + assertEquals(parse("style(foo): update", modules), [ { module: "foo", tag: "style", @@ -123,7 +131,7 @@ Deno.test("defaultParseCommitMessage()", () => { }, ]); - assertEquals(parse("refactor(foo): update"), [ + assertEquals(parse("refactor(foo): update", modules), [ { module: "foo", tag: "refactor", @@ -136,7 +144,7 @@ Deno.test("defaultParseCommitMessage()", () => { }, ]); - assertEquals(parse("test(foo): update"), [ + assertEquals(parse("test(foo): update", modules), [ { module: "foo", tag: "test", @@ -149,7 +157,7 @@ Deno.test("defaultParseCommitMessage()", () => { }, ]); - assertEquals(parse("chore(foo): update"), [ + assertEquals(parse("chore(foo): update", modules), [ { module: "foo", tag: "chore", @@ -162,7 +170,7 @@ Deno.test("defaultParseCommitMessage()", () => { }, ]); - assertEquals(parse("deprecation(foo): update"), [ + assertEquals(parse("deprecation(foo): update", modules), [ { module: "foo", tag: "deprecation", @@ -208,7 +216,12 @@ Deno.test("checkModuleName()", () => { }); Deno.test("defaultParseCommitMessage() errors with invalid subject", () => { - assertEquals(parse("random commit"), { + const modules: WorkspaceModule[] = [ + { name: "foo", version: "0.0.0", [pathProp]: "" }, + { name: "bar", version: "0.0.0", [pathProp]: "" }, + ]; + + assertEquals(parse("random commit", modules), { type: "unknown_commit", commit: { subject: "random commit", @@ -217,7 +230,7 @@ Deno.test("defaultParseCommitMessage() errors with invalid subject", () => { }, reason: "The commit message does not match the default pattern.", }); - assertEquals(parse("fix: update"), { + assertEquals(parse("fix: update", modules), { type: "missing_range", commit: { subject: "fix: update", @@ -226,7 +239,7 @@ Deno.test("defaultParseCommitMessage() errors with invalid subject", () => { }, reason: "The commit message does not specify a module.", }); - assertEquals(parse("chore: update"), { + assertEquals(parse("chore: update", modules), { type: "skipped_commit", commit: { subject: "chore: update", @@ -235,7 +248,7 @@ Deno.test("defaultParseCommitMessage() errors with invalid subject", () => { }, reason: "The commit message does not specify a module.", }); - assertEquals(parse("hey(foo): update"), { + assertEquals(parse("hey(foo): update", modules), { type: "unknown_commit", commit: { subject: "hey(foo): update",