Skip to content
Merged
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
34 changes: 30 additions & 4 deletions website/src/data/changes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { execFileSync } from "node:child_process";
import { existsSync, readdirSync, readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import path from "node:path";

const repositoryRoot = fileURLToPath(new URL("../../../", import.meta.url));
const repositoryRoot = findRepositoryRoot(process.cwd());
const repositoryUrl = "https://github.com/Utility-Gods/gruntend";

export interface ChangeReference {
Expand Down Expand Up @@ -35,6 +34,31 @@ export interface ReleasedChange {
version: string;
}

function findRepositoryRoot(startDirectory: string): string {
let directory = path.resolve(startDirectory);

while (true) {
const changelogPath = path.join(directory, "CHANGELOG.md");
const changesetConfigPath = path.join(
directory,
".changeset",
"config.json",
);

if (existsSync(changelogPath) && existsSync(changesetConfigPath)) {
return directory;
}

const parent = path.dirname(directory);
if (parent === directory) {
throw new Error(
`Unable to find the Gruntend repository root from ${startDirectory}.`,
);
}
directory = parent;
}
}

function gitDate(args: string[]): string | undefined {
try {
const value = execFileSync("git", args, {
Expand Down Expand Up @@ -151,12 +175,14 @@ function releaseDate(

function readReleasedChanges(): ReleasedChange[] {
const changelogPath = path.join(repositoryRoot, "CHANGELOG.md");
if (!existsSync(changelogPath)) return [];

const source = readFileSync(changelogPath, "utf8");
const headingPattern = /^##\s+([^\s(]+)(?:\s+\(([^)]+)\))?\s*$/gm;
const headings = [...source.matchAll(headingPattern)];

if (headings.length === 0) {
throw new Error(`No releases found in ${changelogPath}.`);
}

return headings.map((heading, index) => {
const version = heading[1];
const bodyStart = (heading.index ?? 0) + heading[0].length;
Expand Down
Loading