refactor(tree-wide): add & use calver implementation (#1534)

Co-authored-by: Hammy <[email protected]>
This commit is contained in:
uncenterandHammy authored and GitHub committed 2024-12-28 17:52:30 -05:00
1 parent 0a9cfae2c9
commit fb505120bd
122 files changed
+272 -127

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
import { CalVer } from "@/bump-version/calver.ts";
import { assertEquals } from "@std/assert";
Deno.test({
name: "constructor",
fn() {
const calver = new CalVer("2000.01.01");
assertEquals(calver.toString(), "2000.01.01");
},
});
Deno.test({
name: "incrementWith",
fn() {
const calver = new CalVer("2000.01.01");
calver.incrementWith(new Date("2000-01-01"));
assertEquals(calver.toString(), "2000.01.01.1");
calver.incrementWith(new Date("2000-01-01"));
assertEquals(calver.toString(), "2000.01.01.2");
calver.incrementWith(new Date("2000-01-02"));
assertEquals(calver.toString(), "2000.01.02");
},
});
Deno.test({
name: "incrementWith - new year",
fn() {
const calver = new CalVer("2000.12.31");
calver.incrementWith(new Date("2001-01-01"));
assertEquals(calver.toString(), "2001.01.01");
},
});
Deno.test({
name: "incrementWith - new month",
fn() {
const calver = new CalVer("2000.01.31");
calver.incrementWith(new Date("2000-02-01"));
assertEquals(calver.toString(), "2000.02.01");
},
});
Deno.test({
name: "incrementWith - earlier date",
fn() {
const calver = new CalVer("2000.01.01");
calver.incrementWith(new Date("1999-12-31"));
assertEquals(calver.toString(), "1999.12.31");
},
});
+37
View File
@@ -0,0 +1,37 @@
export class CalVer {
year: number;
month: number;
day: number;
micro?: number;
constructor(date: string) {
const [year, month, day, micro] = date.split(".").map((num) =>
Number.parseInt(num)
);
this.year = year;
this.month = month;
this.day = day;
this.micro = micro ?? undefined;
}
toString() {
return `${this.year}.${this.month.toString().padStart(2, "0")}.${
this.day.toString().padStart(2, "0")
}${this.micro ? "." + this.micro : ""}`;
}
incrementWith(date: Date) {
const year = date.getUTCFullYear();
const month = date.getUTCMonth() + 1;
const day = date.getUTCDate();
if (this.year === year && this.month === month && this.day === day) {
this.micro = (this.micro ?? 0) + 1;
} else {
this.year = year;
this.month = month;
this.day = day;
this.micro = undefined;
}
}
}
+57
View File
@@ -0,0 +1,57 @@
import * as path from "@std/path";
import { REPO_ROOT } from "@/constants.ts";
import { CalVer } from "@/bump-version/calver.ts";
import { parseArgs } from "@std/cli";
const args = parseArgs(Deno.args, { boolean: ["all"] });
if (!Deno.env.get("CI") && !args.all) {
throw new Error(
"This script is intended to be used in CI. Userstyle versions are automatically bumped after pull requests are merged.",
);
}
let files = [];
if (args.all) {
for (const dir of Deno.readDirSync(path.join(REPO_ROOT, "styles"))) {
if (!dir.isDirectory) continue;
files.push(path.join(REPO_ROOT, "styles", dir.name, "catppuccin.user.css"));
}
} else {
files = args._.filter((val) => typeof val == "string").map((p: string) =>
path.join(REPO_ROOT, p)
);
}
for (
const file of files
) {
const content = await Deno.readTextFile(file);
const metadataMatches = content.match(
/^\/\*\s*==UserStyle==[\s\S]*?==\/UserStyle==\s*\*\//,
);
if (!metadataMatches) {
throw new Error(`No metadata found in ${file}`);
}
const metadata = metadataMatches[0];
const post = content.slice(metadata.length);
const versionMatches = metadata.match(/@version(\s+)(.*)/);
if (!versionMatches) {
throw new Error(`No version found in ${file}`);
}
const whitespace = versionMatches[1];
const version = new CalVer(versionMatches[2]);
version.incrementWith(new Date());
const newContent = metadata.replace(
/@version\s+.*/,
`@version${whitespace}${version.toString()}`,
) + post;
await Deno.writeTextFile(file, newContent);
}