From 79ac3e24cb91a2a5a9579fdf634961836bac1770 Mon Sep 17 00:00:00 2001 From: leah Date: Mon, 20 Jul 2026 14:38:07 -0400 Subject: [PATCH] feat(scripts/lint): intercept lib http imports for local linting (#2292) --- scripts/lint/library.ts | 51 +++++++++++++++++++++++++++++++++++++++++ scripts/lint/main.ts | 13 +---------- 2 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 scripts/lint/library.ts diff --git a/scripts/lint/library.ts b/scripts/lint/library.ts new file mode 100644 index 00000000..e632f86a --- /dev/null +++ b/scripts/lint/library.ts @@ -0,0 +1,51 @@ +import * as path from "@std/path"; +// @ts-types="npm:@types/less"; +import less from "less"; + +import { REPO_ROOT } from "@/constants.ts"; + +const LIB_URL_PREFIX = "https://userstyles.catppuccin.com/lib"; +const LIB_DIRECTORY = path.join(REPO_ROOT, "lib"); + +class InterceptingFileManager extends less.FileManager { + #cache = new Map>(); + + override supports(filename: string) { + return filename.startsWith(LIB_URL_PREFIX); + } + override supportsSync() { + return false; + } + + override async loadFile( + filename: string, + _currentDirectory: string, + _options: Less.LoadFileOptions, + _environment: Less.Environment, + ): Promise { + const relativePath = filename.slice(LIB_URL_PREFIX.length); + const localPath = path.join(LIB_DIRECTORY, relativePath); + + let cached = this.#cache.get(localPath); + if (!cached) { + cached = Deno.readTextFile(localPath).catch(() => { + throw { + type: "File", + message: `${filename} failed: not found at ${localPath}`, + }; + }); + this.#cache.set(localPath, cached); + } + + return { contents: await cached, filename: localPath }; + } +} + +interface LessEnvironment { + addFileManager(fileManager: Less.FileManager): void; +} + +(less as unknown as { environment: LessEnvironment }).environment + .addFileManager( + new InterceptingFileManager(), + ); diff --git a/scripts/lint/main.ts b/scripts/lint/main.ts index 66ed73e9..2e6312d4 100755 --- a/scripts/lint/main.ts +++ b/scripts/lint/main.ts @@ -12,6 +12,7 @@ import { verifyMetadata } from "@/lint/metadata.ts"; import { runStylelint } from "@/lint/stylelint.ts"; import { getUserstylesData, getUserstylesFiles } from "@/utils.ts"; import stylelintConfig from "../../.stylelintrc.js"; +import "@/lint/library.ts"; const args = parseArgs(Deno.args, { boolean: ["fix"] }); const userstyle = args._[0]?.toString().match( @@ -24,19 +25,12 @@ const stylesheets = userstyle const { userstyles } = getUserstylesData(); let didLintFail = false; -const patches = [ - ["https://userstyles.catppuccin.com/lib", path.join(REPO_ROOT, "lib")], -]; for (const style of stylesheets) { const dir = path.basename(path.dirname(style)); const file = path.relative(REPO_ROOT, style); let content = await Deno.readTextFile(style); - // Apply patches. - for (const [search, replace] of patches) { - content = content.replaceAll(search, replace); - } // Verify the UserCSS metadata. const { globalVars, isLess, fixed } = await verifyMetadata( @@ -63,11 +57,6 @@ for (const style of stylesheets) { }, ); - // Reverse apply patches. - for (const [search, replace] of patches) { - content = content.replaceAll(replace, search); - } - // Lint with Stylelint. const results = await runStylelint(style, content, args.fix, stylelintConfig) .catch(() => didLintFail = true);