From c5d639d6947b1e4fd8aa0a4ef7d5db4e2c112a84 Mon Sep 17 00:00:00 2001 From: uncenter <47499684+uncenter@users.noreply.github.com> Date: Fri, 13 Sep 2024 17:10:03 -0400 Subject: [PATCH] feat: improve scripts/lint logging (#1305) --- scripts/lint/file-checker.ts | 5 +-- scripts/lint/main.ts | 2 +- scripts/lint/metadata.ts | 46 +++++++++++++------- scripts/lint/stylelint.ts | 2 +- scripts/{lint => }/logger.ts | 84 ++++++++++++++++++++---------------- scripts/utils.ts | 42 ++++++++++-------- 6 files changed, 106 insertions(+), 75 deletions(-) rename scripts/{lint => }/logger.ts (54%) diff --git a/scripts/lint/file-checker.ts b/scripts/lint/file-checker.ts index e265ac1a..ccdcc309 100644 --- a/scripts/lint/file-checker.ts +++ b/scripts/lint/file-checker.ts @@ -3,8 +3,7 @@ import { join, relative } from "@std/path"; import core from "@actions/core"; import { REPO_ROOT } from "@/deps.ts"; -import { log } from "@/lint/logger.ts"; -import * as color from "@std/fmt/colors"; +import { log } from "@/logger.ts"; const requiredFiles = [ "catppuccin.user.css", @@ -37,7 +36,7 @@ export const checkForMissingFiles = async () => { .write(); } else { missingFiles.map((f) => { - log.error(color.red(`Missing file:`) + ` ${f}`, { file: f }); + log.error(`File does not exist`, { file: f }); }); } diff --git a/scripts/lint/main.ts b/scripts/lint/main.ts index e2ac391b..bc66f11c 100755 --- a/scripts/lint/main.ts +++ b/scripts/lint/main.ts @@ -7,7 +7,7 @@ import less from "less"; import { REPO_ROOT } from "@/deps.ts"; import { checkForMissingFiles } from "@/lint/file-checker.ts"; -import { log } from "@/lint/logger.ts"; +import { log } from "@/logger.ts"; import { verifyMetadata } from "@/lint/metadata.ts"; import { lint } from "@/lint/stylelint.ts"; import { getUserstylesData } from "@/utils.ts"; diff --git a/scripts/lint/metadata.ts b/scripts/lint/metadata.ts index 51472c66..5f33f135 100644 --- a/scripts/lint/metadata.ts +++ b/scripts/lint/metadata.ts @@ -6,7 +6,7 @@ import type { WalkEntry } from "@std/fs"; import { join, relative } from "@std/path"; import { REPO_ROOT } from "@/deps.ts"; -import { log } from "@/lint/logger.ts"; +import { log } from "@/logger.ts"; import { formatListOfItems } from "@/utils.ts"; import type { Userstyles } from "@/types/userstyles.d.ts"; @@ -30,30 +30,43 @@ export const verifyMetadata = async ( // Pretty print / annotate the parsing errors. parsingErrors.map((e) => { - let startLine = 0; - for (const line of lines) { - startLine++; - e.index -= line.length + 1; - if (e.index < 0) break; + let startLine; + if (e.index !== undefined && !Number.isNaN(e.index)) { + startLine = 0; + for (const line of lines) { + startLine++; + e.index -= line.length + 1; + if (e.index < 0) break; + } } - log.error(e.message, { file, startLine, content }); + + // Skip "missing mandatory metadata property" ParseError, assertions checks below will cover. + if (e.code === "missingMandatory") return; + + log.error(e.message, { + file, + startLine, + content, + }); }); for (const [key, expected] of Object.entries(assert)) { const current = metadata[key]; + const atKey = "@" + key; + if (current !== expected) { const line = lines - .findIndex((line) => line.includes(key)) + 1; + .findIndex((line) => line.includes(`${atKey} `)) + 1; const message = current === undefined ? sprintf( - "Metadata `%s` should not be undefined", - color.bold(key), + "UserCSS metadata property `%s` is undefined", + color.bold(atKey), ) : sprintf( - 'Metadata `%s` should be "%s" but is "%s"', - color.bold(key), + 'UserCSS metadata property `%s` should be "%s" but is "%s"', + color.bold(atKey), color.green(expected), color.red(String(current)), ); @@ -134,9 +147,12 @@ const assertions = (userstyle: string, userstyles: Userstyles) => { const prefix = "https://github.com/catppuccin/userstyles"; if (!userstyles[userstyle]) { - log.error("Metadata section for this userstyle has not been added", { - file: "scripts/userstyles.yml", - }); + log.error( + `Metadata section for \`${color.bold(userstyle)}\` userstyle is missing`, + { + file: "scripts/userstyles.yml", + }, + ); Deno.exit(1); } diff --git a/scripts/lint/stylelint.ts b/scripts/lint/stylelint.ts index df110230..a2e7d604 100644 --- a/scripts/lint/stylelint.ts +++ b/scripts/lint/stylelint.ts @@ -8,7 +8,7 @@ import "stylelint-config-standard"; import "stylelint-config-recommended"; import { REPO_ROOT } from "@/deps.ts"; -import { log } from "@/lint/logger.ts"; +import { log } from "@/logger.ts"; export const lint = ( entry: WalkEntry, diff --git a/scripts/lint/logger.ts b/scripts/logger.ts similarity index 54% rename from scripts/lint/logger.ts rename to scripts/logger.ts index 872642eb..08a55f38 100644 --- a/scripts/lint/logger.ts +++ b/scripts/logger.ts @@ -2,7 +2,10 @@ import { sprintf } from "@std/fmt/printf"; import * as color from "@std/fmt/colors"; import core from "@actions/core"; -export type LoggerProps = core.AnnotationProperties & { content?: string }; +export type LoggerProps = core.AnnotationProperties & { + file: string; + content?: string; +}; const prettyPrint = ( message: string, @@ -10,7 +13,6 @@ const prettyPrint = ( severity: "error" | "warning" = "warning", ) => { const { file, startColumn, startLine } = props; - if (!startLine) return console.log(message); const lines = (props.content ?? "").split("\n"); @@ -19,50 +21,58 @@ const prettyPrint = ( message, ].join(" "); - const line = lines[startLine - 1].split("").map((char, i) => { - const startCol = (props.startColumn ?? 0) - 1; - const endCol = (props.endColumn ?? Infinity) - 1; + const pad = startLine ? (startLine + 1).toString().length : 0; - if (i >= startCol && i <= endCol) { - return color[severity === "error" ? "red" : "yellow"](char); - } else { - return char; - } - }).join(""); + const logs = [color.underline( + sprintf( + "%s%s%s", + file, + startLine ? ":" + startLine : "", + startColumn ? ":" + startColumn : "", + ), + )]; - const pad = startLine.toString().length; - console.log( - [ - color.underline( - sprintf( - "%s%s%s", - file, - startLine ? ":" + startLine : "", - startColumn ? ":" + startColumn : "", - ), - ), + if (startLine) { + const line = lines[startLine - 1].split("").map((char, i) => { + const startCol = (props.startColumn ?? 0) - 1; + const endCol = (props.endColumn ?? Infinity) - 1; + + if (i >= startCol && i <= endCol) { + return color[severity === "error" ? "red" : "yellow"](char); + } else { + return char; + } + }).join(""); + + logs.push(...[ sprintf( - "%*s│ %s", - pad, - color.dim(String(startLine - 1)), + "%s│ %s", + color.dim(String(startLine - 1).padStart(pad, " ")), color.dim(lines[startLine - 2]), ), - sprintf("%*s│ %s", pad, color.bold(String(startLine)), line), sprintf( - "%*s│ %s", - pad, - color.dim(String(startLine + 1)), + "%s│ %s", + color.bold(String(startLine).padStart(pad, " ")), + line, + ), + sprintf( + "%s│ %s", + color.dim(String(startLine + 1).padStart(pad, " ")), color.dim(lines[startLine]), ), - sprintf( - "%*s╰─► %s", - pad, - "", - error, - ), - undefined, - ].join("\n"), + ]); + } + + logs.push( + sprintf( + "%*s╰─► %s", + pad, + "", + error, + ), ); + + console.log("\n" + logs.join("\n")); }; export const log = { diff --git a/scripts/utils.ts b/scripts/utils.ts index 19631760..373df74b 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -5,7 +5,7 @@ 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 { log } from "@/lint/logger.ts"; +import { log } from "@/logger.ts"; import { sprintf } from "@std/fmt/printf"; /** @@ -22,18 +22,20 @@ export const validateYaml = ( const data = parse(content); 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 "), + log.error( + 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 "), + { + file: "scripts/userstyles.yml", + }, ); Deno.exit(1); } @@ -56,14 +58,18 @@ export const getUserstylesData = (): Userstyles => { userStylesSchema, ); - if (data.userstyles === undefined || data.collaborators === undefined) { - console.log("userstyles.yml is missing required fields"); - Deno.exit(1); + for (const field of ["userstyles", "collaborators"] as const) { + if (data[field] === undefined) { + log.error(`Missing required field \`${field}\``, { + file: "scripts/userstyles.yml", + }); + Deno.exit(1); + } } return data as Userstyles; } catch (err) { - if (err.name === "YAMLError") { + if (err.name === "SyntaxError") { const groups = /(?.*) at line (?\d+), column (?\d+):[\S\s]*/ .exec(err.message)?.groups; @@ -76,7 +82,7 @@ export const getUserstylesData = (): Userstyles => { }, ); } else { - console.log(err); + throw err; } Deno.exit(1);