diff --git a/web/.husky/check-staged.mjs b/web/.husky/check-staged.mjs new file mode 100644 index 00000000..eadc10b9 --- /dev/null +++ b/web/.husky/check-staged.mjs @@ -0,0 +1,184 @@ +import { spawnSync } from "node:child_process"; +import { lstatSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; + +const webRoot = resolve(import.meta.dirname, ".."); +const root = resolve(webRoot, ".."); +const theme = "web/src/lib/preferences/theme.ts"; +const generator = "web/src/scripts/generate-theme-presets.ts"; +const text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }); +const forGit = (paths) => `${paths.join("\0")}\0`; + +function run(command, args, { status = 0, ...options } = {}) { + const result = spawnSync(command, args, { + cwd: root, + stdio: ["pipe", "pipe", "inherit"], + maxBuffer: 32 * 1024 * 1024, + ...options, + }); + if (result.error) throw result.error; + if (result.status !== status) throw new Error(`${command} ${args[0]} failed (${result.signal ?? result.status})`); + return result.stdout; +} + +function git(args, input) { + return run("git", args, { input }); +} + +function records(output) { + return text.decode(output).split("\0").slice(0, -1); +} + +function indexedFiles() { + return git(["ls-files", "--stage", "-z", "--", "web/", ".gitignore", ".editorconfig"]); +} + +function isThemeInput(path) { + return ( + path === theme || + path === generator || + path === "web/biome.json" || + path === "web/tsconfig.json" || + path === "web/tsconfig.scripts.json" || + path === "web/src/app/globals.css" || + /^web\/src\/styles\/presets\/[^/]+\.css$/.test(path) + ); +} + +function checkCommitIndex() { + if (!process.env.GIT_INDEX_FILE) return; + const args = ["rev-parse", "--path-format=absolute", "--git-path", "index"]; + const { GIT_INDEX_FILE, ...env } = process.env; + const active = git(args).toString().trimEnd(); + const normal = run("git", args, { env }).toString().trimEnd(); + // Git publishes index.lock for a normal tracked-files commit. Path-limited + // commits instead publish a different index from the one the hook receives. + if (active !== normal && active !== `${normal}.lock`) { + throw new Error("Staged fixes need the normal Git index. Stage the paths and run git commit without paths."); + } +} + +function formattingPatch(snapshot, path, original) { + const before = `before/${path}`; + const after = `staged/${path}`; + const file = join(snapshot, before); + mkdirSync(dirname(file), { recursive: true }); + writeFileSync(file, original, { mode: statSync(join(snapshot, after)).mode }); + return run( + "git", + [ + "diff", + "--no-index", + "--binary", + "--no-ext-diff", + "--no-textconv", + "--no-color", + "--unified=1000000", + "--src-prefix=a/", + "--dst-prefix=b/", + "--", + before, + after, + ], + { cwd: snapshot, status: 1, maxBuffer: 32 * 1024 * 1024 }, + ); +} + +function worktreeMatches(path, original) { + const file = join(root, path); + try { + return lstatSync(file).isFile() && readFileSync(file).equals(original); + } catch { + // Leave removed or inaccessible working files untouched. + return false; + } +} + +function check() { + const before = indexedFiles(); + const changed = records(git(["diff", "--cached", "--name-only", "--no-renames", "-z", "--", "web/"])); + const regularFiles = new Map(); + for (const record of records(before)) { + const entry = /^(100644|100755) [0-9a-f]+ 0\t/.exec(record); + if (entry) regularFiles.set(record.slice(entry[0].length), entry[1]); + } + const targets = new Set(changed.filter((path) => /\.(js|jsx|ts|tsx)$/.test(path) && regularFiles.has(path))); + const generateTheme = changed.some(isThemeInput) && regularFiles.has(theme); + if (targets.size === 0 && !generateTheme) return; + + const snapshot = mkdtempSync(join(tmpdir(), "scopeweaver-staged-")); + const stagedRoot = join(snapshot, "staged"); + try { + // Project-aware Biome rules need real files and the rest of the staged web project. + mkdirSync(join(stagedRoot, ".git"), { recursive: true }); + git( + ["checkout-index", "--ignore-skip-worktree-bits", `--prefix=${stagedRoot}/`, "-z", "--stdin"], + forGit([...regularFiles.keys()]), + ); + if (generateTheme) targets.add(theme); + const originals = new Map([...targets].map((path) => [path, readFileSync(join(stagedRoot, path))])); + const stagedWeb = join(stagedRoot, "web"); + const runTool = (args) => run(process.execPath, args, { cwd: stagedWeb, stdio: "inherit" }); + + if (generateTheme) { + // Git has no directory entry when all preset CSS files have been deleted. + mkdirSync(join(stagedWeb, "src/styles/presets"), { recursive: true }); + symlinkSync(join(webRoot, "node_modules"), join(stagedWeb, "node_modules"), "junction"); + runTool([ + fileURLToPath(import.meta.resolve("ts-node/dist/bin.js")), + "-P", + join(stagedWeb, "tsconfig.scripts.json"), + join(stagedRoot, generator), + ]); + if (!changed.includes(theme) && readFileSync(join(stagedRoot, theme)).equals(originals.get(theme))) { + targets.delete(theme); + } + } + if (targets.size === 0) return; + + runTool([ + fileURLToPath(import.meta.resolve("@biomejs/biome/bin/biome")), + "check", + "--write", + "--no-errors-on-unmatched", + ...[...targets].map((path) => `./${path.slice("web/".length)}`), + ]); + + const updates = []; + const patches = new Map(); + for (const path of targets) { + const content = readFileSync(join(stagedRoot, path)); + if (content.equals(originals.get(path))) continue; + const object = git(["hash-object", "-w", `--path=${path}`, "--stdin"], content).toString().trim(); + updates.push(`${regularFiles.get(path)} ${object}\t${path}`); + patches.set(path, formattingPatch(snapshot, path, originals.get(path))); + } + if (!indexedFiles().equals(before)) throw new Error("The Git index changed while checking staged files"); + if (updates.length === 0) return; + checkCommitIndex(); + git(["update-index", "-z", "--index-info"], forGit(updates)); + + // Sync only unchanged working copies. A patch hunk can match a different + // location in an edited file, so leave partially staged files byte-for-byte. + for (const [path, patch] of patches) { + if (!worktreeMatches(path, originals.get(path))) continue; + const applied = spawnSync("git", ["-c", "apply.ignoreWhitespace=false", "apply", "-p2", "--whitespace=nowarn"], { + cwd: root, + input: patch, + stdio: "pipe", + }); + if (applied.status !== 0) console.warn(`Could not apply staged fixes to the working copy of ${JSON.stringify(path)}.`); + } + } finally { + rmSync(snapshot, { recursive: true, force: true }); + } +} + +try { + check(); +} catch (error) { + console.error(`Staged web checks failed: ${error.message}`); + process.exitCode = 1; +} diff --git a/web/.husky/check-staged.test.mjs b/web/.husky/check-staged.test.mjs new file mode 100644 index 00000000..973572c3 --- /dev/null +++ b/web/.husky/check-staged.test.mjs @@ -0,0 +1,183 @@ +import assert from "node:assert/strict"; +import { execFileSync, spawnSync } from "node:child_process"; +import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join, resolve } from "node:path"; +import test from "node:test"; + +const web = resolve(import.meta.dirname, ".."); +const theme = "web/src/lib/preferences/theme.ts"; +const preset = "web/src/styles/presets/brutalist.css"; +const environments = execFileSync("git", ["rev-parse", "--local-env-vars"], { encoding: "utf8" }).trim().split(/\r?\n/); + +function fixture(t, files = {}) { + const root = mkdtempSync(join(tmpdir(), "scopeweaver-hook-test-")); + t.after(() => rmSync(root, { recursive: true, force: true })); + const env = { ...process.env }; + for (const key of environments) delete env[key]; + Object.assign(env, { + GIT_CONFIG_GLOBAL: join(root, ".empty-global-config"), + GIT_CONFIG_NOSYSTEM: "1", + GIT_AUTHOR_NAME: "Hook fixture", + GIT_AUTHOR_EMAIL: "hook@example.invalid", + GIT_COMMITTER_NAME: "Hook fixture", + GIT_COMMITTER_EMAIL: "hook@example.invalid", + HUSKY: "1", + }); + const options = { cwd: root, env, encoding: "utf8" }; + const git = (...args) => execFileSync("git", args, options); + const write = (path, content) => { + mkdirSync(dirname(join(root, path)), { recursive: true }); + writeFileSync(join(root, path), content); + }; + const read = (path) => readFileSync(join(root, path), "utf8"); + + git("init", "--quiet", "--template="); + for (const path of [ + ".gitignore", + ".husky/check-staged.mjs", + ".husky/pre-commit", + "biome.json", + "package.json", + "src/scripts/generate-theme-presets.ts", + "tsconfig.json", + "tsconfig.scripts.json", + ]) { + const destination = join(root, "web", path); + mkdirSync(dirname(destination), { recursive: true }); + copyFileSync(join(web, path), destination); + } + write(".gitignore", "web/node_modules/\n"); + for (const [path, content] of Object.entries(files)) write(path, content); + symlinkSync(join(web, "node_modules"), join(root, "web/node_modules"), "junction"); + git("add", "."); + git("commit", "--quiet", "-m", "Initialize the hook fixture"); + const prepared = spawnSync("npm", ["run", "prepare"], { + ...options, + cwd: join(root, "web"), + shell: process.platform === "win32", + }); + assert.equal(prepared.status, 0, prepared.stderr); + assert.equal(git("config", "--get", "core.hooksPath").trim(), "web/.husky/_"); + return { + git, + write, + read, + commit: (...paths) => spawnSync("git", ["commit", "--quiet", "-m", "Run the pre-commit hook", ...paths], options), + }; +} + +test("the hook formats only supported staged files and preserves overlapping worktree edits", (t) => { + const deleted = "web/src/deleted.ts"; + const invalid = "export const invalid = missing;\n"; + const { git, write, read, commit } = fixture(t, { [deleted]: invalid }); + const directory = process.platform === "win32" ? "space [한글]" : "space [한글]\nnext"; + const staged = `web/src/${directory}/sample.ts`; + const nonWeb = "sample.ts"; + const unsupported = "web/src/sample.mts"; + const ignored = "web/src/components/ui/sample.ts"; + const fullyStaged = `web/src/${directory}/fully-staged.js`; + const partial = "web/src/partial.ts"; + const unchangedLines = [1, 2, 3, 4, 5].map((index) => `export const kept${index} = ${index};\n`).join(""); + const stagedPartial = `export const formatted=2\n${unchangedLines}`; + const unstagedPartial = `${stagedPartial}export const worktree = true;\n`; + write(partial, stagedPartial); + write(staged, "export const sample=2\n"); + write(fullyStaged, "export const fullyStaged=2\n"); + for (const path of [nonWeb, unsupported, ignored]) write(path, invalid); + git("add", "--", staged, fullyStaged, nonWeb, unsupported, ignored, partial); + write(partial, unstagedPartial); + git("rm", "--quiet", "--", deleted); + write(deleted, "export const untracked = true;\n"); + const unstaged = "export const sample=3\n"; + write(staged, unstaged); + + const result = commit(); + assert.equal(result.status, 0, result.stderr); + assert.equal(git("show", `HEAD:${staged}`), "export const sample = 2;\n"); + assert.equal(git("show", `HEAD:${fullyStaged}`), "export const fullyStaged = 2;\n"); + assert.equal(read(fullyStaged), "export const fullyStaged = 2;\n"); + assert.equal(git("diff", "--", fullyStaged), ""); + for (const path of [nonWeb, unsupported, ignored]) assert.equal(git("show", `HEAD:${path}`), invalid); + assert.equal(git("ls-files", "--", deleted), ""); + assert.equal(read(deleted), "export const untracked = true;\n"); + assert.equal(read(staged), unstaged); + assert.equal(git("show", `HEAD:${partial}`), `export const formatted = 2;\n${unchangedLines}`); + assert.equal(read(partial), unstagedPartial); +}); + +test("path-limited commits requiring fixes leave the original index and worktree intact", (t) => { + const path = "web/src/sample.ts"; + const { git, write, read, commit } = fixture(t, { [path]: "export const sample = 1;\n" }); + const unstaged = "export const sample=2\n"; + write(path, unstaged); + const index = git("ls-files", "--stage"); + + const result = commit("--", path); + assert.equal(result.status, 1); + assert.match(result.stderr, /Stage the paths and run git commit without paths/); + assert.equal(git("ls-files", "--stage"), index); + assert.equal(read(path), unstaged); + + git("add", "--", path); + const retried = commit(); + assert.equal(retried.status, 0, retried.stderr); + assert.equal(read(path), "export const sample = 2;\n"); + assert.equal(git("status", "--short"), ""); +}); + +test("a staged lint error aborts the commit and leaves all staged and unstaged content intact", (t) => { + const { git, write, read, commit } = fixture(t); + const fixable = "web/src/fixable.ts"; + const invalid = "web/src/invalid.ts"; + write(fixable, "export const fixable=1\n"); + write(invalid, "export const invalid = missing;\n"); + git("add", "--", fixable, invalid); + const index = git("ls-files", "--stage"); + const worktree = "export const invalid = 4;\n"; + write(invalid, worktree); + + const result = commit(); + assert.equal(result.status, 1); + assert.match(result.stderr, /noUndeclaredVariables/); + assert.equal(git("ls-files", "--stage"), index); + assert.equal(read(fixable), "export const fixable=1\n"); + assert.equal(read(invalid), worktree); +}); + +test("theme generation reads staged CSS and protects unstaged theme changes", (t) => { + const originalTheme = ` +const swt = (key: string) => key; +// --- generated:themePresets:start --- +// --- generated:themePresets:end --- +`; + const colors = (color) => + `/*\nlabel: Brutalist\nvalue: brutalist\n*/\n:root[data-theme-preset="brutalist"] { --primary: ${color}; }\n.dark:root[data-theme-preset="brutalist"] { --primary: black; }\n`; + const { git, write, read, commit } = fixture(t, { + [theme]: originalTheme, + [preset]: colors("red"), + "web/src/app/globals.css": ":root { --primary: white; }\n.dark { --primary: gray; }\n", + }); + write(preset, colors("green")); + git("add", "--", preset); + write(preset, colors("purple")); + const generator = "web/src/scripts/generate-theme-presets.ts"; + const config = "web/tsconfig.scripts.json"; + write(generator, "throw new Error('unstaged generator');\n"); + write(config, "invalid unstaged TypeScript options"); + const unstagedTheme = `${originalTheme}export const worktree = true;\n`; + write(theme, unstagedTheme); + + const result = commit(); + assert.equal(result.status, 0, result.stderr); + const generated = git("show", `HEAD:${theme}`); + assert.match(generated, /light: "green"/); + assert.match(generated, /swt\("english\.e188"\)/); + assert.match(generated, /swt\("english\.e189"\)/); + assert.doesNotMatch(generated, /purple|worktree/); + assert.equal(read(theme), unstagedTheme); + assert.equal(read(preset), colors("purple")); + assert.equal(read(generator), "throw new Error('unstaged generator');\n"); + assert.equal(read(config), "invalid unstaged TypeScript options"); + assert.equal(git("show", "--name-only", "--format=", "HEAD"), `${theme}\n${preset}\n`); +}); diff --git a/web/.husky/pre-commit b/web/.husky/pre-commit index bef792a6..7e3fac24 100644 --- a/web/.husky/pre-commit +++ b/web/.husky/pre-commit @@ -1,3 +1 @@ -npm run generate:presets -git add src/lib/preferences/theme.ts -npm exec -- lint-staged +cd web && exec node .husky/check-staged.mjs diff --git a/web/package-lock.json b/web/package-lock.json index 22548dc7..4836257c 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -59,7 +59,6 @@ "@types/topojson-client": "^3.1.5", "babel-plugin-react-compiler": "^1.0.0", "husky": "^9.1.7", - "lint-staged": "^16.4.0", "postcss": "^8.5.29", "tailwindcss": "^4.3.3", "ts-node": "^10.9.2", @@ -3817,48 +3816,6 @@ "node": ">=0.4.0" } }, - "node_modules/ansi-escapes": { - "version": "7.3.0", - "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-7.3.0.tgz", - "integrity": "sha512-BvU8nYgGQBxcmMuEeUEmNTvrMVjJNSH7RgW24vXexN4Ven6qCvy4TntnvlnwnMLTVlcRQQdbRY8NKnaIoeWDNg==", - "dev": true, - "license": "MIT", - "dependencies": { - "environment": "^1.0.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/ansi-regex": { - "version": "6.2.2", - "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-6.2.2.tgz", - "integrity": "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/chalk/ansi-regex?sponsor=1" - } - }, - "node_modules/ansi-styles": { - "version": "6.2.3", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-6.2.3.tgz", - "integrity": "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/chalk/ansi-styles?sponsor=1" - } - }, "node_modules/arg": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", @@ -4013,39 +3970,6 @@ "integrity": "sha512-JhZUT7JFcQy/EzW605k/ktHtncoo9vnyW/2GspNYwFlN1C/WmjuV/xtS04e9SOkL2sTdw0VAZ2UGCcQ9lR6p6w==", "license": "MIT" }, - "node_modules/cli-cursor": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-5.0.0.tgz", - "integrity": "sha512-aCj4O5wKyszjMmDT4tZj93kxyydN/K5zPWSCe6/0AV/AA1pqe5ZBIw0a2ZfPQV7lL5/yb5HsUreJ6UFAF1tEQw==", - "dev": true, - "license": "MIT", - "dependencies": { - "restore-cursor": "^5.0.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/cli-truncate": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/cli-truncate/-/cli-truncate-5.2.0.tgz", - "integrity": "sha512-xRwvIOMGrfOAnM1JYtqQImuaNtDEv9v6oIYAs4LIHwTiKee8uwvIi363igssOC0O5U04i4AlENs79LQLu9tEMw==", - "dev": true, - "license": "MIT", - "dependencies": { - "slice-ansi": "^8.0.0", - "string-width": "^8.2.0" - }, - "engines": { - "node": ">=20" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/client-only": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/client-only/-/client-only-0.0.1.tgz", @@ -4093,13 +4017,6 @@ "simple-swizzle": "^0.2.2" } }, - "node_modules/colorette": { - "version": "2.0.20", - "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.20.tgz", - "integrity": "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==", - "dev": true, - "license": "MIT" - }, "node_modules/comlink": { "version": "4.4.2", "resolved": "https://registry.npmjs.org/comlink/-/comlink-4.4.2.tgz", @@ -4116,16 +4033,6 @@ "url": "https://github.com/sponsors/wooorm" } }, - "node_modules/commander": { - "version": "14.0.3", - "resolved": "https://registry.npmjs.org/commander/-/commander-14.0.3.tgz", - "integrity": "sha512-H+y0Jo/T1RZ9qPP4Eh1pkcQcLRglraJaSLoyOtHxu6AapkjWVCy2Sit1QQ4x3Dng8qDlSsZEet7g5Pq06MvTgw==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=20" - } - }, "node_modules/create-require": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", @@ -4645,13 +4552,6 @@ "embla-carousel": "8.6.0" } }, - "node_modules/emoji-regex": { - "version": "10.6.0", - "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-10.6.0.tgz", - "integrity": "sha512-toUI84YS5YmxW219erniWD0CIVOo46xGKColeNQRgOzDorgBi1v4D71/OFzgD9GO2UGKIv1C3Sp8DAn0+j5w7A==", - "dev": true, - "license": "MIT" - }, "node_modules/enhanced-resolve": { "version": "5.26.0", "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.26.0.tgz", @@ -4666,19 +4566,6 @@ "node": ">=10.13.0" } }, - "node_modules/environment": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/environment/-/environment-1.1.0.tgz", - "integrity": "sha512-xUtoPkMggbz0MPyPiIWr1Kp4aeWJjDZ6SMvURhimjdZgsRuDplF5/s9hcgGhyXMhs+6vpnuoiZ2kFiu3FMnS8Q==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/es-toolkit": { "version": "1.49.0", "resolved": "https://registry.npmjs.org/es-toolkit/-/es-toolkit-1.49.0.tgz", @@ -4744,19 +4631,6 @@ "next": ">=13.2.0" } }, - "node_modules/get-east-asian-width": { - "version": "1.6.0", - "resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.6.0.tgz", - "integrity": "sha512-QRbvDIbx6YklUe6RxeTeleMR0yv3cYH6PsPZHcnVn7xv7zO1BHN8r0XETu8n6Ye3Q+ahtSarc3WgtNWmehIBfA==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/get-nonce": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/get-nonce/-/get-nonce-1.0.1.tgz", @@ -4942,22 +4816,6 @@ "url": "https://github.com/sponsors/wooorm" } }, - "node_modules/is-fullwidth-code-point": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-5.1.0.tgz", - "integrity": "sha512-5XHYaSyiqADb4RnZ1Bdad6cPp8Toise4TzEjcOYDHZkTCbKgiUl7WTUCpNWHuxmDt91wnsZBc9xinNzopv3JMQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "get-east-asian-width": "^1.3.1" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/is-hexadecimal": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/is-hexadecimal/-/is-hexadecimal-2.0.1.tgz", @@ -5251,91 +5109,12 @@ "url": "https://opencollective.com/parcel" } }, - "node_modules/lint-staged": { - "version": "16.4.0", - "resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-16.4.0.tgz", - "integrity": "sha512-lBWt8hujh/Cjysw5GYVmZpFHXDCgZzhrOm8vbcUdobADZNOK/bRshr2kM3DfgrrtR1DQhfupW9gnIXOfiFi+bw==", - "dev": true, - "license": "MIT", - "dependencies": { - "commander": "^14.0.3", - "listr2": "^9.0.5", - "picomatch": "^4.0.3", - "string-argv": "^0.3.2", - "tinyexec": "^1.0.4", - "yaml": "^2.8.2" - }, - "bin": { - "lint-staged": "bin/lint-staged.js" - }, - "engines": { - "node": ">=20.17" - }, - "funding": { - "url": "https://opencollective.com/lint-staged" - } - }, - "node_modules/listr2": { - "version": "9.0.5", - "resolved": "https://registry.npmjs.org/listr2/-/listr2-9.0.5.tgz", - "integrity": "sha512-ME4Fb83LgEgwNw96RKNvKV4VTLuXfoKudAmm2lP8Kk87KaMK0/Xrx/aAkMWmT8mDb+3MlFDspfbCs7adjRxA2g==", - "dev": true, - "license": "MIT", - "dependencies": { - "cli-truncate": "^5.0.0", - "colorette": "^2.0.20", - "eventemitter3": "^5.0.1", - "log-update": "^6.1.0", - "rfdc": "^1.4.1", - "wrap-ansi": "^9.0.0" - }, - "engines": { - "node": ">=20.0.0" - } - }, "node_modules/lodash": { "version": "4.18.1", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.18.1.tgz", "integrity": "sha512-dMInicTPVE8d1e5otfwmmjlxkZoUpiVLwyeTdUsi/Caj/gfzzblBcCE5sRHV/AsjuCmxWrte2TNGSYuCeCq+0Q==", "license": "MIT" }, - "node_modules/log-update": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/log-update/-/log-update-6.1.0.tgz", - "integrity": "sha512-9ie8ItPR6tjY5uYJh8K/Zrv/RMZ5VOlOWvtZdEHYSTFKZfIBPQa9tOAEeAWhd+AnIneLJ22w5fjOYtoutpWq5w==", - "dev": true, - "license": "MIT", - "dependencies": { - "ansi-escapes": "^7.0.0", - "cli-cursor": "^5.0.0", - "slice-ansi": "^7.1.0", - "strip-ansi": "^7.1.0", - "wrap-ansi": "^9.0.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/log-update/node_modules/slice-ansi": { - "version": "7.1.2", - "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-7.1.2.tgz", - "integrity": "sha512-iOBWFgUX7caIZiuutICxVgX1SdxwAVFFKwt1EvMYYec/NWO5meOJ6K5uQxhrYBdQJne4KxiqZc+KptFOWFSI9w==", - "dev": true, - "license": "MIT", - "dependencies": { - "ansi-styles": "^6.2.1", - "is-fullwidth-code-point": "^5.0.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/chalk/slice-ansi?sponsor=1" - } - }, "node_modules/longest-streak": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/longest-streak/-/longest-streak-3.1.0.tgz", @@ -6215,19 +5994,6 @@ ], "license": "MIT" }, - "node_modules/mimic-function": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/mimic-function/-/mimic-function-5.0.1.tgz", - "integrity": "sha512-VP79XUPxV2CigYP3jWwAUFSku2aKqBH7uTAapFWCBqutsbmDo96KY5o8uh6U+/YSIn5OxJnXp73beVkpqMIGhA==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/ml-array-max": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ml-array-max/-/ml-array-max-2.0.0.tgz", @@ -6381,22 +6147,6 @@ "node": "^10 || ^12 || >=14" } }, - "node_modules/onetime": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/onetime/-/onetime-7.0.0.tgz", - "integrity": "sha512-VXJjc87FScF88uafS3JllDgvAm+c/Slfz06lorj2uAY34rlUu0Nt+v8wreiImcrgAjjIHp1rXpTDlLOGw29WwQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "mimic-function": "^5.0.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/parse-entities": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/parse-entities/-/parse-entities-4.0.2.tgz", @@ -6428,19 +6178,6 @@ "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", "license": "ISC" }, - "node_modules/picomatch": { - "version": "4.0.4", - "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz", - "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/jonschlinkert" - } - }, "node_modules/postcss": { "version": "8.5.29", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.29.tgz", @@ -6877,30 +6614,6 @@ "integrity": "sha512-AgZ3UOZm3YndfrJ4OYjgrT7bmCm/1iqkjvEfH/oYjzh6PD2qw4QuT3jjnXIrpdt4MTpMXclMT3lXbmRY+XRakw==", "license": "MIT" }, - "node_modules/restore-cursor": { - "version": "5.1.0", - "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-5.1.0.tgz", - "integrity": "sha512-oMA2dcrw6u0YfxJQXm342bFKX/E4sG9rbTzO9ptUcR/e8A33cHuvStiYOwH7fszkZlZ1z/ta9AAoPk2F4qIOHA==", - "dev": true, - "license": "MIT", - "dependencies": { - "onetime": "^7.0.0", - "signal-exit": "^4.1.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/rfdc": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/rfdc/-/rfdc-1.4.1.tgz", - "integrity": "sha512-q1b3N5QkRUWUl7iyylaaj3kOpIT0N2i9MqIEQXP73GVsN9cw3fdx8X63cEmWhJGi2PPCF23Ijp7ktmd39rawIA==", - "dev": true, - "license": "MIT" - }, "node_modules/rw": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/rw/-/rw-1.3.3.tgz", @@ -6981,19 +6694,6 @@ "node": ">=10" } }, - "node_modules/signal-exit": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-4.1.0.tgz", - "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==", - "dev": true, - "license": "ISC", - "engines": { - "node": ">=14" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - }, "node_modules/simple-icons": { "version": "16.24.1", "resolved": "https://registry.npmjs.org/simple-icons/-/simple-icons-16.24.1.tgz", @@ -7028,23 +6728,6 @@ "integrity": "sha512-m6UrgzFVUYawGBh1dUsWR5M2Clqic9RVXC/9f8ceNlv2IcO9j9J/z8UoCLPqtsPBFNzEpfR3xftohbfqDx8EQA==", "license": "MIT" }, - "node_modules/slice-ansi": { - "version": "8.0.0", - "resolved": "https://registry.npmjs.org/slice-ansi/-/slice-ansi-8.0.0.tgz", - "integrity": "sha512-stxByr12oeeOyY2BlviTNQlYV5xOj47GirPr4yA1hE9JCtxfQN0+tVbkxwCtYDQWhEKWFHsEK48ORg5jrouCAg==", - "dev": true, - "license": "MIT", - "dependencies": { - "ansi-styles": "^6.2.3", - "is-fullwidth-code-point": "^5.1.0" - }, - "engines": { - "node": ">=20" - }, - "funding": { - "url": "https://github.com/chalk/slice-ansi?sponsor=1" - } - }, "node_modules/sonner": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/sonner/-/sonner-2.0.7.tgz", @@ -7074,33 +6757,6 @@ "url": "https://github.com/sponsors/wooorm" } }, - "node_modules/string-argv": { - "version": "0.3.2", - "resolved": "https://registry.npmjs.org/string-argv/-/string-argv-0.3.2.tgz", - "integrity": "sha512-aqD2Q0144Z+/RqG52NeHEkZauTAUWJO8c6yTftGJKO3Tja5tUgIfmIl6kExvhtxSDP7fXB6DvzkfMpCd/F3G+Q==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=0.6.19" - } - }, - "node_modules/string-width": { - "version": "8.2.1", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-8.2.1.tgz", - "integrity": "sha512-IIaP0g3iy9Cyy18w3M9YcaDudujEAVHKt3a3QJg1+sr/oX96TbaGUubG0hJyCjCBThFH+tFpcIyoUHUn1ogaLA==", - "dev": true, - "license": "MIT", - "dependencies": { - "get-east-asian-width": "^1.5.0", - "strip-ansi": "^7.1.2" - }, - "engines": { - "node": ">=20" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/stringify-entities": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/stringify-entities/-/stringify-entities-4.0.4.tgz", @@ -7115,22 +6771,6 @@ "url": "https://github.com/sponsors/wooorm" } }, - "node_modules/strip-ansi": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.2.0.tgz", - "integrity": "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w==", - "dev": true, - "license": "MIT", - "dependencies": { - "ansi-regex": "^6.2.2" - }, - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/chalk/strip-ansi?sponsor=1" - } - }, "node_modules/style-to-js": { "version": "1.1.21", "resolved": "https://registry.npmjs.org/style-to-js/-/style-to-js-1.1.21.tgz", @@ -7246,16 +6886,6 @@ "integrity": "sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg==", "license": "MIT" }, - "node_modules/tinyexec": { - "version": "1.2.4", - "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.2.4.tgz", - "integrity": "sha512-SHf/r48b7vOrjve9PxJo3MN5v5yuyjHvdUcrQffT3WXMUfnGmHDVbC4k3sHJaJTgZCwpUplIaAo5ANtMyp3YHg==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=18" - } - }, "node_modules/topojson-client": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/topojson-client/-/topojson-client-3.1.0.tgz", @@ -7605,58 +7235,6 @@ "d3-timer": "^3.0.1" } }, - "node_modules/wrap-ansi": { - "version": "9.0.2", - "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-9.0.2.tgz", - "integrity": "sha512-42AtmgqjV+X1VpdOfyTGOYRi0/zsoLqtXQckTmqTeybT+BDIbM/Guxo7x3pE2vtpr1ok6xRqM9OpBe+Jyoqyww==", - "dev": true, - "license": "MIT", - "dependencies": { - "ansi-styles": "^6.2.1", - "string-width": "^7.0.0", - "strip-ansi": "^7.1.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/chalk/wrap-ansi?sponsor=1" - } - }, - "node_modules/wrap-ansi/node_modules/string-width": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/string-width/-/string-width-7.2.0.tgz", - "integrity": "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "emoji-regex": "^10.3.0", - "get-east-asian-width": "^1.0.0", - "strip-ansi": "^7.1.0" - }, - "engines": { - "node": ">=18" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/yaml": { - "version": "2.9.0", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.0.tgz", - "integrity": "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==", - "dev": true, - "license": "ISC", - "bin": { - "yaml": "bin.mjs" - }, - "engines": { - "node": ">= 14.6" - }, - "funding": { - "url": "https://github.com/sponsors/eemeli" - } - }, "node_modules/yn": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", diff --git a/web/package.json b/web/package.json index b1d34115..44cbd4f9 100644 --- a/web/package.json +++ b/web/package.json @@ -11,14 +11,9 @@ "format": "biome format --write", "check": "biome check", "check:fix": "biome check --write", - "prepare": "husky", + "prepare": "cd .. && husky web/.husky", "generate:presets": "ts-node -P tsconfig.scripts.json src/scripts/generate-theme-presets.ts", - "test": "node --test src/lib/*.test.mjs src/i18n/*.test.mjs" - }, - "lint-staged": { - "*.{js,ts,jsx,tsx}": [ - "biome check --write --no-errors-on-unmatched" - ] + "test": "node --test src/lib/*.test.mjs src/i18n/*.test.mjs .husky/*.test.mjs" }, "dependencies": { "@antv/g6": "^5.1.1", @@ -72,7 +67,6 @@ "@types/topojson-client": "^3.1.5", "babel-plugin-react-compiler": "^1.0.0", "husky": "^9.1.7", - "lint-staged": "^16.4.0", "postcss": "^8.5.29", "tailwindcss": "^4.3.3", "ts-node": "^10.9.2", diff --git a/web/src/scripts/generate-theme-presets.ts b/web/src/scripts/generate-theme-presets.ts index 31994444..e46dce0d 100644 --- a/web/src/scripts/generate-theme-presets.ts +++ b/web/src/scripts/generate-theme-presets.ts @@ -1,4 +1,3 @@ -import { translate as swt } from "@/i18n/runtime"; /** * Script: generate-theme-presets.ts * @@ -12,7 +11,7 @@ import { translate as swt } from "@/i18n/runtime"; * - During local development, run manually after adding any new theme preset: * npm run generate:presets * - Ensure that each new CSS preset includes `label:` and `value:` comments. - * - This generation step is currently automated using a Husky pre-push hook. + * - The Husky pre-commit hook runs this on staged theme inputs. * - You may optionally integrate it directly into a build step if preferred. */ @@ -29,7 +28,10 @@ if (!fs.existsSync(presetDir)) { const outputPath = path.resolve(__dirname, "../lib/preferences/theme.ts"); -const files = fs.readdirSync(presetDir).filter((file) => file.endsWith(".css")); +const files = fs + .readdirSync(presetDir) + .filter((file) => file.endsWith(".css")) + .sort(); if (files.length === 0) { console.warn("⚠️ No preset CSS files found. Only default preset will be included."); @@ -89,11 +91,34 @@ const defaultPrimary = { dark: defaultDarkPrimaryMatch?.[1]?.trim() ?? "", }; -presets.unshift({ get label() { return swt("english.e188"); }, value: "default", primary: defaultPrimary }); +presets.unshift({ label: "Default", value: "default", primary: defaultPrimary }); + +const localizedLabels = new Map([ + ["default", "english.e188"], + ["brutalist", "english.e189"], + ["soft-pop", "english.e190"], + ["tangerine", "english.e191"], +]); +const presetDefinitions = presets.map(({ label, value, primary }) => { + const labelKey = localizedLabels.get(value); + const labelProperty = labelKey + ? `get label() { return swt(${JSON.stringify(labelKey)}); }` + : `label: ${JSON.stringify(label)}`; + return ` { + ${labelProperty}, + value: ${JSON.stringify(value)}, + primary: { + light: ${JSON.stringify(primary.light)}, + dark: ${JSON.stringify(primary.dark)}, + }, + },`; +}); const generatedBlock = `// --- generated:themePresets:start --- -export const THEME_PRESET_OPTIONS = ${JSON.stringify(presets, null, 2)} as const; +export const THEME_PRESET_OPTIONS = [ +${presetDefinitions.join("\n")} +] as const; export const THEME_PRESET_VALUES = THEME_PRESET_OPTIONS.map((p) => p.value); @@ -110,12 +135,10 @@ const updated = fileContent.replace( function main() { const biomeBin = require.resolve("@biomejs/biome/bin/biome"); - const formatted = execFileSync(process.execPath, [biomeBin, "format", "--stdin-file-path", outputPath], { - input: updated, - encoding: "utf8", - }); - - if (formatted === fileContent) { + const format = (input: string) => + execFileSync(process.execPath, [biomeBin, "format", "--stdin-file-path", outputPath], { input, encoding: "utf8" }); + const formatted = format(updated); + if (formatted === fileContent || formatted === format(fileContent)) { console.log("ℹ️ No changes in theme.ts"); return; }