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 });
});
}
-104
View File
@@ -1,104 +0,0 @@
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 };
const prettyPrint = (
message: string,
props: LoggerProps,
severity: "error" | "warning" = "warning",
) => {
const { file, startColumn, startLine } = props;
if (!startLine) return console.log(message);
const lines = (props.content ?? "").split("\n");
const error = [
color[severity === "error" ? "red" : "yellow"](severity),
message,
].join(" ");
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("");
const pad = startLine.toString().length;
console.log(
[
color.underline(
sprintf(
"%s%s%s",
file,
startLine ? ":" + startLine : "",
startColumn ? ":" + startColumn : "",
),
),
sprintf(
"%*s│ %s",
pad,
color.dim(String(startLine - 1)),
color.dim(lines[startLine - 2]),
),
sprintf("%*s│ %s", pad, color.bold(String(startLine)), line),
sprintf(
"%*s│ %s",
pad,
color.dim(String(startLine + 1)),
color.dim(lines[startLine]),
),
sprintf(
"%*s╰─► %s",
pad,
"",
error,
),
undefined,
].join("\n"),
);
};
export const log = {
failed: false,
log: function (
message: string,
props: LoggerProps,
severity: "error" | "warning",
) {
if (severity === "error") this.failed = true;
if (Deno.env.has("CI")) {
switch (severity) {
case "error":
core.error(message, props);
break;
case "warning":
core.warning(message, props);
break;
}
} else {
prettyPrint(message, props, severity);
}
},
warn: function (
message: string,
props: LoggerProps,
) {
this.log(message, props, "warning");
},
error: function (
message: string,
props: LoggerProps,
) {
this.log(message, props, "error");
},
};
+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,