refactor(scripts): cleanup & enforce code style (#1388)

This commit is contained in:
uncenter authored and GitHub committed 2024-10-27 15:32:00 -04:00
1 parent 7db9b234c1
commit 5d74d700a2
23 files changed
+290 -270

No files matched your search

+14 -13
View File
@@ -1,8 +1,9 @@
import { exists } from "@std/fs";
import { join, relative } from "@std/path";
import { REPO_ROOT } from "@/constants.ts";
import * as fs from "@std/fs";
import * as path from "@std/path";
import core from "@actions/core";
import { REPO_ROOT } from "@/deps.ts";
import { log } from "@/logger.ts";
const requiredFiles = [
@@ -10,19 +11,19 @@ const requiredFiles = [
"preview.webp",
];
export const checkForMissingFiles = async () => {
const stylesRoot = join(REPO_ROOT, "styles");
export async function checkForMissingFiles() {
const stylesRoot = path.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);
const styleRoot = path.join(stylesRoot, entry.name);
await Promise.all(requiredFiles.map(async (f) => {
const fp = join(styleRoot, f);
const rfp = relative(REPO_ROOT, fp);
const fp = path.join(styleRoot, f);
const rfp = path.relative(REPO_ROOT, fp);
if (!(await exists(fp))) {
if (!(await fs.exists(fp))) {
missingFiles.push(rfp);
}
}));
@@ -35,10 +36,10 @@ export const checkForMissingFiles = async () => {
.addList(missingFiles)
.write();
} else {
missingFiles.map((f) => {
log.error(`File does not exist`, { file: f });
});
for (const file of missingFiles) {
log.error(`File does not exist`, { file });
}
}
return missingFiles.length === 0;
};
}
+7 -7
View File
@@ -1,11 +1,11 @@
#!/usr/bin/env -S deno run -A
import { walk } from "@std/fs";
import { REPO_ROOT } from "@/constants.ts";
import { parseArgs } from "@std/cli";
import { basename, dirname, join, relative } from "@std/path";
import * as fs from "@std/fs";
import * as path from "@std/path";
// @ts-types="npm:@types/less";
import less from "less";
import { REPO_ROOT } from "@/deps.ts";
import { checkForMissingFiles } from "@/lint/file-checker.ts";
import { log } from "@/logger.ts";
import { verifyMetadata } from "@/lint/metadata.ts";
@@ -15,7 +15,7 @@ import stylelintConfig from "../../.stylelintrc.js";
const args = parseArgs(Deno.args, { boolean: ["fix"] });
const subDir = args._[0]?.toString() ?? "";
const stylesheets = walk(join(REPO_ROOT, "styles", subDir), {
const stylesheets = fs.walk(path.join(REPO_ROOT, "styles", subDir), {
includeFiles: true,
includeDirs: false,
includeSymlinks: false,
@@ -26,8 +26,8 @@ const { userstyles } = getUserstylesData();
let failed = false;
for await (const entry of stylesheets) {
const dir = basename(dirname(entry.path));
const file = relative(REPO_ROOT, entry.path);
const dir = path.basename(path.dirname(entry.path));
const file = path.relative(REPO_ROOT, entry.path);
let content = await Deno.readTextFile(entry.path);
+25 -24
View File
@@ -1,27 +1,28 @@
import type { Userstyles } from "@/types/userstyles.d.ts";
import type { WalkEntry } from "@std/fs";
import { REPO_ROOT } from "@/constants.ts";
import * as color from "@std/fmt/colors";
import * as path from "@std/path";
import { sprintf } from "@std/fmt/printf";
// @ts-types="@/types/usercss-meta.d.ts";
import usercssMeta from "usercss-meta";
import * as color from "@std/fmt/colors";
import { sprintf } from "@std/fmt/printf";
import type { WalkEntry } from "@std/fs";
import { join, relative } from "@std/path";
import { REPO_ROOT } from "@/deps.ts";
import { log } from "@/logger.ts";
import { formatListOfItems } from "@/utils.ts";
import type { Userstyles } from "@/types/userstyles.d.ts";
export const verifyMetadata = async (
export async function verifyMetadata(
entry: WalkEntry,
content: string,
userstyle: string,
userstyles: Userstyles,
fix: boolean,
) => {
) {
// `usercss-meta` prohibits any '\r' characters, which seem to be present on Windows.
content = content.replaceAll("\r\n", "\n");
const assert = assertions(userstyle, userstyles);
const file = relative(REPO_ROOT, entry.path);
const assertions = generateAssertions(userstyle, userstyles);
const file = path.relative(REPO_ROOT, entry.path);
const { metadata, errors: parsingErrors } = usercssMeta.parse(content, {
allowErrors: true,
@@ -29,28 +30,28 @@ export const verifyMetadata = async (
const lines = content.split("\n");
// Pretty print / annotate the parsing errors.
parsingErrors.map((e) => {
for (const error of parsingErrors) {
let startLine;
if (e.index !== undefined && !Number.isNaN(e.index)) {
if (error.index !== undefined && !Number.isNaN(error.index)) {
startLine = 0;
for (const line of lines) {
startLine++;
e.index -= line.length + 1;
if (e.index < 0) break;
error.index -= line.length + 1;
if (error.index < 0) break;
}
}
// Skip "missing mandatory metadata property" ParseError, assertions checks below will cover.
if (e.code === "missingMandatory") return;
if (error.code === "missingMandatory") continue;
log.error(e.message, {
log.error(error.message, {
file,
startLine,
content,
});
});
}
for (const [key, expected] of Object.entries(assert)) {
for (const [key, expected] of Object.entries(assertions)) {
const current = metadata[key];
const atKey = "@" + key;
@@ -80,7 +81,7 @@ export const verifyMetadata = async (
}
const template = (await Deno.readTextFile(
join(REPO_ROOT, "template/catppuccin.user.css"),
path.join(REPO_ROOT, "template/catppuccin.user.css"),
))
.split("\n");
@@ -138,12 +139,12 @@ export const verifyMetadata = async (
return {
globalVars,
isLess: metadata.preprocessor === assert.preprocessor,
isLess: metadata.preprocessor === assertions.preprocessor,
fixed: content,
};
};
}
const assertions = (userstyle: string, userstyles: Userstyles) => {
function generateAssertions(userstyle: string, userstyles: Userstyles) {
const prefix = "https://github.com/catppuccin/userstyles";
if (!userstyles[userstyle]) {
@@ -175,4 +176,4 @@ const assertions = (userstyle: string, userstyles: Userstyles) => {
license: "MIT",
preprocessor: "less",
};
};
}
+35 -29
View File
@@ -1,44 +1,50 @@
import * as color from "@std/fmt/colors";
import type { WalkEntry } from "@std/fs";
import { relative } from "@std/path";
import { REPO_ROOT } from "@/constants.ts";
import * as color from "@std/fmt/colors";
import * as path from "@std/path";
import "postcss-less";
import stylelint from "stylelint";
import "stylelint-config-standard";
import "stylelint-config-recommended";
import { REPO_ROOT } from "@/deps.ts";
import { log } from "@/logger.ts";
export const lint = (
export async function lint(
entry: WalkEntry,
content: string,
fix: boolean,
config: stylelint.Config,
) =>
stylelint.lint({ code: content, config, fix })
.then(({ results, code }) => {
if (code) {
Deno.writeTextFileSync(entry.path, code);
}
results.map((result) => {
result.warnings.map((warning) => {
// Some cleanup for fancier logging - dims the rule name.
const message = warning.text?.replace(
new RegExp(`\\(?${warning.rule}\\)?`),
color.dim(`(${warning.rule})`),
) ?? "unspecified stylelint error";
) {
const { results, code } = await stylelint.lint({
code: content,
config,
fix,
});
log.log(message, {
file: relative(REPO_ROOT, entry.path),
startLine: warning.line,
endLine: warning.endLine,
startColumn: warning.column,
endColumn: warning.endColumn,
content,
}, warning.severity);
});
if (code) {
Deno.writeTextFileSync(entry.path, code);
}
if (result.warnings.length > 0) throw new Error("stylelint error");
});
});
for (const result of results) {
for (const warning of result.warnings) {
// Some cleanup for fancier logging - dims the rule name.
const message = warning.text?.replace(
new RegExp(`\\(?${warning.rule}\\)?`),
color.dim(`(${warning.rule})`),
) ?? "unspecified stylelint error";
log.log(message, {
file: path.relative(REPO_ROOT, entry.path),
startLine: warning.line,
endLine: warning.endLine,
startColumn: warning.column,
endColumn: warning.endColumn,
content,
}, warning.severity);
}
if (result.warnings.length > 0) throw new Error("stylelint error");
}
}