From 6cfb96b1e467fab66f4c3f144a29ec215af12fbd Mon Sep 17 00:00:00 2001 From: uncenter <47499684+uncenter@users.noreply.github.com> Date: Fri, 26 Jan 2024 17:37:57 -0500 Subject: [PATCH] ci(scripts): consistent variables & comments (#420) --- scripts/deps.ts | 4 ++-- scripts/generate/labels.ts | 2 +- scripts/lint/file-checker.ts | 2 +- scripts/lint/logger.ts | 4 ++-- scripts/lint/main.ts | 26 +++++++++------------ scripts/lint/metadata.ts | 40 ++++++++++++++++---------------- scripts/lint/stylelint.ts | 5 ++-- scripts/sync-maintainers/main.ts | 14 +++++++---- 8 files changed, 49 insertions(+), 48 deletions(-) diff --git a/scripts/deps.ts b/scripts/deps.ts index 49f7c80b..0629c531 100644 --- a/scripts/deps.ts +++ b/scripts/deps.ts @@ -1,7 +1,7 @@ -import portsSchema from "catppuccin-repo/resources/ports.schema.json" assert { +import portsSchema from "catppuccin-repo/resources/ports.schema.json" with { type: "json", }; -import userStylesSchema from "@/userstyles.schema.json" assert { +import userStylesSchema from "@/userstyles.schema.json" with { type: "json", }; diff --git a/scripts/generate/labels.ts b/scripts/generate/labels.ts index 82274809..6959060d 100644 --- a/scripts/generate/labels.ts +++ b/scripts/generate/labels.ts @@ -4,7 +4,7 @@ import { REPO_ROOT } from "@/deps.ts"; import { updateFile } from "@/generate/utils.ts"; import { UserStylesSchema } from "@/types/mod.ts"; import { stringify } from "std/yaml/stringify.ts"; -import palette from "https://raw.githubusercontent.com/catppuccin/palette/v0.2.0/palette-porcelain.json" assert { +import palette from "https://raw.githubusercontent.com/catppuccin/palette/v0.2.0/palette-porcelain.json" with { type: "json", }; diff --git a/scripts/lint/file-checker.ts b/scripts/lint/file-checker.ts index 2cac60db..b93f8e12 100644 --- a/scripts/lint/file-checker.ts +++ b/scripts/lint/file-checker.ts @@ -31,7 +31,7 @@ export const checkForMissingFiles = async () => { })); } - // only write summary if running in github actions + // Only write summary if running in GitHub Actions. if (Deno.env.has("GITHUB_ACTIONS")) { await core.summary .addHeading("Missing files") diff --git a/scripts/lint/logger.ts b/scripts/lint/logger.ts index cdce1d77..17642424 100644 --- a/scripts/lint/logger.ts +++ b/scripts/lint/logger.ts @@ -4,7 +4,7 @@ import core from "@actions/core"; export type LoggerProps = core.AnnotationProperties & { content?: string }; -const pretty_print = ( +const prettyPrint = ( message: string, props: LoggerProps, severity: "error" | "warning" = "warning", @@ -77,6 +77,6 @@ export const log = ( break; } } else { - pretty_print(message, props, severity); + prettyPrint(message, props, severity); } }; diff --git a/scripts/lint/main.ts b/scripts/lint/main.ts index b6cc65b5..b4a7b181 100755 --- a/scripts/lint/main.ts +++ b/scripts/lint/main.ts @@ -23,19 +23,19 @@ const stylesheets = walk(join(REPO_ROOT, "styles", subDir), { let failed = false; for await (const entry of stylesheets) { - const repodir = dirname(entry.path); - const repo = basename(repodir); + const dir = basename(dirname(entry.path)); const file = relative(REPO_ROOT, entry.path); const content = await Deno.readTextFile(entry.path); - // verify the usercss metadata - const { globalVars, isLess } = await verifyMetadata(entry, content, repo); - // don't attempt to compile or lint non-less files + // Verify the UserCSS metadata. + const { globalVars, isLess } = await verifyMetadata(entry, content, dir); + + // Don't attempt to compile or lint non-LESS files. if (!isLess) continue; - // try to compile the less file, report any errors - less.render(content, { lint: true, globalVars }).catch( + // Try to compile the LESS file, report any errors. + less.render(content, { lint: true, globalVars: globalVars }).catch( (err: Less.RenderError) => { failed = true; log( @@ -46,15 +46,11 @@ for await (const entry of stylesheets) { }, ); - // advanced linting with stylelint + // Lint with Stylelint. await lint(entry, content, flags.fix).catch(() => failed = true); } -// if any files are missing, cause the workflow to fail -if (await checkForMissingFiles() === false) { - failed = true; -} +if (await checkForMissingFiles() === false) failed = true; -if (failed) { - Deno.exit(1); -} +// Cause the workflow to fail if any issues were found. +if (failed) Deno.exit(1); diff --git a/scripts/lint/metadata.ts b/scripts/lint/metadata.ts index 74bc54ca..5a44c63e 100644 --- a/scripts/lint/metadata.ts +++ b/scripts/lint/metadata.ts @@ -12,16 +12,16 @@ import { getUserstylesData } from "@/utils.ts"; export const verifyMetadata = async ( entry: WalkEntry, content: string, - repo: string, + userstyle: string, ) => { - const assert = await assertions(repo); + const assert = await assertions(userstyle); const file = relative(REPO_ROOT, entry.path); const { metadata, errors: parsingErrors } = usercssMeta.parse(content, { allowErrors: true, }); - // pretty print / annotate the parsing errors + // Pretty print / annotate the parsing errors. parsingErrors.map((e) => { let startLine = 0; for (const line of content.split("\n")) { @@ -32,17 +32,17 @@ export const verifyMetadata = async ( log(e.message, { file, startLine, content }); }); - Object.entries(assert).forEach(([k, v]) => { - const defacto = metadata[k]; - if (defacto !== v) { + for (const [key, value] of Object.entries(assert)) { + const defacto = metadata[key]; + if (defacto !== value) { const line = content .split("\n") - .findIndex((line) => line.includes(k)) + 1; + .findIndex((line) => line.includes(key)) + 1; const message = sprintf( "Metadata %s should be %s but is %s", - color.bold(k), - color.green(v), + color.bold(key), + color.green(value), color.red(String(defacto)), ); @@ -52,12 +52,12 @@ export const verifyMetadata = async ( content, }, "warning"); } - }); + } - // parse the usercss variables to less global variables, e.g. + // Parse the UserCSS variables to LESS global variables, e.g. // `@var select lightFlavor "Light Flavor" ["latte:Latte*", "frappe:Frappé", "macchiato:Macchiato", "mocha:Mocha"]` // gets parsed as - // `lightFlavor: "latte"` + // `lightFlavor: "latte"`. const globalVars = Object.entries(metadata.vars) .reduce((acc, [k, v]) => { return { ...acc, [k]: v.default }; @@ -69,7 +69,7 @@ export const verifyMetadata = async ( }; }; -const assertions = async (repo: string) => { +const assertions = async (userstyle: string) => { const prefix = "https://github.com/catppuccin/userstyles"; const { userstyles } = await getUserstylesData().catch((err) => { @@ -79,16 +79,16 @@ const assertions = async (repo: string) => { return { name: `${ - Array.isArray(userstyles[repo].name) - ? (userstyles[repo].name as string[]).join("/") - : userstyles[repo].name + Array.isArray(userstyles[userstyle].name) + ? (userstyles[userstyle].name as string[]).join("/") + : userstyles[userstyle].name } Catppuccin`, - namespace: `github.com/catppuccin/userstyles/styles/${repo}`, + namespace: `github.com/catppuccin/userstyles/styles/${userstyle}`, author: "Catppuccin", license: "MIT", preprocessor: "less", - homepageURL: `${prefix}/tree/main/styles/${repo}`, - updateURL: `${prefix}/raw/main/styles/${repo}/catppuccin.user.css`, - supportURL: `${prefix}/issues?q=is%3Aopen+is%3Aissue+label%3A${repo}`, + homepageURL: `${prefix}/tree/main/styles/${userstyle}`, + updateURL: `${prefix}/raw/main/styles/${userstyle}/catppuccin.user.css`, + supportURL: `${prefix}/issues?q=is%3Aopen+is%3Aissue+label%3A${userstyle}`, }; }; diff --git a/scripts/lint/stylelint.ts b/scripts/lint/stylelint.ts index 96ab1740..18f8e394 100644 --- a/scripts/lint/stylelint.ts +++ b/scripts/lint/stylelint.ts @@ -1,5 +1,5 @@ import * as color from "std/fmt/colors.ts"; -import { WalkEntry } from "std/fs/mod.ts"; +import type { WalkEntry } from "std/fs/walk.ts"; import { relative } from "std/path/mod.ts"; import "npm:postcss-less"; @@ -15,7 +15,7 @@ export const lint = (entry: WalkEntry, content: string, fix: boolean) => .then(({ results }) => { results.map((result) => { result.warnings.map((warning) => { - // Some cleanup for fancier logging, dims the rule name + // Some cleanup for fancier logging - dims the rule name. const message = warning.text?.replace( new RegExp(`\\(?${warning.rule}\\)?`), color.dim(`(${warning.rule})`), @@ -30,6 +30,7 @@ export const lint = (entry: WalkEntry, content: string, fix: boolean) => content, }, warning.severity); }); + if (result.warnings.length > 0) throw new Error("stylelint error"); }); }); diff --git a/scripts/sync-maintainers/main.ts b/scripts/sync-maintainers/main.ts index 35695ff4..a8317991 100755 --- a/scripts/sync-maintainers/main.ts +++ b/scripts/sync-maintainers/main.ts @@ -13,7 +13,7 @@ const userstylesData = await getUserstylesData().catch((err) => { Deno.exit(1); }); -// lowercase usernames of all the "current-maintainers" in the file +// Lowercase usernames of all the "current-maintainers" in the file. const maintainers = [ ...new Set( Object.values(userstylesData.userstyles).flatMap(( @@ -21,7 +21,7 @@ const maintainers = [ ) => style.readme["current-maintainers"].map((m) => { const username = m.url.split("github.com/")?.pop(); - // check that they follow github.com/username pattern + // Check that they follow github.com/username pattern. assert.assertExists(username); return username.toLowerCase(); }) @@ -29,7 +29,7 @@ const maintainers = [ ), ]; -// lowercase usernames of all maintainers in the current GH team +// Lowercase usernames of all maintainers in the current GitHub team. const teamMembers = await octokit.teams .listMembersInOrg({ ...team, @@ -54,7 +54,9 @@ const syncMaintainers = async () => { }); console.log(`Added ${m} to the ${team.org}/${team.team_slug} team.`); } - console.log(`${toAdd.length} users added to the ${team.org}/${team.team_slug} team.`) + console.log( + `${toAdd.length} users added to the ${team.org}/${team.team_slug} team.`, + ); for (const m of toRemove) { await octokit.teams.removeMembershipForUserInOrg({ @@ -63,7 +65,9 @@ const syncMaintainers = async () => { }); console.log(`Removed ${m} from the ${team.org}/${team.team_slug} team.`); } - console.log(`${toRemove.length} users removed from the ${team.org}/${team.team_slug} team.`) + console.log( + `${toRemove.length} users removed from the ${team.org}/${team.team_slug} team.`, + ); }; await syncMaintainers();