fix(lib,scripts): import locally for linting, snapshot tests, dev server (#2172)
Co-authored-by: WalkQuackBack <[email protected]>
This commit is contained in:
1 parent
dc1fac9639
commit
acd47ee987
8 files changed
+302
-18
No files matched your search
+15
-3
@@ -24,12 +24,19 @@ 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(
|
||||
@@ -56,10 +63,15 @@ for (const style of stylesheets) {
|
||||
},
|
||||
);
|
||||
|
||||
// Reverse apply patches.
|
||||
for (const [search, replace] of patches) {
|
||||
content = content.replaceAll(replace, search);
|
||||
}
|
||||
|
||||
// Lint with Stylelint.
|
||||
await runStylelint(style, content, args.fix, stylelintConfig).catch(() =>
|
||||
didLintFail = true
|
||||
);
|
||||
const results = await runStylelint(style, content, args.fix, stylelintConfig)
|
||||
.catch(() => didLintFail = true);
|
||||
Deno.writeTextFileSync(file, typeof results === "string" ? results : content);
|
||||
}
|
||||
|
||||
if (await checkForMissingFiles() === false) didLintFail = true;
|
||||
|
||||
@@ -85,8 +85,6 @@ export async function verifyMetadata(
|
||||
}
|
||||
}
|
||||
|
||||
Deno.writeTextFileSync(file, content);
|
||||
|
||||
const template = (await Deno.readTextFile(
|
||||
path.join(REPO_ROOT, "template/catppuccin.user.less"),
|
||||
))
|
||||
|
||||
@@ -12,17 +12,13 @@ export async function runStylelint(
|
||||
content: string,
|
||||
fix: boolean,
|
||||
config: stylelint.Config,
|
||||
) {
|
||||
): Promise<string | undefined> {
|
||||
const { results, code } = await stylelint.lint({
|
||||
code: content,
|
||||
config,
|
||||
fix,
|
||||
});
|
||||
|
||||
if (code) {
|
||||
Deno.writeTextFileSync(file, code);
|
||||
}
|
||||
|
||||
for (const result of results) {
|
||||
for (const warning of result.warnings) {
|
||||
// Some cleanup for fancier logging - dims the rule name.
|
||||
@@ -43,4 +39,6 @@ export async function runStylelint(
|
||||
|
||||
if (result.warnings.length > 0) throw new Error("stylelint error");
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
import { parseArgs } from "@std/cli";
|
||||
import { serveDir } from "@std/http/file-server";
|
||||
import { debounce } from "@std/async/debounce";
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import * as path from "@std/path";
|
||||
|
||||
import { REPO_ROOT } from "@/constants.ts";
|
||||
|
||||
const args = parseArgs(Deno.args, {});
|
||||
const userstyle = args._[0]
|
||||
?.toString()
|
||||
.match(
|
||||
/(?<base>styles\/)?(?<userstyle>[a-z0-9_\-.]+)(?<trailing>\/)?(?<file>catppuccin\.user\.less)?/,
|
||||
)?.groups?.userstyle;
|
||||
if (!userstyle) throw new Error("Invalid userstyle argument");
|
||||
|
||||
const server = Deno.serve({
|
||||
onListen() {
|
||||
// Disable unnecessary post-startup log.
|
||||
},
|
||||
}, (req: Request) => {
|
||||
const pathname = new URL(req.url).pathname;
|
||||
|
||||
if (pathname.startsWith("/lib")) {
|
||||
return serveDir(req, {
|
||||
fsRoot: "lib",
|
||||
urlRoot: "lib",
|
||||
});
|
||||
}
|
||||
|
||||
return new Response("404: Not Found", {
|
||||
status: 404,
|
||||
});
|
||||
});
|
||||
|
||||
const userstylePath = path.join(
|
||||
REPO_ROOT,
|
||||
"styles",
|
||||
userstyle,
|
||||
"catppuccin.user.less",
|
||||
);
|
||||
|
||||
const libPath = path.join(
|
||||
REPO_ROOT,
|
||||
"lib",
|
||||
);
|
||||
|
||||
const tempUserstylePath = await Deno.makeTempFile({
|
||||
prefix: "userstyle_",
|
||||
suffix: ".user.less",
|
||||
});
|
||||
|
||||
async function calculateLibChecksum(): Promise<string> {
|
||||
const files = [];
|
||||
for await (const entry of Deno.readDir(libPath)) {
|
||||
if (entry.isFile) {
|
||||
files.push(path.join(libPath, entry.name));
|
||||
}
|
||||
}
|
||||
|
||||
const hash = createHash("sha256");
|
||||
for (const file of files) {
|
||||
const content = await Deno.readTextFile(file);
|
||||
hash.update(content);
|
||||
}
|
||||
|
||||
return hash.digest("hex");
|
||||
}
|
||||
|
||||
let lastLibChecksum = await calculateLibChecksum();
|
||||
|
||||
async function updateTempUserstyle() {
|
||||
let contents = await Deno.readTextFile(userstylePath);
|
||||
/* Appends a query parameter suffix to import URLs from userstyles.catppuccin.com, containing the library modules content checksum. */
|
||||
const importRegex =
|
||||
/(@import\s+"https:\/\/userstyles\.catppuccin\.com\/lib\/[^\s]+\.less")/g;
|
||||
contents = contents.replace(importRegex, (match) => {
|
||||
return match.replace(
|
||||
/(\.less)/,
|
||||
`$1?v=${lastLibChecksum.slice(0, 6)}`,
|
||||
);
|
||||
});
|
||||
|
||||
/* Then replace the remote userstyles.catppuccin.com host with the local(host) server URL for these imports. */
|
||||
contents = contents.replaceAll(
|
||||
"https://userstyles.catppuccin.com",
|
||||
`http://localhost:${server.addr.port}`,
|
||||
);
|
||||
|
||||
await Deno.writeTextFile(tempUserstylePath, contents);
|
||||
}
|
||||
|
||||
updateTempUserstyle();
|
||||
|
||||
console.log(`[serve] ${userstyle} userstyle served at '${tempUserstylePath}'`);
|
||||
|
||||
const reload = debounce(() => {
|
||||
console.log(
|
||||
`[serve]: reloaded ${userstyle} userstyle`,
|
||||
);
|
||||
updateTempUserstyle();
|
||||
}, 200);
|
||||
|
||||
const watcher = Deno.watchFs([userstylePath, libPath]);
|
||||
for await (const event of watcher) {
|
||||
if (event.paths.some((path) => path.startsWith(libPath))) {
|
||||
lastLibChecksum = await calculateLibChecksum();
|
||||
}
|
||||
reload();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
export const snapshot = {};
|
||||
|
||||
snapshot[`#lib.rgbify 1`] = `
|
||||
"/* deno-fmt-ignore */
|
||||
/* deno-fmt-ignore */
|
||||
.foo {
|
||||
--bar: 255, 0, 0;
|
||||
}
|
||||
"
|
||||
`;
|
||||
|
||||
snapshot[`#lib.hslify 1`] = `
|
||||
"/* deno-fmt-ignore */
|
||||
/* deno-fmt-ignore */
|
||||
.foo {
|
||||
--bar: 0, 100%, 50%;
|
||||
}
|
||||
"
|
||||
`;
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { assertSnapshot } from "@std/testing/snapshot";
|
||||
|
||||
import { REPO_ROOT } from "../constants.ts";
|
||||
|
||||
// @ts-types="npm:@types/less";
|
||||
import less from "less";
|
||||
|
||||
async function compile(source: string) {
|
||||
const result = await less.render(
|
||||
`
|
||||
@import "lib/lib.less";
|
||||
|
||||
${source}
|
||||
`,
|
||||
{ paths: [REPO_ROOT] },
|
||||
);
|
||||
return result.css;
|
||||
}
|
||||
|
||||
Deno.test("#lib.rgbify", async (ctx) => {
|
||||
await assertSnapshot(
|
||||
ctx,
|
||||
await compile(`
|
||||
.foo {
|
||||
--bar: #lib.rgbify(red)[];
|
||||
}
|
||||
`),
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("#lib.hslify", async (ctx) => {
|
||||
await assertSnapshot(
|
||||
ctx,
|
||||
await compile(`
|
||||
.foo {
|
||||
--bar: #lib.hslify(red)[];
|
||||
}
|
||||
`),
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user