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 { 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 });
});
}
+1 -1
View File
@@ -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";
+31 -15
View File
@@ -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);
}
+1 -1
View File
@@ -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,
+47 -37
View File
@@ -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 = {
+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 { 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 = <T>(
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 =
/(?<message>.*) at line (?<line>\d+), column (?<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);