Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero authored Feb 20, 2025
2 parents f0cb273 + 5983584 commit 78d8d86
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
31 changes: 30 additions & 1 deletion bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const argv = parseArgs(Deno.args, {
https: true,
quiet: false,
head: null,
combine: false,
"bullet-style": "-",
sortReleases: true,
},
Expand All @@ -27,6 +28,7 @@ const argv = parseArgs(Deno.args, {
"quiet",
"sortReleases",
"help",
"combine"
],
alias: {
h: "help",
Expand Down Expand Up @@ -98,9 +100,35 @@ try {
}
}

if (argv.combine) {
const combinedReleases = changelog.releases.reduce((acc, release) => {
if (release.version) {
if (acc[release.version]) {
acc[release.version].combineChanges(release.changes);
} else {
acc[release.version] = release;
}
}
return acc;
}, {} as Record<string, typeof changelog.releases[0]>);

changelog.releases = Object.values(combinedReleases);

console.info(combinedReleases);
}

if (argv.create) {
const version = typeof argv.create === "string" ? argv.create : undefined;
changelog.addRelease(new Release(version));

const release = changelog.releases.find((release) => {
return release.version === version
});

if (release) {
console.warn("Release already exists.");
} else {
changelog.addRelease(new Release(version));
}
}

save(file, changelog);
Expand Down Expand Up @@ -204,6 +232,7 @@ Options:
--latest-release Print the latest release version
--release Set the date of the specified release
--combine Combine changes from releases with the same version
--create Create a new release
--sortReleases Sort releases (default: true)
Expand Down
12 changes: 12 additions & 0 deletions src/Release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ export default class Release {
return 0;
}

combineChanges(changes: Map<string, Change[]>) {
changes.forEach((changes, type) => {
if (!this.changes.has(type)) {
this.changes.set(type, []);
}

this.changes.get(type)!.push(...changes);
});

return this;
}

isEmpty() {
if (this.description.trim()) {
return false;
Expand Down
30 changes: 29 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Changelog, parser, Release } from "../mod.ts";
import { Change, Changelog, parser, Release } from "../mod.ts";
import customRelease from "./fixture/CustomRelease.ts";
import { assertEquals } from "./deps.ts";

Expand Down Expand Up @@ -144,6 +144,34 @@ Deno.test("should update the date of a dated release", function () {
assertEquals(release.date?.getUTCDate(), 2);
});

Deno.test("should combine changes with other release", function () {
const releaseA = new Release();
releaseA.addChange("added", "foo");
const releaseB = new Release();
releaseB.addChange("added", "bar");

const expectedResult = new Map<string, Change[]>();
expectedResult.set("added", [
new Change("foo"),
new Change("bar")
]
);
expectedResult.set("changed", []);
expectedResult.set("deprecated", []);
expectedResult.set("removed", []);
expectedResult.set("fixed", []);
expectedResult.set("security", []);

releaseA.combineChanges(releaseB.changes);

assertEquals(releaseA.changes.get("added"), expectedResult.get("added"));
assertEquals(releaseA.changes.get("changed"), expectedResult.get("changed"));
assertEquals(releaseA.changes.get("deprecated"), expectedResult.get("deprecated"));
assertEquals(releaseA.changes.get("removed"), expectedResult.get("removed"));
assertEquals(releaseA.changes.get("fixed"), expectedResult.get("fixed"));
assertEquals(releaseA.changes.get("security"), expectedResult.get("security"));
});

Deno.test("adds a change with a new method maintenance()", function () {
assertEquals(
customRelease().maintenance("maintenance").isEmpty(),
Expand Down

0 comments on commit 78d8d86

Please sign in to comment.