feat(scripts/lint): intercept lib http imports for local linting (#2292)
This commit is contained in:
2 files changed
+52
-12
No files matched your search
@@ -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<string, Promise<string>>();
|
||||||
|
|
||||||
|
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<Less.FileLoadResult> {
|
||||||
|
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(),
|
||||||
|
);
|
||||||
+1
-12
@@ -12,6 +12,7 @@ import { verifyMetadata } from "@/lint/metadata.ts";
|
|||||||
import { runStylelint } from "@/lint/stylelint.ts";
|
import { runStylelint } from "@/lint/stylelint.ts";
|
||||||
import { getUserstylesData, getUserstylesFiles } from "@/utils.ts";
|
import { getUserstylesData, getUserstylesFiles } from "@/utils.ts";
|
||||||
import stylelintConfig from "../../.stylelintrc.js";
|
import stylelintConfig from "../../.stylelintrc.js";
|
||||||
|
import "@/lint/library.ts";
|
||||||
|
|
||||||
const args = parseArgs(Deno.args, { boolean: ["fix"] });
|
const args = parseArgs(Deno.args, { boolean: ["fix"] });
|
||||||
const userstyle = args._[0]?.toString().match(
|
const userstyle = args._[0]?.toString().match(
|
||||||
@@ -24,19 +25,12 @@ const stylesheets = userstyle
|
|||||||
const { userstyles } = getUserstylesData();
|
const { userstyles } = getUserstylesData();
|
||||||
|
|
||||||
let didLintFail = false;
|
let didLintFail = false;
|
||||||
const patches = [
|
|
||||||
["https://userstyles.catppuccin.com/lib", path.join(REPO_ROOT, "lib")],
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const style of stylesheets) {
|
for (const style of stylesheets) {
|
||||||
const dir = path.basename(path.dirname(style));
|
const dir = path.basename(path.dirname(style));
|
||||||
const file = path.relative(REPO_ROOT, style);
|
const file = path.relative(REPO_ROOT, style);
|
||||||
|
|
||||||
let content = await Deno.readTextFile(style);
|
let content = await Deno.readTextFile(style);
|
||||||
// Apply patches.
|
|
||||||
for (const [search, replace] of patches) {
|
|
||||||
content = content.replaceAll(search, replace);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify the UserCSS metadata.
|
// Verify the UserCSS metadata.
|
||||||
const { globalVars, isLess, fixed } = await verifyMetadata(
|
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.
|
// Lint with Stylelint.
|
||||||
const results = await runStylelint(style, content, args.fix, stylelintConfig)
|
const results = await runStylelint(style, content, args.fix, stylelintConfig)
|
||||||
.catch(() => didLintFail = true);
|
.catch(() => didLintFail = true);
|
||||||
|
|||||||
Reference in new issue
Block a user