refactor(scripts): cleanup & enforce code style (#1388)

This commit is contained in:
uncenter authored and GitHub committed 2024-10-27 15:32:00 -04:00
1 parent 7db9b234c1
commit 5d74d700a2
23 files changed
+290 -270

No files matched your search

+33 -30
View File
@@ -1,10 +1,11 @@
import { join } from "@std/path";
import type { UserstylesSchema } from "@/types/mod.ts";
import { REPO_ROOT } from "@/constants.ts";
import * as path from "@std/path";
import * as yaml from "@std/yaml";
import { REPO_ROOT } from "@/deps.ts";
import { updateFile } from "@/generate/utils.ts";
import { UserStylesSchema } from "@/types/mod.ts";
import { stringify } from "@std/yaml";
import { type ColorName, flavors } from "@catppuccin/palette";
import { writeWithPreamble } from "@/generate/utils.ts";
/**
* Macchiato color definitions as hex values.
@@ -17,12 +18,11 @@ const macchiatoHex = flavors.macchiato.colorEntries
const toIssueLabel = (slug: string | number) => `lbl:${slug}`;
export const syncIssueLabels = async (
userstyles: UserStylesSchema.Userstyles,
) => {
updateFile(
join(REPO_ROOT, ".github/issue-labeler.yml"),
stringify(
export async function syncIssueLabels(userstyles: UserstylesSchema.Userstyles) {
// .github/issue-labeler.yml
await writeWithPreamble(
path.join(REPO_ROOT, ".github/issue-labeler.yml"),
yaml.stringify(
Object.entries(userstyles)
.reduce((acc, [key]) => {
acc[key.toString()] = [`/${toIssueLabel(key)}(,.*)?$/gm`];
@@ -31,13 +31,14 @@ export const syncIssueLabels = async (
),
);
const userstyleIssueContent = Deno.readTextFileSync(join(
// .github/ISSUE_TEMPLATE/userstyle.yml
const userstyleIssueTemplate = Deno.readTextFileSync(path.join(
REPO_ROOT,
"scripts/generate/templates/userstyle-issue.yml",
));
Deno.writeTextFileSync(
join(REPO_ROOT, ".github/ISSUE_TEMPLATE/userstyle.yml"),
userstyleIssueContent.replace(
await Deno.writeTextFile(
path.join(REPO_ROOT, ".github/ISSUE_TEMPLATE/userstyle.yml"),
userstyleIssueTemplate.replace(
`"$LABELS"`,
`${
Object.entries(userstyles)
@@ -48,9 +49,9 @@ export const syncIssueLabels = async (
);
// .github/pr-labeler.yml
updateFile(
join(REPO_ROOT, ".github/pr-labeler.yml"),
stringify(
await writeWithPreamble(
path.join(REPO_ROOT, ".github/pr-labeler.yml"),
yaml.stringify(
Object.entries(userstyles)
.reduce((acc, [key]) => {
acc[`${key}`] = `styles/${key}/**/*`;
@@ -60,15 +61,17 @@ export const syncIssueLabels = async (
);
// .github/labels.yml
const syncLabelsContent = Object.entries(userstyles)
.map(([slug, style]) => {
return {
name: slug,
description: [style.name].flat().join(", "),
color: style.color ? macchiatoHex[style.color] : macchiatoHex.blue,
};
});
const syncLabels = join(REPO_ROOT, ".github/labels.yml");
// deno-lint-ignore no-explicit-any
await updateFile(syncLabels, stringify(syncLabelsContent as any));
};
await writeWithPreamble(
path.join(REPO_ROOT, ".github/labels.yml"),
yaml.stringify(
Object.entries(userstyles)
.map(([slug, style]) => {
return {
name: slug,
description: [style.name].flat().join(", "),
color: style.color ? macchiatoHex[style.color] : macchiatoHex.blue,
};
}),
),
);
}
+11 -31
View File
@@ -1,36 +1,14 @@
#!/usr/bin/env -S deno run -A
import { join } from "@std/path";
import { portsSchema, REPO_ROOT, userStylesSchema } from "@/deps.ts";
import type { PortsSchema, UserStylesSchema } from "@/types/mod.ts";
import * as path from "@std/path";
import { REPO_ROOT } from "@/constants.ts";
import { syncIssueLabels } from "@/generate/labels.ts";
import { generateMainReadme } from "@/generate/readme-repo.ts";
import { generateStyleReadmes } from "@/generate/readme-styles.ts";
import { updateFile } from "@/generate/utils.ts";
import { validateYaml } from "@/utils.ts";
import { writeWithPreamble } from "@/generate/utils.ts";
import { getPortsData, getUserstylesData } from "@/utils.ts";
const userstylesYaml = Deno.readTextFileSync(
join(REPO_ROOT, "scripts/userstyles.yml"),
);
const portsYaml = await fetch(
"https://raw.githubusercontent.com/catppuccin/catppuccin/main/resources/ports.yml",
).then((res) => res.text());
const [portsData, userstylesData] = await Promise.all([
await validateYaml<PortsSchema.PortsSchema>(
portsYaml,
portsSchema,
),
await validateYaml<UserStylesSchema.UserstylesSchema>(
userstylesYaml,
userStylesSchema,
),
]);
if (!userstylesData.userstyles) {
console.error("No userstyles found");
Deno.exit(1);
}
const userstylesData = getUserstylesData();
const portsData = await getPortsData();
/**
* Generate the main README.md, listing all ports as a table of contents
@@ -67,9 +45,11 @@ const maintainersCodeOwners = () => {
};
const userstylesStaffCodeOwners = () => {
const paths = ["/.github/", "/scripts/", "/template/"];
return paths.map((path) => `${path} @catppuccin/userstyles-staff`).join("\n");
return paths.map((path) => `${path} @catppuccin/userstyles-staff`).join(
"\n",
);
};
await updateFile(
join(REPO_ROOT, ".github/CODEOWNERS"),
await writeWithPreamble(
path.join(REPO_ROOT, ".github/CODEOWNERS"),
`${maintainersCodeOwners()}\n\n${userstylesStaffCodeOwners()}`,
);
+24 -14
View File
@@ -1,20 +1,21 @@
import { join } from "@std/path";
import type { PortsSchema, UserstylesSchema } from "@/types/mod.ts";
import { REPO_ROOT } from "@/constants.ts";
import * as path from "@std/path";
import Handlebars from "handlebars";
import { REPO_ROOT } from "@/deps.ts";
import { PortsSchema, UserStylesSchema } from "@/types/mod.ts";
import { updateFile, updateReadme } from "@/generate/utils.ts";
import { updateReadme } from "@/generate/utils.ts";
type MappedPorts = {
[k: string]: (
UserStylesSchema.Userstyle & { path: string }
UserstylesSchema.Userstyle & { path: string }
)[];
};
export const generateMainReadme = async (
userstyles: UserStylesSchema.Userstyles,
export async function generateMainReadme(
userstyles: UserstylesSchema.Userstyles,
portsData: PortsSchema.PortsSchema,
) => {
) {
if (!portsData.categories) throw ("Ports data is missing categories");
const categorized = Object.entries(userstyles)
@@ -23,7 +24,11 @@ export const generateMainReadme = async (
// only care about the first (primary) category in the categories array
acc[categories[0]] ??= [];
acc[categories[0]].push({ path: `styles/${slug}`, categories, ...port });
acc[categories[0]].push({
path: `styles/${slug}`,
categories,
...port,
});
// Sort by name, first array entry if necessary
acc[categories[0]].sort((a, b) =>
@@ -53,7 +58,13 @@ export const generateMainReadme = async (
emoji: meta.emoji,
name: meta.name,
ports: ports.map(
({ name, path, "current-maintainers": currentMaintainers }) => {
(
{
name,
path,
"current-maintainers": currentMaintainers,
},
) => {
return {
name: [name].flat(),
maintained: currentMaintainers.length > 0,
@@ -65,14 +76,13 @@ export const generateMainReadme = async (
}),
});
const readmePath = join(REPO_ROOT, "README.md");
await updateFile(
const readmePath = path.join(REPO_ROOT, "README.md");
await Deno.writeTextFile(
readmePath,
updateReadme({
readme: Deno.readTextFileSync(readmePath),
section: "userstyles",
newContent: portContent,
}),
false,
).catch((e) => console.error(e));
};
}
+15 -16
View File
@@ -1,6 +1,7 @@
import { UserStylesSchema } from "@/types/mod.ts";
import { join } from "@std/path";
import { REPO_ROOT } from "@/deps.ts";
import type { UserstylesSchema } from "@/types/mod.ts";
import { REPO_ROOT } from "@/constants.ts";
import * as path from "@std/path";
import Handlebars from "handlebars";
// we can have some nice things :)
@@ -14,8 +15,8 @@ Handlebars.registerHelper(
);
const heading = (
name: UserStylesSchema.Name,
link: UserStylesSchema.ApplicationLink,
name: UserstylesSchema.Name,
link: UserstylesSchema.ApplicationLink,
) => {
const [nameArray, linkArray] = [[name].flat(), [link].flat()];
@@ -30,11 +31,11 @@ const heading = (
});
};
const extractName = (
function extractName(
collaborators?:
| UserStylesSchema.CurrentMaintainers
| UserStylesSchema.PastMaintainers,
) => {
| UserstylesSchema.CurrentMaintainers
| UserstylesSchema.PastMaintainers,
) {
// no-op when undefined
if (!collaborators) return;
// set the name to the github.com/<name>
@@ -42,12 +43,10 @@ const extractName = (
c.name ??= c.url.split("/").pop();
return c;
});
};
}
export const generateStyleReadmes = (
userstyles: UserStylesSchema.Userstyles,
) => {
const stylesReadmePath = join(
export function generateStyleReadmes(userstyles: UserstylesSchema.Userstyles) {
const stylesReadmePath = path.join(
REPO_ROOT,
"scripts/generate/templates/userstyle.md",
);
@@ -77,9 +76,9 @@ export const generateStyleReadmes = (
},
});
Deno.writeTextFile(
join(REPO_ROOT, "styles", slug.toString(), "README.md"),
path.join(REPO_ROOT, "styles", slug.toString(), "README.md"),
readmeContent,
).catch((e) => console.error(e));
},
);
};
}
+11 -14
View File
@@ -1,15 +1,14 @@
export const updateFile = (
export function writeWithPreamble(
filePath: string,
fileContent: string,
comment = true,
) => {
const preamble = comment
? "# THIS FILE IS AUTOGENERATED. DO NOT EDIT IT BY HAND.\n"
: "";
return Deno.writeTextFile(filePath, preamble + fileContent);
};
) {
return Deno.writeTextFile(
filePath,
"# THIS FILE IS AUTOGENERATED. DO NOT EDIT IT BY HAND.\n" + fileContent,
);
}
export const updateReadme = ({
export function updateReadme({
readme,
section,
newContent,
@@ -17,20 +16,18 @@ export const updateReadme = ({
readme: string;
section: string;
newContent: string;
}): string => {
}): string {
const preamble =
"<!-- the following section is auto-generated, do not edit -->";
const startMarker = `<!-- AUTOGEN:${section.toUpperCase()} START -->`;
const endMarker = `<!-- AUTOGEN:${section.toUpperCase()} END -->`;
const wrapped = `${startMarker}\n${preamble}\n${newContent}\n${endMarker}`;
if (
!(readme.includes(startMarker) && readme.includes(endMarker))
) {
if (!(readme.includes(startMarker) && readme.includes(endMarker))) {
throw new Error("Markers not found in README.md");
}
const pre = readme.split(startMarker)[0];
const end = readme.split(endMarker)[1];
return pre + wrapped + end;
};
}