Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(scripts): overhaul #1262

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
13 changes: 11 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@std/path": "jsr:@std/path@^1.0.2",
"@std/yaml": "jsr:@std/yaml@^1.0.3",
"ajv": "npm:[email protected]",
"catppuccin-repo/": "https://raw.githubusercontent.com/catppuccin/catppuccin/d4f2666c2b04337f0a8632713de0889d9a7d332d/",
"@catppuccin/catppuccin/": "https://raw.githubusercontent.com/catppuccin/catppuccin/d4f2666c2b04337f0a8632713de0889d9a7d332d/",
"handlebars": "npm:[email protected]",
"json-schema-to-typescript": "npm:[email protected]",
"less": "npm:[email protected]",
Expand All @@ -33,5 +33,14 @@
"update-types": "deno run -A ./scripts/update-types.ts",
"format": "deno run -A npm:[email protected] --write ."
},
"nodeModulesDir": true
"nodeModulesDir": true,
"fmt": {
"include": ["scripts/**/*.ts"]
},
"lint": {
"rules": {
"tags": ["recommended"],
"include": ["verbatim-module-syntax"]
}
}
}
1 change: 0 additions & 1 deletion scripts/_prettier.ts

This file was deleted.

20 changes: 20 additions & 0 deletions scripts/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import PORTS_SCHEMA from "@catppuccin/catppuccin/resources/ports.schema.json" with {
type: "json",
};
import USERSTYLES_SCHEMA from "@/userstyles.schema.json" with {
type: "json",
};

import * as path from "@std/path";

const ROOT = import.meta.dirname;
if (!ROOT) {
throw new Error("ROOT was not located.");
}

/**
* Absolute path to the repository.
*/
export const REPO_ROOT = path.join(ROOT, "..");

export { PORTS_SCHEMA, USERSTYLES_SCHEMA };
20 changes: 0 additions & 20 deletions scripts/deps.ts

This file was deleted.

60 changes: 33 additions & 27 deletions scripts/generate/labels.ts
Original file line number Diff line number Diff line change
@@ -1,9 +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 { updateFileWithPreamble } from "@/generate/utils.ts";

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";

