ztheme-cli 0.0.7 → 0.0.8
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 +36 -2
- package/audit.mjs +51 -5
- package/fix.mjs +88 -0
- package/package.json +2 -1
- package/ztheme.mjs +128 -5
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
> CLI 是本仓原创代码,与 upstream tweakcn 无关。
|
|
5
5
|
|
|
6
6
|
不依赖 shadcn 的主题 CLI。只走 HTTP,不需要 shadcn CLI、不需要 MCP、不需要登录。
|
|
7
|
-
当前版本 **0.0.
|
|
7
|
+
当前版本 **0.0.8**(`npm i -g ztheme-cli` 或 `npx ztheme-cli@0.0.8` 都行,下文统一写 `ztheme`)。
|
|
8
8
|
|
|
9
9
|
## 前提:告诉它站点在哪
|
|
10
10
|
|
|
@@ -106,7 +106,7 @@ ztheme check --lock --file ztheme/modern-minimal.css --strict
|
|
|
106
106
|
## 7. CI 里最小接入
|
|
107
107
|
|
|
108
108
|
```yaml
|
|
109
|
-
- run: npx ztheme-cli@0.0.
|
|
109
|
+
- run: npx ztheme-cli@0.0.8 check --file app/globals.css --rules 你的主题 --strict
|
|
110
110
|
env:
|
|
111
111
|
ZTHEME_BASE: https://你的生产站点
|
|
112
112
|
```
|
|
@@ -180,6 +180,30 @@ JSON)会让 audit 以退出码 2 结束,而不是当作「没有规则」继
|
|
|
180
180
|
|
|
181
181
|
> 阈值不自动从主题派生:「主题圆角 0.5rem」推不出「这个项目只准用这么大」,后者是团队的决定。
|
|
182
182
|
|
|
183
|
+
### 自动修:`--fix`
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
ztheme audit . --fix # 只打印 patch,不改任何文件
|
|
187
|
+
ztheme audit . --fix --write # 真正落盘,写完自动重扫并报告剩余
|
|
188
|
+
ztheme audit . --fix --json # fixes 数组 + diff,仍然不写盘
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
能自动修的三类:圆角/间距政策命中(换成规则允许的档位)、颜色命中(换成对应的 token 类,
|
|
192
|
+
如 `bg-[#3b82f6]` → `bg-primary`)、行内纯颜色(换成 `var(--token)`)。`style` 里的
|
|
193
|
+
`boxShadow` 这类组合值不修——换掉整段会丢语义。`no-hardcoded-color` 永不自动修:
|
|
194
|
+
在 TS 常量里塞 `var()` 是错的。
|
|
195
|
+
|
|
196
|
+
每条修复带 `confidence`:
|
|
197
|
+
|
|
198
|
+
| 值 | 含义 | 处理建议 |
|
|
199
|
+
|---|---|---|
|
|
200
|
+
| `policy` | 违反了 `.ztheme/rules.json` 声明的上限 | 直接接受 |
|
|
201
|
+
| `exact` | 与某个 token 肉眼同色(ΔE < 2) | 直接接受 |
|
|
202
|
+
| `near` | 近似色或就近取整 | **先看**:dry-run 会把它们单独列出来 |
|
|
203
|
+
|
|
204
|
+
diff 是逐行的(`--- a/` / `+++ b/`),所有替换都不增删换行,所以行号与原文一一对应。
|
|
205
|
+
`--write` 只改命中的那几个字符,文件其余字节(包括 CRLF)不动。
|
|
206
|
+
|
|
183
207
|
在 PR 里自动评论(复用 workflow,不单独装 App):
|
|
184
208
|
|
|
185
209
|
|
|
@@ -215,6 +239,7 @@ workflow 按 `<!-- ztheme-audit -->` 更新同一条评论,不刷屏;完整
|
|
|
215
239
|
| `init <slug> [--dir .]` | 生成 CSS + `AGENTS.md` 片段 + 主题锁 |
|
|
216
240
|
| `check --lock [--file <css>]` | 与锁文件比对,报漂移 |
|
|
217
241
|
| `audit [path] [--json] [--theme <slug>] [--max-warnings n]` | 扫组件源码里的硬编码颜色与任意值 |
|
|
242
|
+
| `audit [path] --fix [--write]` | 打印可审阅的 patch;`--write` 才落盘 |
|
|
218
243
|
| `--help` | 打印用法 |
|
|
219
244
|
|
|
220
245
|
退出码:`0` 通过;`1` `--strict` 下出现「建议修」或「无法判定」,或有漂移,或 audit 有
|
|
@@ -230,6 +255,15 @@ error / 超过 `--max-warnings`;`2` audit 的参数、配置或扫描错误。
|
|
|
230
255
|
|
|
231
256
|
## 变更记录
|
|
232
257
|
|
|
258
|
+
### 0.0.8
|
|
259
|
+
|
|
260
|
+
- 新增 `audit --fix`:把建议落成可审阅的逐行 patch,**默认不写盘**,`--write` 才落盘。
|
|
261
|
+
- 每条修复带 `confidence`(`policy` / `exact` / `near`);近似色与就近取整单列提示。
|
|
262
|
+
- 行内颜色只修纯颜色值(`color: "#666"` → `var(--muted-foreground)`),组合值不碰;
|
|
263
|
+
`no-hardcoded-color` 永不自动修。
|
|
264
|
+
- 修复顺带修正了行内颜色定位偏移:此前 `style` 的 `{{` 之后 8 个字符没算进列号。
|
|
265
|
+
- `--fix --json` 输出 `fixes` / `diff` / `written`,`schemaVersion` 仍为 1。
|
|
266
|
+
|
|
233
267
|
### 0.0.7
|
|
234
268
|
|
|
235
269
|
- 新增项目规则 `.ztheme/rules.json`:`radius.maxRem` 与 `spacing.baseUnit` 两条可声明阈值,
|
package/audit.mjs
CHANGED
|
@@ -206,7 +206,7 @@ function buildPalette(theme) {
|
|
|
206
206
|
return palette;
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
function
|
|
209
|
+
function suggestColorMatch(value, palette) {
|
|
210
210
|
if (!palette.size) return null;
|
|
211
211
|
const parsed = parseColor(value) ?? parseColor(extractColor(value) ?? "");
|
|
212
212
|
if (!parsed) return null;
|
|
@@ -216,7 +216,11 @@ function suggestColor(value, palette) {
|
|
|
216
216
|
if (!best || distance < best.distance) best = { token, distance };
|
|
217
217
|
}
|
|
218
218
|
if (!best || best.distance >= 10) return null;
|
|
219
|
-
return "--" + best.token;
|
|
219
|
+
return { suggestion: "--" + best.token, distance: best.distance };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function suggestColor(value, palette) {
|
|
223
|
+
return suggestColorMatch(value, palette)?.suggestion ?? null;
|
|
220
224
|
}
|
|
221
225
|
|
|
222
226
|
function ignoredLinesOf(lines) {
|
|
@@ -305,6 +309,9 @@ export async function auditProject(options) {
|
|
|
305
309
|
policy: extra?.policy ?? null,
|
|
306
310
|
expected: extra?.expected ?? null,
|
|
307
311
|
actual: extra?.actual ?? null,
|
|
312
|
+
// fix 是「可直接落盘的替换」:绝对偏移 + 新文本;confidence 让 --fix 能区分
|
|
313
|
+
// 「可证明等价」(exact / policy)与「近似」(near),后者必须人先看一眼。
|
|
314
|
+
fix: extra?.fix ?? null,
|
|
308
315
|
});
|
|
309
316
|
};
|
|
310
317
|
|
|
@@ -342,7 +349,18 @@ export async function auditProject(options) {
|
|
|
342
349
|
const value = match[1];
|
|
343
350
|
if (!isColorValue(value) || value.includes("var(")) continue;
|
|
344
351
|
if (isCommentLine(text, starts, match.index)) continue;
|
|
345
|
-
const
|
|
352
|
+
const detail = suggestColorMatch(value, palette);
|
|
353
|
+
let fix = null;
|
|
354
|
+
const bracket = match[0].indexOf("-[");
|
|
355
|
+
if (detail && bracket > 0) {
|
|
356
|
+
fix = {
|
|
357
|
+
start: match.index,
|
|
358
|
+
end: match.index + match[0].length,
|
|
359
|
+
text: match[0].slice(0, bracket) + "-" + detail.suggestion.slice(2),
|
|
360
|
+
confidence: detail.distance < 2 ? "exact" : "near",
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
const outcome = emit(match, "no-arbitrary-color", value, detail?.suggestion ?? null, { fix });
|
|
346
364
|
if (outcome === "ignored") ignoredCount += 1;
|
|
347
365
|
}
|
|
348
366
|
|
|
@@ -360,6 +378,14 @@ export async function auditProject(options) {
|
|
|
360
378
|
policy: verdict.rule === RULE_RADIUS ? "radius" : "spacing",
|
|
361
379
|
expected: verdict.expected,
|
|
362
380
|
actual: verdict.actual,
|
|
381
|
+
fix: verdict.suggestion
|
|
382
|
+
? {
|
|
383
|
+
start: match.index,
|
|
384
|
+
end: match.index + match[0].length,
|
|
385
|
+
text: verdict.suggestion,
|
|
386
|
+
confidence: "policy",
|
|
387
|
+
}
|
|
388
|
+
: null,
|
|
363
389
|
});
|
|
364
390
|
if (outcome === "ignored") ignoredCount += 1;
|
|
365
391
|
}
|
|
@@ -374,14 +400,34 @@ export async function auditProject(options) {
|
|
|
374
400
|
for (const block of text.matchAll(R_STYLE_BLOCK)) {
|
|
375
401
|
const blockStart = block.index ?? 0;
|
|
376
402
|
const body = block[1] ?? "";
|
|
403
|
+
// property.index 是相对 body 的;body 从 `{{` 之后开始。少加这段偏移会让
|
|
404
|
+
// 行内颜色的定位(以及 fix 区间)整体前移 8 个字符。
|
|
405
|
+
const bodyOffset = (block[0] ?? "").indexOf("{{") + 2;
|
|
377
406
|
R_STYLE_COLOR.lastIndex = 0;
|
|
378
407
|
for (const property of body.matchAll(R_STYLE_COLOR)) {
|
|
379
408
|
const rawValue = property[3] ?? "";
|
|
380
409
|
if (!isColorValue(rawValue) || rawValue.includes("var(")) continue;
|
|
381
410
|
const value = extractColor(rawValue) ?? rawValue;
|
|
382
|
-
const index = blockStart + (property.index ?? 0);
|
|
411
|
+
const index = blockStart + bodyOffset + (property.index ?? 0);
|
|
383
412
|
const fakeMatch = { index, 0: property[0] };
|
|
384
|
-
const
|
|
413
|
+
const detail = suggestColorMatch(value, palette);
|
|
414
|
+
// 只修「整个值就是一个颜色」的属性。boxShadow 这类组合值换掉整段会丢语义,
|
|
415
|
+
// 宁可让人手工改。
|
|
416
|
+
let fix = null;
|
|
417
|
+
if (detail && isPureColorValue(rawValue)) {
|
|
418
|
+
const colon = property[0].indexOf(":");
|
|
419
|
+
const quoteAt = property[0].indexOf(property[2], colon + 1);
|
|
420
|
+
if (quoteAt >= 0) {
|
|
421
|
+
const start = index + quoteAt + 1;
|
|
422
|
+
fix = {
|
|
423
|
+
start,
|
|
424
|
+
end: start + rawValue.length,
|
|
425
|
+
text: "var(" + detail.suggestion + ")",
|
|
426
|
+
confidence: detail.distance < 2 ? "exact" : "near",
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
const outcome = emit(fakeMatch, "no-inline-color", value, detail?.suggestion ?? null, { fix });
|
|
385
431
|
if (outcome === "ignored") ignoredCount += 1;
|
|
386
432
|
}
|
|
387
433
|
}
|
package/fix.mjs
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 把 finding 上的 fix 区间落成文本,并打印可审阅的 patch。
|
|
3
|
+
*
|
|
4
|
+
* 所有可修命中都是「同一个 token 内的局部替换」,不会增删换行——因此 applyFixes
|
|
5
|
+
* 之后行数与原文一一对应,diff 只需要逐行比较,不必引入 LCS。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 应用修复。区间按位置排序后先筛重叠(位置靠前的优先),再从后往前替换,
|
|
10
|
+
* 这样前面区间的偏移不会被后面的长度变化挤走。
|
|
11
|
+
*/
|
|
12
|
+
export function applyFixes(text, fixes) {
|
|
13
|
+
if (!Array.isArray(fixes) || fixes.length === 0) return text;
|
|
14
|
+
|
|
15
|
+
const ordered = [...fixes]
|
|
16
|
+
.filter((fix) => Number.isInteger(fix.start) && Number.isInteger(fix.end))
|
|
17
|
+
.filter((fix) => fix.start >= 0 && fix.end >= fix.start)
|
|
18
|
+
.sort((left, right) => left.start - right.start);
|
|
19
|
+
|
|
20
|
+
const accepted = [];
|
|
21
|
+
let end = -1;
|
|
22
|
+
for (const fix of ordered) {
|
|
23
|
+
if (fix.start < end) continue; // 与前一个已接受区间重叠:跳过,不制造坏代码
|
|
24
|
+
accepted.push(fix);
|
|
25
|
+
end = fix.end;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let output = text;
|
|
29
|
+
for (let index = accepted.length - 1; index >= 0; index--) {
|
|
30
|
+
const fix = accepted[index];
|
|
31
|
+
output = output.slice(0, fix.start) + fix.text + output.slice(fix.end);
|
|
32
|
+
}
|
|
33
|
+
return output;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function splitLines(text) {
|
|
37
|
+
const lines = text.split("\n");
|
|
38
|
+
if (lines.length > 0 && lines[lines.length - 1] === "") lines.pop();
|
|
39
|
+
return lines;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const stripCr = (line) => (line.endsWith("\r") ? line.slice(0, -1) : line);
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 逐行 diff。`context` 是改动行上下文行数;相邻 hunk 自动合并。
|
|
46
|
+
* 行数不变是 applyFixes 的保证,这里仍按两边的最大长度遍历,避免越界。
|
|
47
|
+
*/
|
|
48
|
+
export function diffLines(file, before, after, context = 3) {
|
|
49
|
+
if (before === after) return "";
|
|
50
|
+
|
|
51
|
+
const oldLines = splitLines(before);
|
|
52
|
+
const newLines = splitLines(after);
|
|
53
|
+
const total = Math.max(oldLines.length, newLines.length);
|
|
54
|
+
|
|
55
|
+
const changed = [];
|
|
56
|
+
for (let index = 0; index < total; index++) {
|
|
57
|
+
if ((oldLines[index] ?? "") !== (newLines[index] ?? "")) changed.push(index);
|
|
58
|
+
}
|
|
59
|
+
if (changed.length === 0) return "";
|
|
60
|
+
|
|
61
|
+
const hunks = [];
|
|
62
|
+
for (const index of changed) {
|
|
63
|
+
const start = Math.max(0, index - context);
|
|
64
|
+
const end = Math.min(total - 1, index + context);
|
|
65
|
+
const last = hunks[hunks.length - 1];
|
|
66
|
+
if (last && start <= last.end + 1) last.end = Math.max(last.end, end);
|
|
67
|
+
else hunks.push({ start, end });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const output = ["--- a/" + file, "+++ b/" + file];
|
|
71
|
+
for (const hunk of hunks) {
|
|
72
|
+
const count = hunk.end - hunk.start + 1;
|
|
73
|
+
output.push(
|
|
74
|
+
"@@ -" + (hunk.start + 1) + "," + count + " +" + (hunk.start + 1) + "," + count + " @@"
|
|
75
|
+
);
|
|
76
|
+
for (let index = hunk.start; index <= hunk.end; index++) {
|
|
77
|
+
const oldLine = oldLines[index] ?? "";
|
|
78
|
+
const newLine = newLines[index] ?? "";
|
|
79
|
+
if (oldLine === newLine) {
|
|
80
|
+
output.push(" " + stripCr(oldLine));
|
|
81
|
+
} else {
|
|
82
|
+
output.push("-" + stripCr(oldLine));
|
|
83
|
+
output.push("+" + stripCr(newLine));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return output.join("\n");
|
|
88
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ztheme-cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Ztheme 主题 CLI:从站点拉取主题 CSS 变量,并在仓库里扫设计漂移",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"audit.mjs",
|
|
12
12
|
"color.mjs",
|
|
13
13
|
"rules.mjs",
|
|
14
|
+
"fix.mjs",
|
|
14
15
|
"README.md",
|
|
15
16
|
"LICENSE"
|
|
16
17
|
],
|
package/ztheme.mjs
CHANGED
|
@@ -19,6 +19,7 @@ import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSy
|
|
|
19
19
|
import { join, resolve } from "node:path";
|
|
20
20
|
|
|
21
21
|
import { auditProject, formatHuman, formatJson } from "./audit.mjs";
|
|
22
|
+
import { applyFixes, diffLines } from "./fix.mjs";
|
|
22
23
|
import { validateRules } from "./rules.mjs";
|
|
23
24
|
|
|
24
25
|
const BASE = (process.env.ZTHEME_BASE ?? "http://localhost:3001").replace(/\/$/, "");
|
|
@@ -308,6 +309,102 @@ const loadAuditTheme = async (root, slug) => {
|
|
|
308
309
|
return { slug, source: "none", tokens: undefined };
|
|
309
310
|
};
|
|
310
311
|
|
|
312
|
+
/**
|
|
313
|
+
* 把 findings 上的 fix 区间按文件归组,算出改前/改后与 patch。
|
|
314
|
+
* 只读不写——落盘是调用方在 --write 时做的事。
|
|
315
|
+
*/
|
|
316
|
+
const buildFixPlan = (root, result) => {
|
|
317
|
+
const byFile = new Map();
|
|
318
|
+
for (const finding of result.findings) {
|
|
319
|
+
if (!finding.fix) continue;
|
|
320
|
+
if (!byFile.has(finding.file)) byFile.set(finding.file, []);
|
|
321
|
+
byFile.get(finding.file).push(finding);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const files = [];
|
|
325
|
+
const fixes = [];
|
|
326
|
+
const patches = [];
|
|
327
|
+
for (const [file, findings] of byFile) {
|
|
328
|
+
const before = readFileSync(join(root, file), "utf8");
|
|
329
|
+
const after = applyFixes(
|
|
330
|
+
before,
|
|
331
|
+
findings.map((finding) => finding.fix)
|
|
332
|
+
);
|
|
333
|
+
const diff = diffLines(file, before, after);
|
|
334
|
+
files.push({ file, before, after, diff });
|
|
335
|
+
if (diff) patches.push(diff);
|
|
336
|
+
for (const finding of findings) {
|
|
337
|
+
fixes.push({
|
|
338
|
+
file: finding.file,
|
|
339
|
+
line: finding.line,
|
|
340
|
+
column: finding.column,
|
|
341
|
+
rule: finding.rule,
|
|
342
|
+
from: finding.evidence,
|
|
343
|
+
to: finding.fix.text,
|
|
344
|
+
confidence: finding.fix.confidence,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
fixes.sort(
|
|
350
|
+
(left, right) =>
|
|
351
|
+
left.file.localeCompare(right.file) || left.line - right.line || left.column - right.column
|
|
352
|
+
);
|
|
353
|
+
return {
|
|
354
|
+
files,
|
|
355
|
+
fixes,
|
|
356
|
+
diff: patches.join("\n"),
|
|
357
|
+
near: fixes.filter((fix) => fix.confidence === "near"),
|
|
358
|
+
};
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
const formatFixHuman = (result, plan, options) => {
|
|
362
|
+
const manual = result.findings.length - plan.fixes.length;
|
|
363
|
+
const lines = [];
|
|
364
|
+
|
|
365
|
+
if (!plan.fixes.length) {
|
|
366
|
+
lines.push("没有可自动修的问题(" + manual + " 条需要人工处理)");
|
|
367
|
+
return lines.join("\n");
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
const near = plan.near.length;
|
|
371
|
+
if (options.write) {
|
|
372
|
+
lines.push(
|
|
373
|
+
"已写入 " +
|
|
374
|
+
plan.files.filter((entry) => entry.diff).length +
|
|
375
|
+
" 个文件 · " +
|
|
376
|
+
plan.fixes.length +
|
|
377
|
+
" 处" +
|
|
378
|
+
(near ? "(其中近似 " + near + " 处)" : "")
|
|
379
|
+
);
|
|
380
|
+
lines.push("剩余 " + result.summary.warnings + " 条告警 · " + result.summary.errors + " 条错误");
|
|
381
|
+
} else {
|
|
382
|
+
lines.push(
|
|
383
|
+
"可自动修 " +
|
|
384
|
+
plan.fixes.length +
|
|
385
|
+
" 处" +
|
|
386
|
+
(near ? "(其中近似 " + near + " 处)" : "") +
|
|
387
|
+
",需人工 " +
|
|
388
|
+
manual +
|
|
389
|
+
" 处"
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (near) {
|
|
394
|
+
lines.push("");
|
|
395
|
+
lines.push("近似修复(不是同色,换的是最近的 token / 刻度)请先确认:");
|
|
396
|
+
for (const fix of plan.near.slice(0, 10)) {
|
|
397
|
+
lines.push(" " + fix.file + ":" + fix.line + " " + fix.from + " → " + fix.to);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (plan.diff) {
|
|
402
|
+
lines.push("");
|
|
403
|
+
lines.push(plan.diff);
|
|
404
|
+
}
|
|
405
|
+
return lines.join("\n");
|
|
406
|
+
};
|
|
407
|
+
|
|
311
408
|
|
|
312
409
|
const TOKENS = [
|
|
313
410
|
"background", "foreground", "card", "card-foreground", "popover", "popover-foreground",
|
|
@@ -356,6 +453,7 @@ const main = async () => {
|
|
|
356
453
|
ztheme check --lock [--file x.css]
|
|
357
454
|
与锁文件比对:本地 CSS 变差、或站点主题已更新时报警(--strict 判失败)
|
|
358
455
|
ztheme audit [path] [--json] [--theme <slug>] [--max-warnings <n>] [--quiet]
|
|
456
|
+
ztheme audit [path] --fix [--write] 打印可审阅的 patch;--write 才落盘
|
|
359
457
|
扫描 TSX/JSX/TS/JS 里的硬编码颜色与任意值;
|
|
360
458
|
--json 只输出 JSON;默认只告警,不阻断
|
|
361
459
|
环境变量:
|
|
@@ -698,8 +796,12 @@ const main = async () => {
|
|
|
698
796
|
maxWarnings = parsed;
|
|
699
797
|
}
|
|
700
798
|
|
|
799
|
+
const fixMode = flags.has("fix");
|
|
800
|
+
const writeMode = flags.has("write");
|
|
801
|
+
if (writeMode && !fixMode) failWith("--write 需要和 --fix 一起用", 2);
|
|
802
|
+
|
|
701
803
|
const theme = slug ? await loadAuditTheme(root, slug) : null;
|
|
702
|
-
const
|
|
804
|
+
const scanOptions = {
|
|
703
805
|
root,
|
|
704
806
|
include: config.include,
|
|
705
807
|
exclude: config.exclude,
|
|
@@ -707,10 +809,31 @@ const main = async () => {
|
|
|
707
809
|
ignore: config.ignore,
|
|
708
810
|
rules,
|
|
709
811
|
theme,
|
|
710
|
-
}
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
812
|
+
};
|
|
813
|
+
let result = await auditProject(scanOptions);
|
|
814
|
+
|
|
815
|
+
if (fixMode) {
|
|
816
|
+
const plan = buildFixPlan(root, result);
|
|
817
|
+
if (writeMode) {
|
|
818
|
+
for (const entry of plan.files) {
|
|
819
|
+
if (!entry.diff) continue;
|
|
820
|
+
writeFileSync(join(root, entry.file), entry.after, "utf8");
|
|
821
|
+
}
|
|
822
|
+
// 写后重扫:报告的是「现在还剩什么」,不是「刚才修了什么」。
|
|
823
|
+
result = await auditProject(scanOptions);
|
|
824
|
+
}
|
|
825
|
+
if (flags.has("json")) {
|
|
826
|
+
console.log(
|
|
827
|
+
formatJson({ ...result, fixes: plan.fixes, diff: plan.diff, written: writeMode })
|
|
828
|
+
);
|
|
829
|
+
} else {
|
|
830
|
+
console.log(formatFixHuman(result, plan, { write: writeMode }));
|
|
831
|
+
}
|
|
832
|
+
} else if (flags.has("json")) {
|
|
833
|
+
console.log(formatJson(result));
|
|
834
|
+
} else {
|
|
835
|
+
console.log(formatHuman(result, { quiet: flags.has("quiet") }));
|
|
836
|
+
}
|
|
714
837
|
|
|
715
838
|
if (result.errors.length > 0) {
|
|
716
839
|
process.exitCode = 2;
|