Skip to content

Commit

Permalink
ci(lint): refactor & handle more errors (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekowinston authored Nov 18, 2023
1 parent 17c647d commit 610e977
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 156 deletions.
3 changes: 0 additions & 3 deletions src/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"imports": {
"@/": "./",
"std/": "https://deno.land/[email protected]/",

"catppuccin-repo/": "https://raw.githubusercontent.com/catppuccin/catppuccin/91d6b5433730103c504dcf43583b594e418ee7c1/",
"globber": "https://deno.land/x/[email protected]/mod.ts",

"@actions/core": "npm:@actions/[email protected]",
"@octokit/rest": "npm:@octokit/[email protected]",
"usercss-meta": "npm:[email protected]",
Expand Down
45 changes: 22 additions & 23 deletions src/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions src/lint/file-checker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { exists } from "std/fs/mod.ts";
import { join, relative } from "std/path/mod.ts";
import core from "@actions/core";

import { REPO_ROOT } from "@/deps.ts";
import { log } from "./logger.ts";
import chalk from "chalk";

const requiredFiles = [
"catppuccin.user.css",
...["latte", "frappe", "macchiato", "mocha", "catwalk"].map((f) =>
join("assets", `${f}.webp`)
),
];

export const checkForMissingFiles = async () => {
const stylesRoot = join(REPO_ROOT, "styles");

const missingFiles: string[] = [];
for await (const entry of Deno.readDir(stylesRoot)) {
if (!entry.isDirectory) continue;
const styleRoot = join(stylesRoot, entry.name);

await Promise.all(requiredFiles.map(async (f) => {
const fp = join(styleRoot, f);
const rfp = relative(REPO_ROOT, fp);

if (!(await exists(fp))) {
missingFiles.push(rfp);
}
}));
}

// only write summary if running in github actions
if (Deno.env.has("GITHUB_ACTIONS")) {
await core.summary
.addHeading("Missing files")
.addList(missingFiles)
.write();
} else {
missingFiles.map((f) => {
log(chalk.red(`Missing file:`) + ` ${f}`, { file: f }, "error");
});
}

return missingFiles.length === 0;
};
39 changes: 30 additions & 9 deletions src/lint/logger.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { sprintf } from "std/fmt/printf.ts";
import chalk from "chalk";
import core from "@actions/core";

export type LoggerProps = core.AnnotationProperties & {
content?: string;
};
export type LoggerProps = core.AnnotationProperties & { content?: string };

const pretty_print = (
message: string,
props: LoggerProps,
severity: "error" | "warning" = "warning",
) => {
const startLine = props.startLine ?? 0;
const { file, startColumn, startLine } = props;
if (!startLine) return console.log(message);

const lines = (props.content ?? "").split("\n");

const error = [
Expand All @@ -29,13 +30,33 @@ const pretty_print = (
}
}).join("");

const pad = startLine.toString().length;
console.log(
[
`${props.file}:${chalk.grey(`${props.startLine}:${props.startColumn}`)}`,
` ┃${chalk.dim(lines[startLine - 2])}`,
` ┃${chalk.grey(line)}`,
` ┃${chalk.dim(lines[startLine])}`,
error,
chalk.underline(
sprintf(
"%s%s%d%s%d",
file,
startLine ? ":" : "",
startLine ?? "",
startColumn ? ":" : "",
startColumn ?? "",
),
),
sprintf(
"%*s│ %s",
pad,
chalk.dim(startLine - 1),
chalk.dim(lines[startLine - 2]),
),
sprintf("%*s│ %s", pad, chalk.bold(startLine), line),
sprintf(
"%*s│ %s",
pad,
chalk.dim(startLine + 1),
chalk.dim(lines[startLine]),
),
sprintf("%*s╰─► %s", pad, "", error),
undefined,
].join("\n"),
);
Expand Down
Loading

0 comments on commit 610e977

Please sign in to comment.