ztheme-cli 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +260 -45
- package/audit.mjs +559 -0
- package/color.mjs +228 -0
- package/package.json +8 -2
- package/rules.mjs +186 -0
- package/ztheme.mjs +236 -5
package/ztheme.mjs
CHANGED
|
@@ -2,19 +2,24 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* ztheme —— 不依赖 shadcn 的主题 CLI。
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* 只做四件事:读索引、看单套、落到本地 CSS、扫仓库漂移。
|
|
6
|
+
* 前 3 件全部走 HTTP;audit 在本地静态扫描,不复制服务端的判定算法。
|
|
6
7
|
* 不用 shadcn CLI、不用 MCP、不需要登录,只要求 Node 18+(用内置 fetch)。
|
|
7
8
|
*
|
|
8
9
|
* node cli/ztheme.mjs list
|
|
9
10
|
* node cli/ztheme.mjs show modern-minimal
|
|
10
11
|
* node cli/ztheme.mjs add modern-minimal [--out ztheme] [--print]
|
|
11
12
|
* node cli/ztheme.mjs check modern-minimal [--strict]
|
|
13
|
+
* node cli/ztheme.mjs audit [path] [--json]
|
|
12
14
|
*
|
|
13
15
|
* 默认站点取 ZTHEME_BASE 环境变量,否则 http://localhost:3001。
|
|
14
16
|
*/
|
|
15
17
|
import { createHash } from "node:crypto";
|
|
16
18
|
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
17
|
-
import { join } from "node:path";
|
|
19
|
+
import { join, resolve } from "node:path";
|
|
20
|
+
|
|
21
|
+
import { auditProject, formatHuman, formatJson } from "./audit.mjs";
|
|
22
|
+
import { validateRules } from "./rules.mjs";
|
|
18
23
|
|
|
19
24
|
const BASE = (process.env.ZTHEME_BASE ?? "http://localhost:3001").replace(/\/$/, "");
|
|
20
25
|
|
|
@@ -140,6 +145,170 @@ const digestOf = (value) =>
|
|
|
140
145
|
const LOCK_DIR = ".ztheme";
|
|
141
146
|
const LOCK_FILE = join(LOCK_DIR, "theme-lock.json");
|
|
142
147
|
|
|
148
|
+
const failWith = (msg, code = 1) => {
|
|
149
|
+
console.error("[ztheme] " + msg);
|
|
150
|
+
process.exit(code);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const tryGetJson = async (path) => {
|
|
154
|
+
try {
|
|
155
|
+
const res = await fetch(BASE + path);
|
|
156
|
+
if (!res.ok) return null;
|
|
157
|
+
return await res.json();
|
|
158
|
+
} catch {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/** 支持 --name value 与 --name=value;缺值返回 undefined */
|
|
164
|
+
const valueOf = (name) => {
|
|
165
|
+
const prefix = "--" + name + "=";
|
|
166
|
+
const inline = argv.find((arg) => arg.startsWith(prefix));
|
|
167
|
+
if (inline) return inline.slice(prefix.length);
|
|
168
|
+
const index = argv.indexOf("--" + name);
|
|
169
|
+
if (index < 0) return undefined;
|
|
170
|
+
const value = argv[index + 1];
|
|
171
|
+
return value && !value.startsWith("--") ? value : undefined;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/** audit 的位置参数:跳过 --theme / --max-warnings 的取值 */
|
|
175
|
+
const auditTargetOf = () => {
|
|
176
|
+
const rest = argv.slice(1);
|
|
177
|
+
for (let index = 0; index < rest.length; index++) {
|
|
178
|
+
const arg = rest[index];
|
|
179
|
+
if (arg === "--theme" || arg === "--max-warnings") {
|
|
180
|
+
index += 1;
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
if (arg.startsWith("--")) continue;
|
|
184
|
+
return arg;
|
|
185
|
+
}
|
|
186
|
+
return ".";
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const loadAuditConfig = (root) => {
|
|
190
|
+
const file = join(root, LOCK_DIR, "audit.json");
|
|
191
|
+
if (!existsSync(file)) return { config: {}, warnings: [] };
|
|
192
|
+
|
|
193
|
+
let parsed;
|
|
194
|
+
try {
|
|
195
|
+
parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
196
|
+
} catch (error) {
|
|
197
|
+
failWith("无法解析 " + file + ":" + (error?.message ?? error), 2);
|
|
198
|
+
}
|
|
199
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
200
|
+
failWith("audit 配置必须是对象:" + file, 2);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const warnings = [];
|
|
204
|
+
const allowed = ["theme", "include", "exclude", "severity", "ignore", "maxWarnings"];
|
|
205
|
+
for (const key of Object.keys(parsed)) {
|
|
206
|
+
if (!allowed.includes(key)) warnings.push("未知配置项 " + key + "(已忽略)");
|
|
207
|
+
}
|
|
208
|
+
for (const key of ["include", "exclude"]) {
|
|
209
|
+
if (
|
|
210
|
+
parsed[key] !== undefined &&
|
|
211
|
+
(!Array.isArray(parsed[key]) || parsed[key].some((value) => typeof value !== "string"))
|
|
212
|
+
) {
|
|
213
|
+
failWith("audit." + key + " 必须是字符串数组", 2);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (parsed.theme !== undefined && typeof parsed.theme !== "string") {
|
|
217
|
+
failWith("audit.theme 必须是字符串", 2);
|
|
218
|
+
}
|
|
219
|
+
if (
|
|
220
|
+
parsed.maxWarnings !== undefined &&
|
|
221
|
+
(!Number.isInteger(parsed.maxWarnings) || parsed.maxWarnings < 0)
|
|
222
|
+
) {
|
|
223
|
+
failWith("audit.maxWarnings 必须是非负整数", 2);
|
|
224
|
+
}
|
|
225
|
+
if (parsed.severity !== undefined) {
|
|
226
|
+
if (!parsed.severity || typeof parsed.severity !== "object" || Array.isArray(parsed.severity)) {
|
|
227
|
+
failWith("audit.severity 必须是对象", 2);
|
|
228
|
+
}
|
|
229
|
+
for (const [rule, level] of Object.entries(parsed.severity)) {
|
|
230
|
+
if (!["off", "warn", "error"].includes(level)) {
|
|
231
|
+
failWith("audit.severity." + rule + " 必须是 off / warn / error", 2);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (parsed.ignore !== undefined) {
|
|
236
|
+
if (!Array.isArray(parsed.ignore)) failWith("audit.ignore 必须是数组", 2);
|
|
237
|
+
for (const entry of parsed.ignore) {
|
|
238
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
|
|
239
|
+
failWith("audit.ignore 的每一项必须是对象", 2);
|
|
240
|
+
}
|
|
241
|
+
for (const key of ["rule", "file", "value"]) {
|
|
242
|
+
if (entry[key] !== undefined && typeof entry[key] !== "string") {
|
|
243
|
+
failWith("audit.ignore." + key + " 必须是字符串", 2);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return { config: parsed, warnings };
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 项目规则:只读阈值(圆角上限、间距基准),级别仍由 audit.json 的 severity 决定。
|
|
254
|
+
* 坏掉的规则文件必须让退出码变成 2——当成「没有规则」继续扫会静默放行。
|
|
255
|
+
*/
|
|
256
|
+
const loadAuditRules = (root) => {
|
|
257
|
+
const file = join(root, LOCK_DIR, "rules.json");
|
|
258
|
+
if (!existsSync(file)) return null;
|
|
259
|
+
|
|
260
|
+
let parsed;
|
|
261
|
+
try {
|
|
262
|
+
parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
263
|
+
} catch (error) {
|
|
264
|
+
failWith("无法解析 " + file + ":" + (error?.message ?? error), 2);
|
|
265
|
+
}
|
|
266
|
+
const { policy, warnings, errors } = validateRules(parsed);
|
|
267
|
+
for (const warning of warnings) console.error("[ztheme] 警告:" + warning);
|
|
268
|
+
if (errors.length) failWith("rules.json 无效(" + file + "):" + errors.join(";"), 2);
|
|
269
|
+
return policy;
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const readLockFile = (root) => {
|
|
273
|
+
const file = join(root, LOCK_FILE);
|
|
274
|
+
if (!existsSync(file)) return null;
|
|
275
|
+
try {
|
|
276
|
+
return JSON.parse(readFileSync(file, "utf8"));
|
|
277
|
+
} catch (error) {
|
|
278
|
+
failWith("无法解析锁文件 " + file + ":" + (error?.message ?? error), 2);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* audit 的主题来源:本地 CSS 优先(离线、可复现),否则回站点。
|
|
284
|
+
* 拉取失败不阻断扫描,只关闭建议。
|
|
285
|
+
*/
|
|
286
|
+
const loadAuditTheme = async (root, slug) => {
|
|
287
|
+
const cssFile = join(root, "ztheme", slug + ".css");
|
|
288
|
+
if (existsSync(cssFile)) {
|
|
289
|
+
const vars = parseCssVars(readFileSync(cssFile, "utf8"));
|
|
290
|
+
if (Object.keys(vars.light).length || Object.keys(vars.dark).length) {
|
|
291
|
+
return { slug, source: "lock", tokens: vars };
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const item = await tryGetJson("/r/themes/" + slug);
|
|
296
|
+
if (item?.cssVars) {
|
|
297
|
+
return {
|
|
298
|
+
slug,
|
|
299
|
+
source: "site",
|
|
300
|
+
tokens: {
|
|
301
|
+
light: item.cssVars.light ?? {},
|
|
302
|
+
dark: item.cssVars.dark ?? {},
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
console.error("[ztheme] 无法读取主题 " + slug + ",继续扫描但关闭 token 建议");
|
|
308
|
+
return { slug, source: "none", tokens: undefined };
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
|
|
143
312
|
const TOKENS = [
|
|
144
313
|
"background", "foreground", "card", "card-foreground", "popover", "popover-foreground",
|
|
145
314
|
"primary", "primary-foreground", "secondary", "secondary-foreground",
|
|
@@ -186,8 +355,13 @@ const main = async () => {
|
|
|
186
355
|
ztheme init <slug> [--dir .] 生成 ztheme/<slug>.css + AGENTS.md 片段 + .ztheme/theme-lock.json
|
|
187
356
|
ztheme check --lock [--file x.css]
|
|
188
357
|
与锁文件比对:本地 CSS 变差、或站点主题已更新时报警(--strict 判失败)
|
|
358
|
+
ztheme audit [path] [--json] [--theme <slug>] [--max-warnings <n>] [--quiet]
|
|
359
|
+
扫描 TSX/JSX/TS/JS 里的硬编码颜色与任意值;
|
|
360
|
+
--json 只输出 JSON;默认只告警,不阻断
|
|
189
361
|
环境变量:
|
|
190
|
-
ZTHEME_BASE
|
|
362
|
+
ZTHEME_BASE 站点地址,默认 ${BASE}
|
|
363
|
+
ZTHEME_THEME audit 的对照主题(等价 --theme)
|
|
364
|
+
ZTHEME_MAX_WARNINGS audit 的告警阈值(等价 --max-warnings)`);
|
|
191
365
|
return;
|
|
192
366
|
}
|
|
193
367
|
|
|
@@ -296,13 +470,17 @@ const main = async () => {
|
|
|
296
470
|
"",
|
|
297
471
|
" npx ztheme-cli check --file " + relCss + " --rules " + slug + " --strict",
|
|
298
472
|
"",
|
|
473
|
+
"提交前自查(必须跑,别只看截图):",
|
|
474
|
+
"",
|
|
475
|
+
" npx ztheme-cli audit --json",
|
|
476
|
+
"",
|
|
299
477
|
"### 可判定断言",
|
|
300
478
|
"",
|
|
301
479
|
...assertions.map((a) => "- " + a.kind + (a.token ? "(" + a.token + ")" : "") + " 应为 " + a.expect),
|
|
302
480
|
"",
|
|
303
|
-
"###
|
|
481
|
+
"### 建议(不是硬约束,硬约束见上)",
|
|
304
482
|
"",
|
|
305
|
-
...((rulesRes.rules && rulesRes.rules.
|
|
483
|
+
...((rulesRes.rules && rulesRes.rules.advisory) || []).map((f) => "- " + f),
|
|
306
484
|
"",
|
|
307
485
|
"### 令牌",
|
|
308
486
|
"",
|
|
@@ -490,6 +668,59 @@ const main = async () => {
|
|
|
490
668
|
return;
|
|
491
669
|
}
|
|
492
670
|
|
|
671
|
+
if (cmd === "audit") {
|
|
672
|
+
const target = auditTargetOf();
|
|
673
|
+
const root = resolve(target);
|
|
674
|
+
if (!existsSync(root)) failWith("扫描目录不存在:" + root, 2);
|
|
675
|
+
|
|
676
|
+
const { config, warnings } = loadAuditConfig(root);
|
|
677
|
+
for (const warning of warnings) console.error("[ztheme] 警告:" + warning);
|
|
678
|
+
const rules = loadAuditRules(root);
|
|
679
|
+
|
|
680
|
+
const flagTheme = valueOf("theme");
|
|
681
|
+
const envTheme = process.env.ZTHEME_THEME;
|
|
682
|
+
const configTheme = typeof config.theme === "string" ? config.theme : undefined;
|
|
683
|
+
const lock = readLockFile(root);
|
|
684
|
+
const slug =
|
|
685
|
+
(flagTheme && flagTheme.trim()) ||
|
|
686
|
+
(envTheme && envTheme.trim()) ||
|
|
687
|
+
configTheme ||
|
|
688
|
+
lock?.slug ||
|
|
689
|
+
null;
|
|
690
|
+
|
|
691
|
+
const rawMax = valueOf("max-warnings") ?? process.env.ZTHEME_MAX_WARNINGS ?? config.maxWarnings;
|
|
692
|
+
let maxWarnings = null;
|
|
693
|
+
if (rawMax !== undefined && rawMax !== null && String(rawMax).trim() !== "") {
|
|
694
|
+
const parsed = Number(rawMax);
|
|
695
|
+
if (!Number.isInteger(parsed) || parsed < 0) {
|
|
696
|
+
failWith("--max-warnings 需要非负整数", 2);
|
|
697
|
+
}
|
|
698
|
+
maxWarnings = parsed;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
const theme = slug ? await loadAuditTheme(root, slug) : null;
|
|
702
|
+
const result = await auditProject({
|
|
703
|
+
root,
|
|
704
|
+
include: config.include,
|
|
705
|
+
exclude: config.exclude,
|
|
706
|
+
severity: config.severity,
|
|
707
|
+
ignore: config.ignore,
|
|
708
|
+
rules,
|
|
709
|
+
theme,
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
if (flags.has("json")) console.log(formatJson(result));
|
|
713
|
+
else console.log(formatHuman(result, { quiet: flags.has("quiet") }));
|
|
714
|
+
|
|
715
|
+
if (result.errors.length > 0) {
|
|
716
|
+
process.exitCode = 2;
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
const overBudget = maxWarnings !== null && result.summary.warnings > maxWarnings;
|
|
720
|
+
if (result.summary.errors > 0 || overBudget) process.exitCode = 1;
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
|
|
493
724
|
die("未知命令 " + cmd + "(用 --help 看用法)");
|
|
494
725
|
};
|
|
495
726
|
|