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): cleanup & enforce code style #1388

Merged
merged 11 commits into from
Oct 27, 2024
2 changes: 1 addition & 1 deletion .github/workflows/generate-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
deno-version: v1.x

- name: Generate health files
run: deno task ci:generate
run: deno task generate

- name: Commit changes
uses: EndBug/add-and-commit@v9
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ jobs:
with:
deno-version: v1.x

- name: Generate import file
run: deno task ci:generate-import
- name: Generate Stylus import file
run: deno task stylus-import

- name: Upload Release Artifacts
run: gh release upload --clobber all-userstyles-export ./dist/import.json
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/maintainers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ jobs:

- name: Sync maintainers
if: ${{ github.repository == 'catppuccin/userstyles' && github.ref == 'refs/heads/main' }}
run: deno task ci:sync-maintainers
run: deno task sync-maintainers
env:
GITHUB_TOKEN: ${{ secrets.USERSTYLES_TOKEN }}
21 changes: 15 additions & 6 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.6",
"@std/yaml": "jsr:@std/yaml@^1.0.5",
"ajv": "npm:ajv@8.17.1",
"catppuccin-repo/": "https://raw.githubusercontent.com/catppuccin/catppuccin/d4f2666c2b04337f0a8632713de0889d9a7d332d/",
"@catppuccin/catppuccin/": "https://raw.githubusercontent.com/catppuccin/catppuccin/d4f2666c2b04337f0a8632713de0889d9a7d332d/",
"handlebars": "npm:handlebars@4.7.8",
"json-schema-to-typescript": "npm:json-schema-to-typescript@15.0.2",
"less": "npm:less@4.2.0",
Expand All @@ -25,13 +25,22 @@
"usercss-meta": "npm:usercss-meta@0.12.0"
},
"tasks": {
"ci:generate": "deno run -A ./scripts/generate/main.ts",
"ci:generate-import": "deno run -A ./scripts/import-styles/main.ts",
"ci:sync-maintainers": "deno run -A ./scripts/sync-maintainers/main.ts",
"generate": "deno run -A ./scripts/generate/main.ts",
"stylus-import": "deno run -A ./scripts/stylus-import/main.ts",
"sync-maintainers": "deno run -A ./scripts/sync-maintainers/main.ts",
"lint": "deno run -A ./scripts/lint/main.ts",
"lint:fix": "deno task lint --fix",
"update-types": "deno run -A ./scripts/update-types.ts",
"update-types": "deno run -A ./scripts/types/update-types.ts",
"format": "deno run -A npm:prettier@3.2.4 --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.

18 changes: 18 additions & 0 deletions scripts/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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.

63 changes: 33 additions & 30 deletions scripts/generate/labels.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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`];
Expand All @@ -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)
Expand All @@ -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}/**/*`;
Expand All @@ -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,
};
}),
),
);
}
42 changes: 11 additions & 31 deletions scripts/generate/main.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()}`,
);
38 changes: 24 additions & 14 deletions scripts/generate/readme-repo.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) =>
Expand Down Expand Up @@ -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,
Expand All @@ -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));
};
}
Loading
Loading