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
@@ -6,10 +6,13 @@
|
||||
"@catppuccin/palette": "npm:@catppuccin/palette@^1.7.1",
|
||||
"@octokit/rest": "npm:@octokit/rest@^21.0.2",
|
||||
"@std/assert": "jsr:@std/assert@^1.0.9",
|
||||
"@std/async": "jsr:@std/async@^1.2.0",
|
||||
"@std/cli": "jsr:@std/cli@^1.0.8",
|
||||
"@std/fmt": "jsr:@std/fmt@^1.0.2",
|
||||
"@std/fs": "jsr:@std/fs@^1.0.6",
|
||||
"@std/http": "jsr:@std/http@^1.0.25",
|
||||
"@std/path": "jsr:@std/path@^1.0.8",
|
||||
"@std/testing": "jsr:@std/testing@^1.0.17",
|
||||
"@std/yaml": "jsr:@std/yaml@^1.0.5",
|
||||
"ajv": "npm:[email protected]",
|
||||
"handlebars": "npm:[email protected]",
|
||||
@@ -31,7 +34,10 @@
|
||||
"ci:update-types": "deno run -A ./scripts/types/update-types.ts",
|
||||
"ci:bump": "deno run -A ./scripts/bump-version/main.ts",
|
||||
"lint": "deno run -A ./scripts/lint/main.ts",
|
||||
"lint:fix": "deno task lint --fix"
|
||||
"lint:fix": "deno task lint --fix",
|
||||
"serve": "deno run -A ./scripts/serve/main.ts",
|
||||
"test": "deno test -A",
|
||||
"test:update": "deno test -A -- --update"
|
||||
},
|
||||
"nodeModulesDir": "auto",
|
||||
"fmt": {
|
||||
|
||||
@@ -1,12 +1,30 @@
|
||||
{
|
||||
"version": "5",
|
||||
"specifiers": {
|
||||
"jsr:@std/assert@^1.0.9": "1.0.9",
|
||||
"jsr:@std/cli@^1.0.8": "1.0.8",
|
||||
"jsr:@std/fmt@^1.0.2": "1.0.3",
|
||||
"jsr:@std/fs@^1.0.6": "1.0.6",
|
||||
"jsr:@std/assert@^1.0.17": "1.0.19",
|
||||
"jsr:@std/assert@^1.0.9": "1.0.19",
|
||||
"jsr:@std/async@^1.1.0": "1.2.0",
|
||||
"jsr:@std/async@^1.2.0": "1.2.0",
|
||||
"jsr:@std/cli@^1.0.28": "1.0.28",
|
||||
"jsr:@std/cli@^1.0.8": "1.0.28",
|
||||
"jsr:@std/data-structures@^1.0.10": "1.0.10",
|
||||
"jsr:@std/encoding@^1.0.10": "1.0.10",
|
||||
"jsr:@std/fmt@^1.0.2": "1.0.9",
|
||||
"jsr:@std/fmt@^1.0.9": "1.0.9",
|
||||
"jsr:@std/fs@^1.0.22": "1.0.23",
|
||||
"jsr:@std/fs@^1.0.23": "1.0.23",
|
||||
"jsr:@std/fs@^1.0.6": "1.0.23",
|
||||
"jsr:@std/html@^1.0.5": "1.0.5",
|
||||
"jsr:@std/http@^1.0.25": "1.0.25",
|
||||
"jsr:@std/internal@^1.0.12": "1.0.12",
|
||||
"jsr:@std/internal@^1.0.5": "1.0.5",
|
||||
"jsr:@std/path@^1.0.8": "1.0.8",
|
||||
"jsr:@std/media-types@^1.1.0": "1.1.0",
|
||||
"jsr:@std/net@^1.0.6": "1.0.6",
|
||||
"jsr:@std/path@^1.0.8": "1.1.4",
|
||||
"jsr:@std/path@^1.1.4": "1.1.4",
|
||||
"jsr:@std/streams@^1.0.17": "1.0.17",
|
||||
"jsr:@std/testing@*": "1.0.17",
|
||||
"jsr:@std/testing@^1.0.17": "1.0.17",
|
||||
"jsr:@std/yaml@^1.0.5": "1.0.5",
|
||||
"npm:@actions/[email protected]": "1.11.1",
|
||||
"npm:@catppuccin/palette@^1.7.1": "1.7.1",
|
||||
@@ -31,27 +49,105 @@
|
||||
"@std/[email protected]": {
|
||||
"integrity": "a9f0c611a869cc791b26f523eec54c7e187aab7932c2c8e8bea0622d13680dcd",
|
||||
"dependencies": [
|
||||
"jsr:@std/internal"
|
||||
"jsr:@std/internal@^1.0.5"
|
||||
]
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "eaada96ee120cb980bc47e040f82814d786fe8162ecc53c91d8df60b8755991e",
|
||||
"dependencies": [
|
||||
"jsr:@std/internal@^1.0.12"
|
||||
]
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "c059c6f6d95ca7cc012ae8e8d7164d1697113d54b0b679e4372b354b11c2dee5"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "3762d8dc9a373715c08d871c38d45e637b25266f013a1d0bbe560bca409de94e"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "74ef9b976db59ca6b23a5283469c9072be6276853807a83ec6c7ce412135c70a",
|
||||
"dependencies": [
|
||||
"jsr:@std/fmt@^1.0.9",
|
||||
"jsr:@std/internal@^1.0.12"
|
||||
]
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "f574f86b0e07c69b9edc555fcc814b57d29258bad39fd5a34ba8a80ecf033cfe"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "8783c6384a2d13abd5e9e87a7ae0520a30e9f56aeeaa3bdf910a3eaaf5c811a1"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "97765c16aa32245ff4e2204ecf7d8562496a3cb8592340a80e7e554e0bb9149f"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "2487343e8899fb2be5d0e3d35013e54477ada198854e52dd05ed0422eddcabe0"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "42b56e1e41b75583a21d5a37f6a6a27de9f510bcd36c0c85791d685ca0b85fa2",
|
||||
"dependencies": [
|
||||
"jsr:@std/path"
|
||||
"jsr:@std/path@^1.0.8"
|
||||
]
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "3ecbae4ce4fee03b180fa710caff36bb5adb66631c46a6460aaad49515565a37",
|
||||
"dependencies": [
|
||||
"jsr:@std/internal@^1.0.12",
|
||||
"jsr:@std/path@^1.1.4"
|
||||
]
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "4e2d693f474cae8c16a920fa5e15a3b72267b94b84667f11a50c6dd1cb18d35e"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "577b4252290af1097132812b339fffdd55fb0f4aeb98ff11bdbf67998aa17193",
|
||||
"dependencies": [
|
||||
"jsr:@std/cli@^1.0.28",
|
||||
"jsr:@std/encoding",
|
||||
"jsr:@std/fmt@^1.0.9",
|
||||
"jsr:@std/fs@^1.0.23",
|
||||
"jsr:@std/html",
|
||||
"jsr:@std/media-types",
|
||||
"jsr:@std/net",
|
||||
"jsr:@std/path@^1.1.4",
|
||||
"jsr:@std/streams"
|
||||
]
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "54a546004f769c1ac9e025abd15a76b6671ddc9687e2313b67376125650dc7ba"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "972a634fd5bc34b242024402972cd5143eac68d8dffaca5eaa4dba30ce17b027"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "c9d093f0c05c3512932b330e3cc1fe1d627b301db33a4c2c2185c02471d6eaa4"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "110735f93e95bb9feb95790a8b1d1bf69ec0dc74f3f97a00a76ea5efea25500c"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "548fa456bb6a04d3c1a1e7477986b6cffbce95102d0bb447c67c4ee70e0364be"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "1d2d43f39efb1b42f0b1882a25486647cb851481862dc7313390b2bb044314b5",
|
||||
"dependencies": [
|
||||
"jsr:@std/internal@^1.0.12"
|
||||
]
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "7859f3d9deed83cf4b41f19223d4a67661b3d3819e9fc117698f493bf5992140"
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "87bdc2700fa98249d48a17cd72413352d3d3680dcfbdb64947fd0982d6bbf681",
|
||||
"dependencies": [
|
||||
"jsr:@std/assert@^1.0.17",
|
||||
"jsr:@std/async@^1.1.0",
|
||||
"jsr:@std/data-structures",
|
||||
"jsr:@std/fs@^1.0.22",
|
||||
"jsr:@std/internal@^1.0.12",
|
||||
"jsr:@std/path@^1.1.4"
|
||||
]
|
||||
},
|
||||
"@std/[email protected]": {
|
||||
"integrity": "71ba3d334305ee2149391931508b2c293a8490f94a337eef3a09cade1a2a2742"
|
||||
}
|
||||
@@ -1032,10 +1128,13 @@
|
||||
"workspace": {
|
||||
"dependencies": [
|
||||
"jsr:@std/assert@^1.0.9",
|
||||
"jsr:@std/async@^1.2.0",
|
||||
"jsr:@std/cli@^1.0.8",
|
||||
"jsr:@std/fmt@^1.0.2",
|
||||
"jsr:@std/fs@^1.0.6",
|
||||
"jsr:@std/http@^1.0.25",
|
||||
"jsr:@std/path@^1.0.8",
|
||||
"jsr:@std/testing@^1.0.17",
|
||||
"jsr:@std/yaml@^1.0.5",
|
||||
"npm:@actions/[email protected]",
|
||||
"npm:@catppuccin/palette@^1.7.1",
|
||||
|
||||
+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