Skip to content

Commit

Permalink
feat: bumping modules with wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
eser committed Jul 17, 2024
1 parent cc955a4 commit fa7800a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
8 changes: 6 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 9 additions & 4 deletions util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const TAG_TO_VERSION: Record<string, "major" | "minor" | "patch"> = {

const TAG_PRIORITY = Object.keys(TAG_TO_VERSION);

export const DEFAULT_RANGE_REQUIED = [
export const DEFAULT_RANGE_REQUIRED = [
"BREAKING",
"feat",
"fix",
Expand All @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
47 changes: 30 additions & 17 deletions util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -84,7 +92,7 @@ Deno.test("defaultParseCommitMessage()", () => {
},
]);

assertEquals(parse("perf(foo): update"), [
assertEquals(parse("perf(foo): update", modules), [
{
module: "foo",
tag: "perf",
Expand All @@ -97,7 +105,7 @@ Deno.test("defaultParseCommitMessage()", () => {
},
]);

assertEquals(parse("docs(foo): update"), [
assertEquals(parse("docs(foo): update", modules), [
{
module: "foo",
tag: "docs",
Expand All @@ -110,7 +118,7 @@ Deno.test("defaultParseCommitMessage()", () => {
},
]);

assertEquals(parse("style(foo): update"), [
assertEquals(parse("style(foo): update", modules), [
{
module: "foo",
tag: "style",
Expand All @@ -123,7 +131,7 @@ Deno.test("defaultParseCommitMessage()", () => {
},
]);

assertEquals(parse("refactor(foo): update"), [
assertEquals(parse("refactor(foo): update", modules), [
{
module: "foo",
tag: "refactor",
Expand All @@ -136,7 +144,7 @@ Deno.test("defaultParseCommitMessage()", () => {
},
]);

assertEquals(parse("test(foo): update"), [
assertEquals(parse("test(foo): update", modules), [
{
module: "foo",
tag: "test",
Expand All @@ -149,7 +157,7 @@ Deno.test("defaultParseCommitMessage()", () => {
},
]);

assertEquals(parse("chore(foo): update"), [
assertEquals(parse("chore(foo): update", modules), [
{
module: "foo",
tag: "chore",
Expand All @@ -162,7 +170,7 @@ Deno.test("defaultParseCommitMessage()", () => {
},
]);

assertEquals(parse("deprecation(foo): update"), [
assertEquals(parse("deprecation(foo): update", modules), [
{
module: "foo",
tag: "deprecation",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit fa7800a

Please sign in to comment.