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/README.md
CHANGED
|
@@ -4,63 +4,267 @@
|
|
|
4
4
|
> CLI 是本仓原创代码,与 upstream tweakcn 无关。
|
|
5
5
|
|
|
6
6
|
不依赖 shadcn 的主题 CLI。只走 HTTP,不需要 shadcn CLI、不需要 MCP、不需要登录。
|
|
7
|
+
当前版本 **0.0.7**(`npm i -g ztheme-cli` 或 `npx ztheme-cli@0.0.7` 都行,下文统一写 `ztheme`)。
|
|
7
8
|
|
|
8
|
-
##
|
|
9
|
+
## 前提:告诉它站点在哪
|
|
9
10
|
|
|
10
11
|
```bash
|
|
11
|
-
#
|
|
12
|
-
|
|
12
|
+
export ZTHEME_BASE=https://你的站点 # 默认 http://localhost:3001
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
CLI **自己不算**对比度:它把解析出的变量 `POST` 给站点的 `/r/check`,和仓库门禁
|
|
16
|
+
`check:presets`、规则接口 `/r/themes/<slug>/rules`、编辑器面板是**同一份实现**。
|
|
17
|
+
所以站点版本要够新,否则会明确报「站点没有 /r/check 接口(版本过旧?)」——不会退回本地估算。
|
|
18
|
+
|
|
19
|
+
## 1. 挑主题
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
ztheme list # 列出全部主题(对本地站点实测输出「共 42 套」)
|
|
23
|
+
ztheme show modern-minimal # 单套完整 JSON(light / dark 全部令牌)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 2. 落进项目
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
ztheme add modern-minimal # 生成 ./ztheme/modern-minimal.css
|
|
30
|
+
ztheme add modern-minimal --print # 直接打到 stdout,不落盘
|
|
31
|
+
ztheme add modern-minimal --patch globals.css --dry-run # 只报告会改什么
|
|
32
|
+
ztheme add modern-minimal --patch globals.css # 真正写入
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`--patch` 用 `/* ztheme:begin <slug> */` … `/* ztheme:end <slug> */` 标记块定位,**幂等**:
|
|
36
|
+
重复执行只替换同名块,不重复追加,也不碰标记外的任何一行。
|
|
37
|
+
输出目录可以用 `--out <dir>` 改,默认 `./ztheme/`。
|
|
38
|
+
|
|
39
|
+
## 3. 校验你自己的 CSS(进 CI 的就是这条)
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
ztheme check --file app/globals.css # 只报告
|
|
43
|
+
ztheme check --file app/globals.css --strict # 不达标 / 无法判定 -> exit 1
|
|
44
|
+
ztheme check --file src/ # 接受目录,一次跑完,末尾给合计
|
|
45
|
+
ztheme check bold-tech --strict # 直接校验站点上的某套预设
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
实测输出长这样:
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
modern-minimal:dark 缺 1 个键
|
|
52
|
+
modern-minimal:检查 5 组配对(边界 2.6,站点契约)
|
|
53
|
+
对比度全部达标
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**三档结果,别混着看**:
|
|
57
|
+
|
|
58
|
+
| 档 | 含义 | `--strict` |
|
|
59
|
+
|---|---|---|
|
|
60
|
+
| 建议修 | 低于边界(默认 2.6),真读不清 | exit 1 |
|
|
61
|
+
| 边界 | 2.6 到阈值之间,肉眼基本看不出 | 只提示 |
|
|
62
|
+
| 无法判定 | `color-mix()`、`transparent`、半透明色——没有底色算不出真实对比度 | **exit 1** |
|
|
63
|
+
|
|
64
|
+
第三档在 `--strict` 下也算失败是刻意的:0.0.2 时代这些配色被静默跳过,然后打印
|
|
65
|
+
「对比度全部达标」。**假绿比不检查更危险**。
|
|
66
|
+
|
|
67
|
+
## 4. 连规则断言一起校验
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
ztheme check --file app/globals.css --rules modern-minimal --strict
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
除了对比度,还会判这套主题的**可判定断言**:圆角档位(control / card / modal)、字距、
|
|
74
|
+
深浅底明暗。实测输出「规则断言 6 条全部满足」;把 `--radius` 改成 `1.5rem` 会逐条报错并 exit 1。
|
|
75
|
+
判不了的规则(比如「衬线标题旁不要用同字重」)不以断言形式出现,它们留在规则的
|
|
76
|
+
`advisory` 里当人写建议(接口会标注 level: advisory)——判不了的东西写成断言,只会造一批永远 unknown 的假门。
|
|
77
|
+
|
|
78
|
+
## 5. 建立「本项目设计系统」:`init`
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
ztheme init modern-minimal # 写到当前目录
|
|
82
|
+
ztheme init modern-minimal --dir . # 显式指定项目根
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
产物三件套:
|
|
86
|
+
|
|
87
|
+
| 文件 | 内容 |
|
|
88
|
+
|---|---|
|
|
89
|
+
| `ztheme/<slug>.css` | `:root` + `.dark` 两段 CSS 变量 |
|
|
90
|
+
| `AGENTS.md` | 一个幂等标记块:令牌表 + 可判定断言 + 禁用组合 + **改完必须跑的那条命令** |
|
|
91
|
+
| `.ztheme/theme-lock.json` | slug + 主题 digest + 阈值 + 断言 + 锁定时刻的判定结果 |
|
|
92
|
+
|
|
93
|
+
`AGENTS.md` 里写的是**相对项目根**的路径(`ztheme/<slug>.css`),可以直接提交进仓库。
|
|
94
|
+
(0.0.4 曾把本机绝对路径写进去,换台机器就跑不了;0.0.5 修了,并有测试守着。)
|
|
95
|
+
|
|
96
|
+
## 6. 防「站点主题更新了、项目还停在旧版」:`check --lock`
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
ztheme check --lock --file ztheme/modern-minimal.css
|
|
100
|
+
ztheme check --lock --file ztheme/modern-minimal.css --strict
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
报两类漂移:① 本地 CSS 比锁定版本变差;② 站点上那套主题的 digest 变了。
|
|
104
|
+
正常时输出「无漂移:主题与锁定版本一致」;`--strict` 下有漂移即 exit 1。
|
|
105
|
+
|
|
106
|
+
## 7. CI 里最小接入
|
|
13
107
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
node cli/ztheme.mjs add modern-minimal --patch globals.css # 写进标记块(幂等)
|
|
19
|
-
node cli/ztheme.mjs add modern-minimal --patch globals.css --dry-run # 只报告会改什么
|
|
20
|
-
node cli/ztheme.mjs check bold-tech --strict # 亮暗成对 + 对比度自查
|
|
21
|
-
node cli/ztheme.mjs check --file app/globals.css --strict # 校验你自己的 CSS(可当 CI 门禁)
|
|
108
|
+
```yaml
|
|
109
|
+
- run: npx ztheme-cli@0.0.7 check --file app/globals.css --rules 你的主题 --strict
|
|
110
|
+
env:
|
|
111
|
+
ZTHEME_BASE: https://你的生产站点
|
|
22
112
|
```
|
|
23
113
|
|
|
24
|
-
|
|
25
|
-
(与仓库门禁 `check:presets` 共用 `data/preset-checks.json`),站点改阈值 CLI 自动跟随。
|
|
26
|
-
分两档报告:低于边界(默认 2.6)记「建议修」,边界到阈值之间记「边界」;
|
|
27
|
-
`--strict` 时两档都会 exit 1,不带时只打印。
|
|
114
|
+
## 8. 仓库级自查:`audit`
|
|
28
115
|
|
|
29
|
-
`
|
|
30
|
-
|
|
116
|
+
`check` 看的是 CSS 令牌文件;`audit` 看的是组件源码——AI 最容易写坏的地方。
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
ztheme audit . # 人类摘要,前 10 条
|
|
120
|
+
ztheme audit . --json # stdout 只输出 JSON,适合 agent / CI
|
|
121
|
+
ztheme audit . --max-warnings 0 # 有告警就 exit 1
|
|
122
|
+
ztheme audit . --theme modern-minimal
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
扫四类:`bg-[#…]` / `text-[#…]` 这类任意色值、`p-[13px]` / `rounded-[7px]` 这类任意尺寸、
|
|
126
|
+
`style={{ color: "#666" }}` 这类行内颜色,以及可选的裸色值(默认关闭)。
|
|
127
|
+
命中颜色时会给最近的 token 建议(OKLab 距离,< 10 才给),只报告不改代码。
|
|
128
|
+
|
|
129
|
+
主题来源:`--theme` > `.ztheme/audit.json` 的 `theme` > lock。本地有
|
|
130
|
+
`ztheme/<slug>.css` 时离线读;没有才回站点拉。拉不到只关闭建议,不阻断扫描。
|
|
131
|
+
本地想看到 token 建议:先 `npm run dev`(默认 `ZTHEME_BASE` 就是 `http://localhost:3001`),
|
|
132
|
+
再用 `--theme <slug>`;或先 `ztheme init <slug>` 生成 lock 与本地 CSS。
|
|
133
|
+
|
|
134
|
+
> Windows PowerShell 5.1 的 `>` 会把输出写成 UTF-16,后续 JSON 解析会失败。写文件请用
|
|
135
|
+
> `ztheme audit . --json | Out-File -Encoding utf8 audit.json`;bash、macOS 与 PowerShell 7 的 `>` 没这个问题。
|
|
136
|
+
|
|
137
|
+
配置(`.ztheme/audit.json`):
|
|
138
|
+
|
|
139
|
+
```json
|
|
140
|
+
{
|
|
141
|
+
"theme": "modern-minimal",
|
|
142
|
+
"exclude": ["components/ui", "**/*.test.tsx"],
|
|
143
|
+
"severity": { "no-arbitrary-size": "warn", "no-inline-color": "error" },
|
|
144
|
+
"ignore": [{ "rule": "no-arbitrary-color", "file": "app/experiments/**" }],
|
|
145
|
+
"maxWarnings": 50
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
行内抑制:
|
|
150
|
+
|
|
151
|
+
```tsx
|
|
152
|
+
// ztheme-ignore-next-line no-arbitrary-color
|
|
153
|
+
<div className="bg-[#3b82f6]" />
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 项目规则:`.ztheme/rules.json`
|
|
157
|
+
|
|
158
|
+
audit 默认只把 `rounded-[20px]`、`p-[13px]` 当成「任意值」。要让它按你的设计系统判断,
|
|
159
|
+
在 `.ztheme/rules.json` 声明阈值:
|
|
160
|
+
|
|
161
|
+
```json
|
|
162
|
+
{
|
|
163
|
+
"schemaVersion": 1,
|
|
164
|
+
"radius": { "maxRem": 0.5 },
|
|
165
|
+
"spacing": { "baseUnit": 4 }
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
| 规则 | 触发 | 例子 |
|
|
170
|
+
|---|---|---|
|
|
171
|
+
| `no-excessive-radius` | 声明 `radius.maxRem` 后,圆角超过上限 | `rounded-[20px]`(1.25rem > 0.5rem)→ 建议 `rounded-lg` |
|
|
172
|
+
| `no-off-grid-spacing` | 声明 `spacing.baseUnit` 后,间距不是基准的整数倍 | `p-[13px]` → 建议 `p-3` |
|
|
173
|
+
|
|
174
|
+
判定落在 `rounded-*` 与 `p/m/gap/space-*` 上。**合规的值不再报**(规则的价值就是降噪);
|
|
175
|
+
判不了的值(`calc()`、`var()`、百分比、`em`、负值)退回通用规则,不静默放行。
|
|
176
|
+
`w-*` / `h-*` / `size-*` / `text-*` 不受间距政策约束——布局尺寸常常是有意的。
|
|
177
|
+
|
|
178
|
+
严重级别仍然只写在 `.ztheme/audit.json` 的 `severity` 里。`rules.json` 写坏(类型错、不是
|
|
179
|
+
JSON)会让 audit 以退出码 2 结束,而不是当作「没有规则」继续扫。
|
|
180
|
+
|
|
181
|
+
> 阈值不自动从主题派生:「主题圆角 0.5rem」推不出「这个项目只准用这么大」,后者是团队的决定。
|
|
182
|
+
|
|
183
|
+
在 PR 里自动评论(复用 workflow,不单独装 App):
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
```yaml
|
|
188
|
+
name: PR
|
|
189
|
+
on: pull_request
|
|
190
|
+
jobs:
|
|
191
|
+
design:
|
|
192
|
+
permissions:
|
|
193
|
+
contents: read
|
|
194
|
+
pull-requests: write
|
|
195
|
+
uses: zuoyunlai/Ztheme/.github/workflows/ztheme-audit.yml@main
|
|
196
|
+
with:
|
|
197
|
+
theme: modern-minimal
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
workflow 按 `<!-- ztheme-audit -->` 更新同一条评论,不刷屏;完整 JSON 在 artifact。
|
|
201
|
+
静态扫描的边界:不解析动态拼接的 className,不做 AST;`components/ui/`、品牌图标目录、
|
|
202
|
+
生成的图片路由默认跳过;字号任意值与 `ch`/`em` 相对单位 v1 不报。
|
|
203
|
+
|
|
204
|
+
## 命令速查
|
|
205
|
+
|
|
206
|
+
| 命令 | 作用 |
|
|
207
|
+
|---|---|
|
|
208
|
+
| `list` | 列出全部主题 |
|
|
209
|
+
| `show <slug>` | 打印单套主题 JSON |
|
|
210
|
+
| `add <slug> [--out dir] [--print]` | 生成 `<dir>/<slug>.css` |
|
|
211
|
+
| `add <slug> --patch <css> [--dry-run]` | 写进标记块(幂等) |
|
|
212
|
+
| `check <slug> [--strict]` | 校验站点上的预设 |
|
|
213
|
+
| `check --file <path> [--strict]` | 校验本地 CSS(也接受目录),可当 CI 门禁 |
|
|
214
|
+
| `check --file <path> --rules <slug>` | 同时校验该主题的规则断言 |
|
|
215
|
+
| `init <slug> [--dir .]` | 生成 CSS + `AGENTS.md` 片段 + 主题锁 |
|
|
216
|
+
| `check --lock [--file <css>]` | 与锁文件比对,报漂移 |
|
|
217
|
+
| `audit [path] [--json] [--theme <slug>] [--max-warnings n]` | 扫组件源码里的硬编码颜色与任意值 |
|
|
218
|
+
| `--help` | 打印用法 |
|
|
219
|
+
|
|
220
|
+
退出码:`0` 通过;`1` `--strict` 下出现「建议修」或「无法判定」,或有漂移,或 audit 有
|
|
221
|
+
error / 超过 `--max-warnings`;`2` audit 的参数、配置或扫描错误。
|
|
31
222
|
|
|
32
223
|
## 为什么这么小
|
|
33
224
|
|
|
34
|
-
- **零依赖**:只用 Node 内置 `fetch
|
|
35
|
-
-
|
|
36
|
-
|
|
37
|
-
|
|
225
|
+
- **零依赖**:只用 Node 内置 `fetch`、`node:fs`、`node:crypto`
|
|
226
|
+
- **算法只有一份,在服务端**:CLI 不自带亮度计算。多一份实现就多一个漂移源——
|
|
227
|
+
实测 0.0.2 的 CLI 认 hex / oklch 但不认 rgb / hsl / 命名色,于是把不达标配色判成「全部达标」
|
|
228
|
+
- **改文件只改自己那块**:默认只生成新文件;`--patch` 只动标记块内
|
|
38
229
|
- **数据面与站点同一套**:`/r/themes/<slug>` 的 `cssVars` 就是站点门禁校验过的产物
|
|
39
230
|
|
|
40
|
-
##
|
|
231
|
+
## 变更记录
|
|
232
|
+
|
|
233
|
+
### 0.0.7
|
|
41
234
|
|
|
42
|
-
-
|
|
43
|
-
`
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
-
|
|
48
|
-
变量会被静默丢掉。
|
|
49
|
-
- **`check --file` 支持目录**:一次 CI 跑完整个项目,最后给一条合计。
|
|
235
|
+
- 新增项目规则 `.ztheme/rules.json`:`radius.maxRem` 与 `spacing.baseUnit` 两条可声明阈值,
|
|
236
|
+
对应 audit 规则 `no-excessive-radius` / `no-off-grid-spacing`。
|
|
237
|
+
- findings 增加 `policy` / `expected` / `actual`;人类输出直接显示期望值(如「期望 4 的倍数」
|
|
238
|
+
并建议 `p-3`)。
|
|
239
|
+
- 规则判定合规的任意值不再报(降噪);判不了的值仍交给通用规则,不静默放行。
|
|
240
|
+
- `rules.json` 类型错误或不是 JSON 时退出码为 2,不会当作「没有规则」继续扫。
|
|
50
241
|
|
|
51
|
-
|
|
242
|
+
### 0.0.6
|
|
243
|
+
|
|
244
|
+
- 新增 `audit`:扫描 TSX/JSX/TS/JS 里的任意色值、任意尺寸、行内颜色;命中颜色时给最近的
|
|
245
|
+
token 建议;`--json` 供 agent / CI 消费;默认只告警。零依赖、纯静态扫描。
|
|
246
|
+
- `init` 的 `AGENTS.md` 增加 `npx ztheme-cli audit --json` 自查命令。
|
|
247
|
+
- 新增可复用 workflow `.github/workflows/ztheme-audit.yml`:PR 评论 + artifact + 真实退出码。
|
|
248
|
+
|
|
249
|
+
### 0.0.5
|
|
52
250
|
|
|
53
251
|
- `init` 打印的 `@import` 与写进 `AGENTS.md` 的验收命令**改成相对项目根的路径**。
|
|
54
252
|
0.0.4 在 `--dir` 传绝对路径时会写出本机绝对路径,那条命令贴进别人的仓库就跑不了。
|
|
55
253
|
|
|
56
|
-
|
|
254
|
+
### 0.0.4
|
|
255
|
+
|
|
256
|
+
- `init <slug>`:一条命令建立「本项目设计系统」——生成 `ztheme/<slug>.css`、
|
|
257
|
+
往 `AGENTS.md` 写幂等标记块、生成 `.ztheme/theme-lock.json`
|
|
258
|
+
- `check --lock`:本地 CSS 变差、或站点主题已更新都报漂移;`--strict` 判失败
|
|
259
|
+
- 锁文件记的是**判定结果**(fails / warns / unknown + 断言),不是「差不多能过」
|
|
57
260
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
-
|
|
261
|
+
### 0.0.3
|
|
262
|
+
|
|
263
|
+
- **判定交给站点**:`check` 不再自带亮度算法,改为 `POST /r/check`。0.0.2 各自算一份,
|
|
264
|
+
实测 hex 判失败、rgb / hsl / 命名色却打印「对比度全部达标」
|
|
265
|
+
- **无法判定不再静默跳过**:单独列出,`--strict` 下算失败
|
|
266
|
+
- **CSS 变量解析不再要求末尾分号**:原来靠带分号的正则,globals.css 最后一行的变量会被静默丢掉
|
|
267
|
+
- **`check --file` 支持目录**:一次 CI 跑完整个项目,末尾给一条合计
|
|
64
268
|
|
|
65
269
|
## 尚未做
|
|
66
270
|
|
|
@@ -69,15 +273,24 @@ node cli/ztheme.mjs check --file app/globals.css --strict # 校验你自己的
|
|
|
69
273
|
|
|
70
274
|
## 发版(维护者)
|
|
71
275
|
|
|
72
|
-
本机 `~/.npmrc`
|
|
73
|
-
|
|
74
|
-
`
|
|
276
|
+
本机 `~/.npmrc` 默认指向 `registry.npmmirror.com`(国内镜像)。
|
|
277
|
+
镜像不提供写接口,打过去会返回 `401 Unauthorized` / `ENEEDAUTH`——那不是没登录。
|
|
278
|
+
`cli/package.json` 已用 `publishConfig.registry` 钉住官方 registry,所以直接发即可:
|
|
75
279
|
|
|
76
280
|
```bash
|
|
77
281
|
cd cli
|
|
282
|
+
npm publish
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
如果换到别的包或本字段被删掉,才需要显式指定:
|
|
286
|
+
|
|
287
|
+
```bash
|
|
78
288
|
npm publish --registry=https://registry.npmjs.org
|
|
79
289
|
```
|
|
80
290
|
|
|
291
|
+
注意语法:`npm publish` 后面跟的是**本地目录或 tarball**。写成 `npm publish ztheme-cli@0.0.5`
|
|
292
|
+
会被当成「去 registry 找一个还不存在的包」,报 `ETARGET`——先有鸡还是先有蛋。
|
|
293
|
+
|
|
81
294
|
账号是 `auth-and-writes` 2FA 时,写操作会要求一次性密码:
|
|
82
295
|
|
|
83
296
|
```
|
|
@@ -85,11 +298,13 @@ npm error code EOTP
|
|
|
85
298
|
npm error Open this URL in your browser to authenticate:
|
|
86
299
|
```
|
|
87
300
|
|
|
88
|
-
在**能交互的终端**里跑,按提示打开 URL 或输入 6 位 OTP。CI /
|
|
89
|
-
|
|
90
|
-
|
|
301
|
+
在**能交互的终端**里跑,按提示打开 URL 或输入 6 位 OTP。CI / 非交互环境要绕过这道门,
|
|
302
|
+
得用 npmjs.com 生成的 **Automation** token(该类 token 不受 OTP 拦截),
|
|
303
|
+
写进 `~/.npmrc` 的 `//registry.npmjs.org/:_authToken`。
|
|
91
304
|
|
|
92
|
-
|
|
305
|
+
**「CLI 打印了 + 包名@版本」不等于「registry 能查到」**:实测 0.0.4 要轮询 5 次、
|
|
306
|
+
0.0.5 要轮询 6 次(约 100 秒)才出现。发布后用官方 registry 核对,
|
|
307
|
+
别信 `npm view` 的默认值(本机 registry 指向镜像时读到的是镜像值):
|
|
93
308
|
|
|
94
309
|
```bash
|
|
95
310
|
curl -s https://registry.npmjs.org/ztheme-cli | node -e "let s='';process.stdin.on('data',d=>s+=d).on('end',()=>{const j=JSON.parse(s);console.log(j['dist-tags'].latest,Object.keys(j.versions).join(','))})"
|