diff --git a/scripts/lint/main.ts b/scripts/lint/main.ts index b4a7b181..88c8935d 100755 --- a/scripts/lint/main.ts +++ b/scripts/lint/main.ts @@ -10,6 +10,7 @@ import { checkForMissingFiles } from "@/lint/file-checker.ts"; import { log } from "@/lint/logger.ts"; import { verifyMetadata } from "@/lint/metadata.ts"; import { lint } from "@/lint/stylelint.ts"; +import { getUserstylesData } from "@/utils.ts"; const flags = parseFlags(Deno.args, { boolean: ["fix"] }); const subDir = flags._[0]?.toString() ?? ""; @@ -19,6 +20,7 @@ const stylesheets = walk(join(REPO_ROOT, "styles", subDir), { includeSymlinks: false, match: [/\.user.css$/], }); +const { userstyles } = getUserstylesData(); let failed = false; @@ -29,7 +31,12 @@ for await (const entry of stylesheets) { const content = await Deno.readTextFile(entry.path); // Verify the UserCSS metadata. - const { globalVars, isLess } = await verifyMetadata(entry, content, dir); + const { globalVars, isLess } = await verifyMetadata( + entry, + content, + dir, + userstyles, + ); // Don't attempt to compile or lint non-LESS files. if (!isLess) continue; diff --git a/scripts/lint/metadata.ts b/scripts/lint/metadata.ts index 136ad0a0..30df942b 100644 --- a/scripts/lint/metadata.ts +++ b/scripts/lint/metadata.ts @@ -7,14 +7,16 @@ import { join, relative } from "std/path/mod.ts"; import { REPO_ROOT } from "@/deps.ts"; import { log } from "@/lint/logger.ts"; -import { formatListOfItems, getUserstylesData } from "@/utils.ts"; +import { formatListOfItems } from "@/utils.ts"; +import type { Userstyles } from "@/types/userstyles.d.ts"; export const verifyMetadata = async ( entry: WalkEntry, content: string, userstyle: string, + userstyles: Userstyles, ) => { - const assert = await assertions(userstyle); + const assert = assertions(userstyle, userstyles); const file = relative(REPO_ROOT, entry.path); const { metadata, errors: parsingErrors } = usercssMeta.parse(content, { @@ -116,14 +118,9 @@ export const verifyMetadata = async ( }; }; -const assertions = async (userstyle: string) => { +const assertions = (userstyle: string, userstyles: Userstyles) => { const prefix = "https://github.com/catppuccin/userstyles"; - const { userstyles } = await getUserstylesData().catch((err) => { - console.error(err); - Deno.exit(1); - }); - if (!userstyles[userstyle]) { log("Metadata section for this userstyle has not been added", { file: "scripts/userstyles.yml", diff --git a/scripts/utils.ts b/scripts/utils.ts index cbde833a..283d29c8 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -5,6 +5,9 @@ import { SetRequired } from "type-fest/source/set-required.d.ts"; import { REPO_ROOT, userStylesSchema } from "@/deps.ts"; import { UserstylesSchema } from "@/types/userstyles.d.ts"; +import { YAMLError } from "std/yaml/_error.ts"; +import { log } from "@/lint/logger.ts"; +import { sprintf } from "std/fmt/printf.ts"; /** * @param content A string of YAML content @@ -14,34 +17,72 @@ import { UserstylesSchema } from "@/types/userstyles.d.ts"; export const validateYaml = ( content: string, schema: Schema, -): Promise => { - return new Promise((resolve, reject) => { - const ajv = new Ajv.default(); - const validate = ajv.compile(schema); - const data = parse(content); +): T => { + const ajv = new Ajv.default(); + const validate = ajv.compile(schema); + const data = parse(content); - if (!validate(data)) return reject(validate.errors); + if (!validate(data)) { + console.log( + "Found schema errors in scripts/userstyles.yml: " + + validate.errors?.map((err) => + sprintf( + "%s %s%s", + err.instancePath.slice(1).replaceAll("/", "."), + err.message, + err.params.allowedValues + ? ` (${JSON.stringify(err.params.allowedValues, undefined)})` + : "", + ) + ).join(" and "), + ); + Deno.exit(1); + } - return resolve(data); - }); + return data as T; }; /** * Utility function that calls {@link validateYaml} on the userstyles.yml file. * Fails when data.userstyles is undefined. */ -export const getUserstylesData = (): Promise => { - return new Promise((resolve, reject) => { - validateYaml( - Deno.readTextFileSync(join(REPO_ROOT, "scripts/userstyles.yml")), +export const getUserstylesData = (): Userstyles => { + const content = Deno.readTextFileSync( + join(REPO_ROOT, "scripts/userstyles.yml"), + ); + + try { + const data = validateYaml( + content, userStylesSchema, - ).then((data) => { - if (data.userstyles === undefined || data.collaborators === undefined) { - return reject("userstyles.yml is missing required fields"); - } - return resolve(data as Userstyles); - }); - }); + ); + + if (data.userstyles === undefined || data.collaborators === undefined) { + console.log("userstyles.yml is missing required fields"); + Deno.exit(1); + } + + return data as Userstyles; + } catch (err) { + if (err instanceof YAMLError) { + const groups = + /(?.*) at line (?\d+), column (?\d+):[\S\s]*/ + .exec(err.message)?.groups; + log( + groups!.message, + { + file: "scripts/userstyles.yml", + startLine: Number(groups!.line), + content: content, + }, + "error", + ); + } else { + console.log(err); + } + + Deno.exit(1); + } }; /**