feat: improve scripts/lint logging (#1305)

This commit is contained in:
uncenter authored and GitHub committed 2024-09-13 17:10:03 -04:00
1 parent 4de6cc6071
commit c5d639d694
6 files changed
+106 -75

No files matched your search

+2 -3
View File
@@ -3,8 +3,7 @@ import { join, relative } from "@std/path";
import core from "@actions/core"; import core from "@actions/core";
import { REPO_ROOT } from "@/deps.ts"; import { REPO_ROOT } from "@/deps.ts";
import { log } from "@/lint/logger.ts"; import { log } from "@/logger.ts";
import * as color from "@std/fmt/colors";
const requiredFiles = [ const requiredFiles = [
"catppuccin.user.css", "catppuccin.user.css",
@@ -37,7 +36,7 @@ export const checkForMissingFiles = async () => {
.write(); .write();
} else { } else {
missingFiles.map((f) => { missingFiles.map((f) => {
log.error(color.red(`Missing file:`) + ` ${f}`, { file: f }); log.error(`File does not exist`, { file: f });
}); });
} }
+1 -1
View File
@@ -7,7 +7,7 @@ import less from "less";
import { REPO_ROOT } from "@/deps.ts"; import { REPO_ROOT } from "@/deps.ts";
import { checkForMissingFiles } from "@/lint/file-checker.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 { verifyMetadata } from "@/lint/metadata.ts";
import { lint } from "@/lint/stylelint.ts"; import { lint } from "@/lint/stylelint.ts";
import { getUserstylesData } from "@/utils.ts"; import { getUserstylesData } from "@/utils.ts";
+31 -15
View File
@@ -6,7 +6,7 @@ import type { WalkEntry } from "@std/fs";
import { join, relative } from "@std/path"; import { join, relative } from "@std/path";
import { REPO_ROOT } from "@/deps.ts"; import { REPO_ROOT } from "@/deps.ts";
import { log } from "@/lint/logger.ts"; import { log } from "@/logger.ts";
import { formatListOfItems } from "@/utils.ts"; import { formatListOfItems } from "@/utils.ts";
import type { Userstyles } from "@/types/userstyles.d.ts"; import type { Userstyles } from "@/types/userstyles.d.ts";
@@ -30,30 +30,43 @@ export const verifyMetadata = async (
// Pretty print / annotate the parsing errors. // Pretty print / annotate the parsing errors.
parsingErrors.map((e) => { parsingErrors.map((e) => {
let startLine = 0; let startLine;
for (const line of lines) { if (e.index !== undefined && !Number.isNaN(e.index)) {
startLine++; startLine = 0;
e.index -= line.length + 1; for (const line of lines) {
if (e.index < 0) break; 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)) { for (const [key, expected] of Object.entries(assert)) {
const current = metadata[key]; const current = metadata[key];
const atKey = "@" + key;
if (current !== expected) { if (current !== expected) {
const line = lines const line = lines
.findIndex((line) => line.includes(key)) + 1; .findIndex((line) => line.includes(`${atKey} `)) + 1;
const message = current === undefined const message = current === undefined
? sprintf( ? sprintf(
"Metadata `%s` should not be undefined", "UserCSS metadata property `%s` is undefined",
color.bold(key), color.bold(atKey),
) )
: sprintf( : sprintf(
'Metadata `%s` should be "%s" but is "%s"', 'UserCSS metadata property `%s` should be "%s" but is "%s"',
color.bold(key), color.bold(atKey),
color.green(expected), color.green(expected),
color.red(String(current)), color.red(String(current)),
); );
@@ -134,9 +147,12 @@ const assertions = (userstyle: string, userstyles: Userstyles) => {
const prefix = "https://github.com/catppuccin/userstyles"; const prefix = "https://github.com/catppuccin/userstyles";
if (!userstyles[userstyle]) { if (!userstyles[userstyle]) {
log.error("Metadata section for this userstyle has not been added", { log.error(
file: "scripts/userstyles.yml", `Metadata section for \`${color.bold(userstyle)}\` userstyle is missing`,
}); {
file: "scripts/userstyles.yml",
},
);
Deno.exit(1); Deno.exit(1);
} }
+1 -1
View File
@@ -8,7 +8,7 @@ import "stylelint-config-standard";
import "stylelint-config-recommended"; import "stylelint-config-recommended";
import { REPO_ROOT } from "@/deps.ts"; import { REPO_ROOT } from "@/deps.ts";
import { log } from "@/lint/logger.ts"; import { log } from "@/logger.ts";
export const lint = ( export const lint = (
entry: WalkEntry, entry: WalkEntry,
+47 -37
View File
@@ -2,7 +2,10 @@ import { sprintf } from "@std/fmt/printf";
import * as color from "@std/fmt/colors"; import * as color from "@std/fmt/colors";
import core from "@actions/core"; import core from "@actions/core";
export type LoggerProps = core.AnnotationProperties & { content?: string }; export type LoggerProps = core.AnnotationProperties & {
file: string;
content?: string;
};
const prettyPrint = ( const prettyPrint = (
message: string, message: string,
@@ -10,7 +13,6 @@ const prettyPrint = (
severity: "error" | "warning" = "warning", severity: "error" | "warning" = "warning",
) => { ) => {
const { file, startColumn, startLine } = props; const { file, startColumn, startLine } = props;
if (!startLine) return console.log(message);
const lines = (props.content ?? "").split("\n"); const lines = (props.content ?? "").split("\n");
@@ -19,50 +21,58 @@ const prettyPrint = (
message, message,
].join(" "); ].join(" ");
const line = lines[startLine - 1].split("").map((char, i) => { const pad = startLine ? (startLine + 1).toString().length : 0;
const startCol = (props.startColumn ?? 0) - 1;
const endCol = (props.endColumn ?? Infinity) - 1;
if (i >= startCol && i <= endCol) { const logs = [color.underline(
return color[severity === "error" ? "red" : "yellow"](char); sprintf(
} else { "%s%s%s",
return char; file,
} startLine ? ":" + startLine : "",
}).join(""); startColumn ? ":" + startColumn : "",
),
)];
const pad = startLine.toString().length; if (startLine) {
console.log( const line = lines[startLine - 1].split("").map((char, i) => {
[ const startCol = (props.startColumn ?? 0) - 1;
color.underline( const endCol = (props.endColumn ?? Infinity) - 1;
sprintf(
"%s%s%s", if (i >= startCol && i <= endCol) {
file, return color[severity === "error" ? "red" : "yellow"](char);
startLine ? ":" + startLine : "", } else {
startColumn ? ":" + startColumn : "", return char;
), }
), }).join("");
logs.push(...[
sprintf( sprintf(
"%*s│ %s", "%s│ %s",
pad, color.dim(String(startLine - 1).padStart(pad, " ")),
color.dim(String(startLine - 1)),
color.dim(lines[startLine - 2]), color.dim(lines[startLine - 2]),
), ),
sprintf("%*s│ %s", pad, color.bold(String(startLine)), line),
sprintf( sprintf(
"%*s│ %s", "%s│ %s",
pad, color.bold(String(startLine).padStart(pad, " ")),
color.dim(String(startLine + 1)), line,
),
sprintf(
"%s│ %s",
color.dim(String(startLine + 1).padStart(pad, " ")),
color.dim(lines[startLine]), color.dim(lines[startLine]),
), ),
sprintf( ]);
"%*s╰─► %s", }
pad,
"", logs.push(
error, sprintf(
), "%*s╰─► %s",
undefined, pad,
].join("\n"), "",
error,
),
); );
console.log("\n" + logs.join("\n"));
}; };
export const log = { export const log = {
+24 -18
View File
@@ -5,7 +5,7 @@ import { SetRequired } from "type-fest/source/set-required.d.ts";
import { REPO_ROOT, userStylesSchema } from "@/deps.ts"; import { REPO_ROOT, userStylesSchema } from "@/deps.ts";
import { UserstylesSchema } from "@/types/userstyles.d.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"; import { sprintf } from "@std/fmt/printf";
/** /**
@@ -22,18 +22,20 @@ export const validateYaml = <T>(
const data = parse(content); const data = parse(content);
if (!validate(data)) { if (!validate(data)) {
console.log( log.error(
"Found schema errors in scripts/userstyles.yml: " + validate.errors!.map((err) =>
validate.errors?.map((err) => sprintf(
sprintf( "%s %s%s",
"%s %s%s", err.instancePath.slice(1).replaceAll("/", "."),
err.instancePath.slice(1).replaceAll("/", "."), err.message,
err.message, err.params.allowedValues
err.params.allowedValues ? ` (${JSON.stringify(err.params.allowedValues, undefined)})`
? ` (${JSON.stringify(err.params.allowedValues, undefined)})` : "",
: "", )
) ).join(" and "),
).join(" and "), {
file: "scripts/userstyles.yml",
},
); );
Deno.exit(1); Deno.exit(1);
} }
@@ -56,14 +58,18 @@ export const getUserstylesData = (): Userstyles => {
userStylesSchema, userStylesSchema,
); );
if (data.userstyles === undefined || data.collaborators === undefined) { for (const field of ["userstyles", "collaborators"] as const) {
console.log("userstyles.yml is missing required fields"); if (data[field] === undefined) {
Deno.exit(1); log.error(`Missing required field \`${field}\``, {
file: "scripts/userstyles.yml",
});
Deno.exit(1);
}
} }
return data as Userstyles; return data as Userstyles;
} catch (err) { } catch (err) {
if (err.name === "YAMLError") { if (err.name === "SyntaxError") {
const groups = const groups =
/(?<message>.*) at line (?<line>\d+), column (?<column>\d+):[\S\s]*/ /(?<message>.*) at line (?<line>\d+), column (?<column>\d+):[\S\s]*/
.exec(err.message)?.groups; .exec(err.message)?.groups;
@@ -76,7 +82,7 @@ export const getUserstylesData = (): Userstyles => {
}, },
); );
} else { } else {
console.log(err); throw err;
} }
Deno.exit(1); Deno.exit(1);