/**
Expand All @@ -17,12 +19,13 @@ const macchiatoHex = flavors.macchiato.colorEntries

const toIssueLabel = (slug: string | number) => `lbl:${slug}`;

export const syncIssueLabels = async (
export async function syncIssueLabels(
userstyles: UserStylesSchema.Userstyles,
) => {
updateFile(
join(REPO_ROOT, ".github/issue-labeler.yml"),
stringify(
) {
// .github/issue-labeler.yml
await updateFileWithPreamble(
path.join(REPO_ROOT, ".github/issue-labeler.yml"),
YAML.stringify(
Object.entries(userstyles)
.reduce((acc, [key]) => {
acc[key.toString()] = [`/${toIssueLabel(key)}(,.*)?$/gm`];
Expand All @@ -31,12 +34,13 @@ export const syncIssueLabels = async (
),
);

const userstyleIssueContent = Deno.readTextFileSync(join(
// .github/ISSUE_TEMPLATE/userstyle.yml
const userstyleIssueContent = Deno.readTextFileSync(path.join(
REPO_ROOT,
"scripts/generate/templates/userstyle-issue.yml",
));
Deno.writeTextFileSync(
join(REPO_ROOT, ".github/ISSUE_TEMPLATE/userstyle.yml"),
path.join(REPO_ROOT, ".github/ISSUE_TEMPLATE/userstyle.yml"),
userstyleIssueContent.replace(
`"$LABELS"`,
`${
Expand All @@ -48,9 +52,9 @@ export const syncIssueLabels = async (
);

// .github/pr-labeler.yml
updateFile(
join(REPO_ROOT, ".github/pr-labeler.yml"),
stringify(
await updateFileWithPreamble(
path.join(REPO_ROOT, ".github/pr-labeler.yml"),
YAML.stringify(
Object.entries(userstyles)
.reduce((acc, [key]) => {
acc[`${key}`] = `styles/${key}/**/*`;
Expand All @@ -60,15 +64,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 updateFileWithPreamble(
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,
};
}),
),
);
}
31 changes: 18 additions & 13 deletions scripts/generate/main.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#!/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 { PORTS_SCHEMA, REPO_ROOT, USERSTYLES_SCHEMA } from "@/constants.ts";

import * as path from "@std/path";

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 { updateFileWithPreamble } from "@/generate/utils.ts";
import { validateYaml } from "@/utils/yaml.ts";

const userstylesYaml = Deno.readTextFileSync(
join(REPO_ROOT, "scripts/userstyles.yml"),
path.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,
PORTS_SCHEMA,
),
await validateYaml<UserStylesSchema.UserstylesSchema>(
userstylesYaml,
userStylesSchema,
USERSTYLES_SCHEMA,
),
]);

Expand All @@ -49,6 +50,8 @@ generateStyleReadmes(userstylesData.userstyles);
*/
await syncIssueLabels(userstylesData.userstyles);

const STAFF_TEAM = "@catppuccin/userstyles-staff";

/**
* Keep `.github/CODEOWNERS` in sync with the userstyle metadata.
*/
Expand All @@ -61,15 +64,17 @@ const maintainersCodeOwners = () => {
const codeOwners = currentMaintainers
.map((maintainer) => `@${maintainer.url.split("/").pop()}`)
.join(" ");
return `/styles/${slug} ${codeOwners}`;
return `styles/${slug} ${codeOwners} !${STAFF_TEAM}`;
})
.join("\n");
};
const userstylesStaffCodeOwners = () => {
const paths = ["/.github/", "/scripts/", "/template/"];
return paths.map((path) => `${path} @catppuccin/userstyles-staff`).join("\n");
const paths = [".github/", "scripts/", "template/", "*"];
return paths.map((path) => `${path} ${STAFF_TEAM}`).join(
"\n",
);
};
await updateFile(
join(REPO_ROOT, ".github/CODEOWNERS"),
await updateFileWithPreamble(
path.join(REPO_ROOT, ".github/CODEOWNERS"),
`${maintainersCodeOwners()}\n\n${userstylesStaffCodeOwners()}`,
);
32 changes: 21 additions & 11 deletions scripts/generate/readme-repo.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { join } from "@std/path";
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 { REPO_ROOT } from "@/constants.ts";
import type { PortsSchema, UserStylesSchema } from "@/types/mod.ts";
import { updateFileWithPreamble, updateReadme } from "@/generate/utils.ts";

type MappedPorts = {
[k: string]: (
UserStylesSchema.Userstyle & { path: string }
)[];
};

export const generateMainReadme = async (
export async function generateMainReadme(
userstyles: UserStylesSchema.Userstyles,
portsData: PortsSchema.PortsSchema,
) => {
) {
if (!portsData.categories) throw ("Ports data is missing categories");

const categorized = Object.entries(userstyles)
Expand All @@ -23,7 +23,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) =>
Expand Down Expand Up @@ -53,7 +57,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,
Expand All @@ -65,8 +75,8 @@ export const generateMainReadme = async (
}),
});

const readmePath = join(REPO_ROOT, "README.md");
await updateFile(
const readmePath = path.join(REPO_ROOT, "README.md");
await updateFileWithPreamble(
readmePath,
updateReadme({
readme: Deno.readTextFileSync(readmePath),
Expand All @@ -75,4 +85,4 @@ export const generateMainReadme = async (
}),
false,
).catch((e) => console.error(e));
};
}
16 changes: 8 additions & 8 deletions scripts/generate/readme-styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 * as path from "@std/path";
import { REPO_ROOT } from "@/constants.ts";
import Handlebars from "handlebars";

// we can have some nice things :)
Expand Down Expand Up @@ -44,10 +44,10 @@ const extractName = (
});
};

export const generateStyleReadmes = (
export function generateStyleReadmes(
userstyles: UserStylesSchema.Userstyles,
) => {
const stylesReadmePath = join(
) {
const stylesReadmePath = path.join(
REPO_ROOT,
"scripts/generate/templates/userstyle.md",
);
Expand Down Expand Up @@ -77,9 +77,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));
},
);
};
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env -S deno run -A
import usercssMeta from "usercss-meta";
import { ensureDir, walk } from "@std/fs";
import { join } from "@std/path";
import * as path from "@std/path";

import { REPO_ROOT } from "@/deps.ts";
import { REPO_ROOT } from "@/constants.ts";

const stylesheets = walk(join(REPO_ROOT, "styles"), {
const stylesheets = walk(path.join(REPO_ROOT, "styles"), {
includeFiles: true,
includeDirs: false,
includeSymlinks: false,
Expand Down
12 changes: 6 additions & 6 deletions scripts/generate/utils.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
export const updateFile = (
export function updateFileWithPreamble(
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);
};
}

export const updateReadme = ({
export function updateReadme({
readme,
section,
newContent,
}: {
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 -->`;
Expand All @@ -33,4 +33,4 @@ export const updateReadme = ({
const pre = readme.split(startMarker)[0];
const end = readme.split(endMarker)[1];
return pre + wrapped + end;
};
}
Loading
Loading