ztheme-cli 0.0.2
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/LICENSE +21 -0
- package/README.md +32 -0
- package/package.json +29 -0
- package/ztheme.mjs +210 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zuoyunlai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# ztheme CLI
|
|
2
|
+
|
|
3
|
+
不依赖 shadcn 的主题 CLI。只走 HTTP,不需要 shadcn CLI、不需要 MCP、不需要登录。
|
|
4
|
+
|
|
5
|
+
## 用法
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# 站点地址(默认 http://localhost:3001)
|
|
9
|
+
export ZTHEME_BASE=https://你的站点
|
|
10
|
+
|
|
11
|
+
node cli/ztheme.mjs list # 列出 42 套
|
|
12
|
+
node cli/ztheme.mjs show modern-minimal # 打印单套 JSON
|
|
13
|
+
node cli/ztheme.mjs add modern-minimal # 生成 ztheme/modern-minimal.css
|
|
14
|
+
node cli/ztheme.mjs add modern-minimal --print # 直接输出到 stdout
|
|
15
|
+
node cli/ztheme.mjs check bold-tech --strict # 亮暗成对 + 对比度自查
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`add` 默认写到 `./ztheme/<slug>.css`,内容是 `:root` 与 `.dark` 两段 CSS 变量。
|
|
19
|
+
把它 `@import` 到 `globals.css`(放在 Tailwind 之后)即可。
|
|
20
|
+
|
|
21
|
+
## 为什么这么小
|
|
22
|
+
|
|
23
|
+
- **零依赖**:只用 Node 内置 `fetch` 与 `node:fs`,不引入 commander / chalk 之类
|
|
24
|
+
- **不改你的文件**:只生成新文件并打印 import 行,不猜你的 `globals.css` 结构
|
|
25
|
+
(自动 patch 会因 Tailwind v3/v4 布局差异而错,后续可加 `--patch` 但需 dry-run)
|
|
26
|
+
- **数据面与站点同一套**:`/r/themes/<slug>` 的 `cssVars` 就是站点门禁校验过的产物
|
|
27
|
+
|
|
28
|
+
## 尚未做
|
|
29
|
+
|
|
30
|
+
- 发布到 npm(需要定包名、跑 tarball 安装验证)
|
|
31
|
+
- `--patch` 直接改 globals.css(要做 `:root`/`.dark` 定位与幂等标记)
|
|
32
|
+
- `init` 脚手架(写 import、提示 Tailwind v4 的 `@theme inline` 映射)
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ztheme-cli",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Ztheme 主题 CLI:不依赖 shadcn,从站点直接拉取主题 CSS 变量",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"ztheme": "ztheme.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"ztheme.mjs",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/zuoyunlai/Ztheme.git"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"ztheme",
|
|
24
|
+
"shadcn",
|
|
25
|
+
"theme",
|
|
26
|
+
"css-variables",
|
|
27
|
+
"cli"
|
|
28
|
+
]
|
|
29
|
+
}
|
package/ztheme.mjs
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* ztheme —— 不依赖 shadcn 的主题 CLI。
|
|
4
|
+
*
|
|
5
|
+
* 只做三件事,全部走 HTTP:读索引、看单套、落到本地 CSS。
|
|
6
|
+
* 不用 shadcn CLI、不用 MCP、不需要登录,只要求 Node 18+(用内置 fetch)。
|
|
7
|
+
*
|
|
8
|
+
* node cli/ztheme.mjs list
|
|
9
|
+
* node cli/ztheme.mjs show modern-minimal
|
|
10
|
+
* node cli/ztheme.mjs add modern-minimal [--out ztheme] [--print]
|
|
11
|
+
* node cli/ztheme.mjs check modern-minimal [--strict]
|
|
12
|
+
*
|
|
13
|
+
* 默认站点取 ZTHEME_BASE 环境变量,否则 http://localhost:3001。
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
16
|
+
import { join } from "node:path";
|
|
17
|
+
|
|
18
|
+
const BASE = (process.env.ZTHEME_BASE ?? "http://localhost:3001").replace(/\/$/, "");
|
|
19
|
+
|
|
20
|
+
const argv = process.argv.slice(2);
|
|
21
|
+
const cmd = argv[0];
|
|
22
|
+
const positionals = argv.slice(1).filter((a) => !a.startsWith("--"));
|
|
23
|
+
const flags = new Set(argv.filter((a) => a.startsWith("--")).map((a) => a.replace(/^--/, "")));
|
|
24
|
+
const outDir = (() => {
|
|
25
|
+
const i = argv.indexOf("--out");
|
|
26
|
+
return i >= 0 ? argv[i + 1] : "ztheme";
|
|
27
|
+
})();
|
|
28
|
+
|
|
29
|
+
const die = (msg) => {
|
|
30
|
+
console.error("[ztheme] " + msg);
|
|
31
|
+
process.exit(1);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const getJson = async (path) => {
|
|
35
|
+
const res = await fetch(BASE + path);
|
|
36
|
+
if (!res.ok) die("请求失败 " + res.status + ":" + BASE + path);
|
|
37
|
+
return res.json();
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const TOKENS = [
|
|
41
|
+
"background", "foreground", "card", "card-foreground", "popover", "popover-foreground",
|
|
42
|
+
"primary", "primary-foreground", "secondary", "secondary-foreground",
|
|
43
|
+
"muted", "muted-foreground", "accent", "accent-foreground",
|
|
44
|
+
"destructive", "destructive-foreground", "border", "input", "ring",
|
|
45
|
+
"radius", "font-sans", "font-serif", "font-mono",
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
const block = (selector, vars) => {
|
|
49
|
+
const lines = TOKENS.filter((t) => vars[t] !== undefined && vars[t] !== "")
|
|
50
|
+
.map((t) => " --" + t + ": " + String(vars[t]) + ";");
|
|
51
|
+
return selector + " {\n" + lines.join("\n") + "\n}";
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const toCss = (slug, item) => {
|
|
55
|
+
const cssVars = item.cssVars ?? {};
|
|
56
|
+
return [
|
|
57
|
+
"/* ztheme: " + slug + " —— 由 " + BASE + "/r/themes/" + slug + " 生成,勿手改 */",
|
|
58
|
+
block(":root", cssVars.light ?? {}),
|
|
59
|
+
"",
|
|
60
|
+
block(".dark", cssVars.dark ?? {}),
|
|
61
|
+
"",
|
|
62
|
+
].join("\n");
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// —— 对比度(校验用,与站点门禁同一套阈值)——
|
|
66
|
+
const luminance = (v) => {
|
|
67
|
+
const s = String(v).trim();
|
|
68
|
+
const hex = s.match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i);
|
|
69
|
+
if (hex) {
|
|
70
|
+
let h = hex[1];
|
|
71
|
+
if (h.length === 3) h = h.split("").map((c) => c + c).join("");
|
|
72
|
+
const lin = (c) => (c / 255 <= 0.04045 ? c / 255 / 12.92 : Math.pow((c / 255 + 0.055) / 1.055, 2.4));
|
|
73
|
+
return 0.2126 * lin(parseInt(h.slice(0, 2), 16)) + 0.7152 * lin(parseInt(h.slice(2, 4), 16)) + 0.0722 * lin(parseInt(h.slice(4, 6), 16));
|
|
74
|
+
}
|
|
75
|
+
const ok = s.match(/^oklch\(\s*([\d.]+%?)[ ,]+([\d.]+)[ ,]+([\d.]+)/i);
|
|
76
|
+
if (!ok) return null;
|
|
77
|
+
const L = ok[1].endsWith("%") ? parseFloat(ok[1]) / 100 : parseFloat(ok[1]);
|
|
78
|
+
const C = parseFloat(ok[2]);
|
|
79
|
+
const H = (parseFloat(ok[3]) * Math.PI) / 180;
|
|
80
|
+
const a = C * Math.cos(H), b = C * Math.sin(H);
|
|
81
|
+
const l_ = L + 0.3963377774 * a + 0.2158037573 * b;
|
|
82
|
+
const m_ = L - 0.1055613458 * a - 0.0638541728 * b;
|
|
83
|
+
const s_ = L - 0.0894841775 * a - 1.291485548 * b;
|
|
84
|
+
const l3 = l_ ** 3, m3 = m_ ** 3, s3 = s_ ** 3;
|
|
85
|
+
const R = 4.0767416621 * l3 - 3.3077115913 * m3 + 0.2309699292 * s3;
|
|
86
|
+
const G = -1.2684380046 * l3 + 2.6097574011 * m3 - 0.3413193965 * s3;
|
|
87
|
+
const B = -0.0041960863 * l3 - 0.7034186147 * m3 + 1.707614701 * s3;
|
|
88
|
+
const clamp = (x) => Math.max(0, Math.min(1, x));
|
|
89
|
+
return 0.2126 * clamp(R) + 0.7152 * clamp(G) + 0.0722 * clamp(B);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const ratio = (fg, bg) => {
|
|
93
|
+
const a = luminance(fg), b = luminance(bg);
|
|
94
|
+
if (a === null || b === null) return null;
|
|
95
|
+
return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const main = async () => {
|
|
99
|
+
if (!cmd || cmd === "help" || flags.has("help")) {
|
|
100
|
+
console.log(`ztheme CLI(不依赖 shadcn)
|
|
101
|
+
用法:
|
|
102
|
+
ztheme list 列出全部主题
|
|
103
|
+
ztheme show <slug> 打印单套主题的 JSON
|
|
104
|
+
ztheme add <slug> [--out dir] [--print]
|
|
105
|
+
生成 <dir>/<slug>.css(默认 ztheme/),--print 直接输出
|
|
106
|
+
ztheme add <slug> --patch globals.css [--dry-run]
|
|
107
|
+
把主题写进 globals.css 的标记块(幂等);
|
|
108
|
+
--dry-run 只报告会改什么,不落盘
|
|
109
|
+
ztheme check <slug> [--strict] 检查亮暗成对与对比度;--strict 时有不达标即 exit 1
|
|
110
|
+
环境变量:
|
|
111
|
+
ZTHEME_BASE 站点地址,默认 ${BASE}`);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (cmd === "list") {
|
|
116
|
+
const reg = await getJson("/r/themes/registry.json");
|
|
117
|
+
const items = reg.items ?? [];
|
|
118
|
+
console.log("共 " + items.length + " 套(" + BASE + ")");
|
|
119
|
+
for (const it of items) console.log(" " + it.name);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (cmd === "show") {
|
|
124
|
+
const slug = positionals[0] ?? die("缺少 slug");
|
|
125
|
+
const item = await getJson("/r/themes/" + slug);
|
|
126
|
+
console.log(JSON.stringify(item, null, 2));
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (cmd === "add") {
|
|
131
|
+
const slug = positionals[0] ?? die("缺少 slug");
|
|
132
|
+
const item = await getJson("/r/themes/" + slug);
|
|
133
|
+
const css = toCss(slug, item);
|
|
134
|
+
|
|
135
|
+
// --patch:把主题包进标记块,幂等;--dry-run 只报不写
|
|
136
|
+
const patchIdx = argv.indexOf("--patch");
|
|
137
|
+
if (patchIdx >= 0) {
|
|
138
|
+
const target = argv[patchIdx + 1];
|
|
139
|
+
if (!target) die("--patch 需要一个 globals.css 路径");
|
|
140
|
+
if (!existsSync(target)) die("文件不存在:" + target);
|
|
141
|
+
const begin = "/* ztheme:begin " + slug + " */";
|
|
142
|
+
const end = "/* ztheme:end " + slug + " */";
|
|
143
|
+
const marker = begin + "\n" + css + end + "\n";
|
|
144
|
+
const raw = readFileSync(target, "utf8");
|
|
145
|
+
const bIdx = raw.indexOf(begin);
|
|
146
|
+
const eIdx = raw.indexOf(end);
|
|
147
|
+
let next;
|
|
148
|
+
let action;
|
|
149
|
+
if (bIdx >= 0 && eIdx > bIdx) {
|
|
150
|
+
next = raw.slice(0, bIdx) + marker + raw.slice(eIdx + end.length + 1);
|
|
151
|
+
action = "replace";
|
|
152
|
+
} else {
|
|
153
|
+
const sep = raw.endsWith("\n") ? "\n" : "\n\n";
|
|
154
|
+
next = raw + sep + marker;
|
|
155
|
+
action = "append";
|
|
156
|
+
}
|
|
157
|
+
const delta = next.length - raw.length;
|
|
158
|
+
if (flags.has("dry-run")) {
|
|
159
|
+
console.log("[dry-run] " + target + " → " + action + " 标记块(" + begin + "),字节变化 " + (delta >= 0 ? "+" : "") + delta);
|
|
160
|
+
console.log("[dry-run] 未写入任何文件");
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
writeFileSync(target, next);
|
|
164
|
+
console.log("已" + (action === "append" ? "追加到" : "替换进") + " " + target + "(标记块 " + slug + ")");
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (flags.has("print")) {
|
|
169
|
+
console.log(css);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
mkdirSync(outDir, { recursive: true });
|
|
173
|
+
const file = join(outDir, slug + ".css");
|
|
174
|
+
writeFileSync(file, css);
|
|
175
|
+
console.log("已写入 " + file);
|
|
176
|
+
console.log("在 globals.css 顶层加一行(注意放在 Tailwind 之后,覆盖优先):");
|
|
177
|
+
console.log(" @import \"./" + file.replace(/\\/g, "/") + "\";");
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (cmd === "check") {
|
|
182
|
+
const slug = positionals[0] ?? die("缺少 slug");
|
|
183
|
+
const item = await getJson("/r/themes/" + slug);
|
|
184
|
+
const light = item.cssVars?.light ?? {};
|
|
185
|
+
const dark = item.cssVars?.dark ?? {};
|
|
186
|
+
const lk = Object.keys(light).sort(), dk = Object.keys(dark).sort();
|
|
187
|
+
const missing = lk.filter((k) => !dk.includes(k));
|
|
188
|
+
const problems = [];
|
|
189
|
+
const pairs = [["background", "foreground", 4.5], ["card", "card-foreground", 4.5], ["primary", "primary-foreground", 3], ["secondary", "secondary-foreground", 3]];
|
|
190
|
+
for (const [mode, vars] of [["light", light], ["dark", dark]]) {
|
|
191
|
+
for (const [fg, bg, min] of pairs) {
|
|
192
|
+
const r = ratio(vars[fg], vars[bg]);
|
|
193
|
+
if (r !== null && r < min) problems.push(mode + " " + fg + "/" + bg + "=" + r.toFixed(2) + " < " + min);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
console.log(slug + ":" + (missing.length ? "dark 缺 " + missing.length + " 个键" : "亮暗键集一致"));
|
|
197
|
+
if (problems.length) {
|
|
198
|
+
console.log("对比度不达标 " + problems.length + " 条:");
|
|
199
|
+
for (const p of problems) console.log(" · " + p);
|
|
200
|
+
} else {
|
|
201
|
+
console.log("对比度全部达标");
|
|
202
|
+
}
|
|
203
|
+
if (flags.has("strict") && (problems.length || missing.length)) process.exit(1);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
die("未知命令 " + cmd + "(用 --help 看用法)");
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
main().catch((e) => die(String(e?.message ?? e)));
|