@algotao/uiscope 0.0.0-stage → 0.1.0
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/.agents/plugins/marketplace.json +21 -0
- package/.cursor-plugin/plugin.json +8 -0
- package/LICENSE +21 -0
- package/README.md +255 -2
- package/README.zh-CN.md +256 -0
- package/bin/uiscope.js +8 -0
- package/package.json +89 -3
- package/plugin.json +17 -0
- package/presets/README.md +11 -0
- package/presets/ocpui/index.ts +107 -0
- package/skills/README.md +7 -0
- package/skills/uiscope-ux-optimize/SKILL.md +267 -0
- package/skills/uiscope-ux-optimize/scripts/uiscope.mjs +40 -0
- package/src/__tests__/cli-parity.spec.ts +59 -0
- package/src/__tests__/cli-read.spec.ts +141 -0
- package/src/__tests__/component-tree.spec.ts +72 -0
- package/src/__tests__/enhance-core-ux.spec.ts +72 -0
- package/src/__tests__/enhance-feedback-states.spec.ts +104 -0
- package/src/__tests__/facet-analyze.spec.ts +55 -0
- package/src/__tests__/fixtures/exmarket-integration.config.ts +19 -0
- package/src/__tests__/format-scan-stats.spec.ts +32 -0
- package/src/__tests__/install-skill.spec.ts +41 -0
- package/src/__tests__/merge-cli-config.spec.ts +56 -0
- package/src/__tests__/react-tree.spec.ts +29 -0
- package/src/__tests__/read-scan-report-boundaries.spec.ts +139 -0
- package/src/__tests__/read-scan-report.spec.ts +91 -0
- package/src/__tests__/resolve-enhancements.spec.ts +21 -0
- package/src/__tests__/resolve-presets.spec.ts +51 -0
- package/src/__tests__/resolve-scan-scope.spec.ts +28 -0
- package/src/__tests__/run-scan-ux.spec.ts +51 -0
- package/src/__tests__/scan-progress.spec.ts +52 -0
- package/src/__tests__/style-path.spec.ts +11 -0
- package/src/__tests__/style-rules.spec.ts +25 -0
- package/src/__tests__/template-hints.spec.ts +19 -0
- package/src/__tests__/wrapper-roles.spec.ts +31 -0
- package/src/adapters/react/collect-react-files.ts +17 -0
- package/src/adapters/react/component-tree.tsx.ts +79 -0
- package/src/adapters/react/index.ts +3 -0
- package/src/adapters/react/parse-jsx-imports.ts +38 -0
- package/src/adapters/vue/index.ts +4 -0
- package/src/cli/brand.ts +35 -0
- package/src/cli/install-skill.ts +90 -0
- package/src/cli/main.ts +578 -0
- package/src/cli/merge-cli-config.ts +86 -0
- package/src/config/defaults.ts +65 -0
- package/src/config/frameworks.ts +20 -0
- package/src/config/load-config.ts +81 -0
- package/src/config/normalize-legacy-config.ts +39 -0
- package/src/config/preset-types.ts +16 -0
- package/src/config/resolve-preset-library-roots.ts +49 -0
- package/src/config/resolve-presets.ts +142 -0
- package/src/config/resolve-scan-scope.ts +12 -0
- package/src/config/resolve-thresholds.ts +12 -0
- package/src/enhance/dir-utils.ts +25 -0
- package/src/enhance/registry.ts +107 -0
- package/src/enhance/resolve-enhancements.ts +56 -0
- package/src/enhance/rules/action-semantics.ts +60 -0
- package/src/enhance/rules/core-ux.ts +139 -0
- package/src/enhance/rules/facet-hygiene.ts +44 -0
- package/src/enhance/rules/feedback-states.ts +39 -0
- package/src/enhance/rules/form-a11y.ts +62 -0
- package/src/enhance/rules/hollow-shell.ts +39 -0
- package/src/enhance/rules/i18n-hints.ts +29 -0
- package/src/enhance/rules/list-templating.ts +38 -0
- package/src/enhance/rules/page-rollup.ts +82 -0
- package/src/enhance/tree-walk.ts +29 -0
- package/src/enhance/types.ts +48 -0
- package/src/enhance/ux-rule-pack.ts +40 -0
- package/src/enhance/workbench-defaults.ts +28 -0
- package/src/index.ts +80 -0
- package/src/manifest/consumer-wrapper-roles.ts +34 -0
- package/src/manifest/library-components.ts +46 -0
- package/src/manifest/library-style-classes.ts +31 -0
- package/src/manifest/library-style-whitelist.ts +82 -0
- package/src/manifest/library-ux-taxonomy.ts +103 -0
- package/src/paths/monorepo-root.ts +13 -0
- package/src/report/facet-output-limits.ts +56 -0
- package/src/report/format-scan-stats.ts +18 -0
- package/src/report/format-ux-scan-report.ts +59 -0
- package/src/report/markdown-report.ts +108 -0
- package/src/report/print-facets.ts +129 -0
- package/src/report/print-styles.ts +20 -0
- package/src/report/print-tree.ts +70 -0
- package/src/report/print-ux-findings.ts +53 -0
- package/src/report/read-scan-report.ts +201 -0
- package/src/report/run-report-scenarios.ts +139 -0
- package/src/run-scan.ts +341 -0
- package/src/scan/collect-source-files.ts +36 -0
- package/src/scan/component-tree.ts +155 -0
- package/src/scan/extract-node-attrs.ts +49 -0
- package/src/scan/facet-analyze.ts +318 -0
- package/src/scan/file-uses-library.ts +37 -0
- package/src/scan/library-meta.ts +71 -0
- package/src/scan/parse-imports.ts +88 -0
- package/src/scan/scan-progress.ts +89 -0
- package/src/scan/style-path.ts +26 -0
- package/src/scan/style-rules.ts +127 -0
- package/src/scan/style-trace.ts +181 -0
- package/src/scan/template-hints.ts +70 -0
- package/src/types.ts +274 -0
package/src/cli/main.ts
ADDED
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { DEFAULT_CONFIG, loadScanConfig, mergeConfig } from '../config/load-config.js';
|
|
4
|
+
import { mergeCliIntoConfig } from './merge-cli-config.js';
|
|
5
|
+
import { applyPresets } from '../config/resolve-presets.js';
|
|
6
|
+
import { resolveOnlyLibraryConsumers } from '../config/resolve-scan-scope.js';
|
|
7
|
+
import type { UiscopeConfig, UiscopeFramework } from '../types.js';
|
|
8
|
+
import { resolveThresholds } from '../config/resolve-thresholds.js';
|
|
9
|
+
import { OPTIONAL_ENHANCEMENT_IDS } from '../enhance/registry.js';
|
|
10
|
+
import { parseEnhanceCliArgs } from '../enhance/resolve-enhancements.js';
|
|
11
|
+
import { renderMarkdownReport } from '../report/markdown-report.js';
|
|
12
|
+
import { formatAllTrees } from '../report/print-tree.js';
|
|
13
|
+
import { formatStyleHits } from '../report/print-styles.js';
|
|
14
|
+
import { formatScanStatsBanner } from '../report/format-scan-stats.js';
|
|
15
|
+
import { formatFacetReportMarkdown, formatFacetSummary } from '../report/print-facets.js';
|
|
16
|
+
import { formatUxFindings } from '../report/print-ux-findings.js';
|
|
17
|
+
import { formatUxScanReport } from '../report/format-ux-scan-report.js';
|
|
18
|
+
import {
|
|
19
|
+
loadMaintenanceScenarioDefs,
|
|
20
|
+
runScenario,
|
|
21
|
+
scanPackageDir,
|
|
22
|
+
} from '../report/run-report-scenarios.js';
|
|
23
|
+
import {
|
|
24
|
+
getScanReportCountsFromFile,
|
|
25
|
+
readScanReportSectionFromFile,
|
|
26
|
+
readTextReportLinesSync,
|
|
27
|
+
type ScanReportArraySection,
|
|
28
|
+
} from '../report/read-scan-report.js';
|
|
29
|
+
import { createTerminalProgressReporter } from '../scan/scan-progress.js';
|
|
30
|
+
import { runScan, type ScanMode } from '../run-scan.js';
|
|
31
|
+
import {
|
|
32
|
+
cliBrandName,
|
|
33
|
+
cliTag,
|
|
34
|
+
defaultConfigHintBasename,
|
|
35
|
+
defaultMaintenanceReportBasename,
|
|
36
|
+
} from './brand.js';
|
|
37
|
+
import { parseInstallArgs, printInstallHints, runInstallSkill } from './install-skill.js';
|
|
38
|
+
|
|
39
|
+
function printHelp() {
|
|
40
|
+
const cmd = cliBrandName();
|
|
41
|
+
console.log(`用法:
|
|
42
|
+
${cmd} [install|tree|styles|facets|ux|all|report|read] [选项]
|
|
43
|
+
|
|
44
|
+
子命令:
|
|
45
|
+
install 安装 Agent skill(uiscope-ux-optimize)到 .cursor/skills;--global 装到用户目录
|
|
46
|
+
tree 组件嵌套树(roots 下 Vue;配置了 libraryImportSources 时默认收窄消费方)
|
|
47
|
+
styles 样式重载 + 溯源链
|
|
48
|
+
facets UI/UX 切面 + 核心 UX 契约(core-ux)
|
|
49
|
+
ux 切面 + UX 规则(默认跳过样式,单次解析)
|
|
50
|
+
all 树 + 样式 + 切面 + 核心 UX 契约(默认)
|
|
51
|
+
report 生成 Markdown 维护报告(多场景对比 + 白名单)
|
|
52
|
+
read 读取已落盘报告:条数统计或按区段/行范围切片(见 --file)
|
|
53
|
+
|
|
54
|
+
选项:
|
|
55
|
+
--root <dir> 扫描根目录(可重复)
|
|
56
|
+
--fail-on-warn 有告警时退出码 1
|
|
57
|
+
--all-files 扫描 roots 下全部视图文件(覆盖消费方收窄)
|
|
58
|
+
--include-non-ocp 兼容 @ocpui/scan:等同 --all-files
|
|
59
|
+
--library-consumers-only 仅扫描消费设计库的文件(需 libraryImportSources)
|
|
60
|
+
--framework <vue|react> 参与扫描的框架(可重复;默认 vue+react)
|
|
61
|
+
--preset <id|@pkg> 组件库扩展(可重复;短 id → presets/<id>/ 或外置 @algotao/preset-<id>)
|
|
62
|
+
--json JSON 输出
|
|
63
|
+
--threshold <n> 样式 overrideRuleCount(等同 thresholds.style)
|
|
64
|
+
--config <file> 显式加载配置文件
|
|
65
|
+
--no-config 不自动发现 uiscope/ocp-scan 配置(纯 CLI;仍可与 --config 合用)
|
|
66
|
+
--library-root <dir> 设计库源码根,相对 cwd(可重复)
|
|
67
|
+
--library-import <p> 判定消费方的 import 前缀(可重复;通常由 --preset 提供)
|
|
68
|
+
--consumer-wrapper <tag> 站点封装组件名(可重复)
|
|
69
|
+
--ignore <glob> 追加 fast-glob 忽略(可重复)
|
|
70
|
+
--nest-depth <n> 库组件嵌套深度告警线(thresholds.tree)
|
|
71
|
+
--template-warn-rate <n> 模板化率告警线
|
|
72
|
+
--multi-facet-warn <n> 单文件切面数告警线
|
|
73
|
+
--no-warn-orphan-styles 关闭无模板锚点的样式重载告警
|
|
74
|
+
--style-exclude <g> 排除样式 glob(可重复)
|
|
75
|
+
--out <file> 报告:report 默认维护名;ux/facets 写 Markdown + 同名 .json
|
|
76
|
+
--facet-limit <n> 切面列表文件数;0=全量(默认终端 12、report 内 15)
|
|
77
|
+
--enhance <id> 可选增强扫描(可重复或逗号分隔;id 见下)
|
|
78
|
+
--enhance-all 启用全部可选增强
|
|
79
|
+
--with-styles 与 ux 联用:额外跑样式(否则跳过 CSS IO)
|
|
80
|
+
--quiet, -q 扫描时不输出进度(默认 TTY 下 stderr 显示阶段与计数)
|
|
81
|
+
--file <path> read:报告路径(.json ScanResult 或 .md)
|
|
82
|
+
--section <name> read:JSON 区段 trees|facetReports|uxFindings|...
|
|
83
|
+
--offset <n> read:区段起始下标(默认 0)
|
|
84
|
+
--limit <n> read:区段最多条数
|
|
85
|
+
--lines <start-end> read:Markdown 行范围(1-based,如 1-80)
|
|
86
|
+
-h, --help 帮助
|
|
87
|
+
|
|
88
|
+
可选增强 (--enhance):
|
|
89
|
+
${OPTIONAL_ENHANCEMENT_IDS.join(', ')}`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function parseArgs(argv: string[]) {
|
|
93
|
+
let mode: ScanMode | 'report' | 'read' | 'install' = 'all';
|
|
94
|
+
let installArgv: string[] = [];
|
|
95
|
+
const roots: string[] = [];
|
|
96
|
+
let failOnWarn = false;
|
|
97
|
+
let onlyLibraryConsumers: boolean | undefined;
|
|
98
|
+
let json = false;
|
|
99
|
+
let threshold: number | undefined;
|
|
100
|
+
const styleExcludes: string[] = [];
|
|
101
|
+
const enhanceTokens: string[] = [];
|
|
102
|
+
let enhanceAll = false;
|
|
103
|
+
let withStyles = false;
|
|
104
|
+
let outFile: string | undefined;
|
|
105
|
+
let facetLimit: number | undefined;
|
|
106
|
+
let quiet = false;
|
|
107
|
+
let readFile: string | undefined;
|
|
108
|
+
let readSection: ScanReportArraySection | undefined;
|
|
109
|
+
let readOffset: number | undefined;
|
|
110
|
+
let readLimit: number | undefined;
|
|
111
|
+
let readLines: string | undefined;
|
|
112
|
+
let configFile: string | undefined;
|
|
113
|
+
let noConfig = false;
|
|
114
|
+
const libraryRoots: string[] = [];
|
|
115
|
+
const libraryImports: string[] = [];
|
|
116
|
+
const consumerWrappers: string[] = [];
|
|
117
|
+
const ignoreGlobs: string[] = [];
|
|
118
|
+
let nestDepth: number | undefined;
|
|
119
|
+
let templateWarnRate: number | undefined;
|
|
120
|
+
let multiFacetWarn: number | undefined;
|
|
121
|
+
let noWarnOrphanStyles = false;
|
|
122
|
+
const frameworks: UiscopeFramework[] = [];
|
|
123
|
+
const presets: string[] = [];
|
|
124
|
+
|
|
125
|
+
for (let i = 0; i < argv.length; i++) {
|
|
126
|
+
const arg = argv[i];
|
|
127
|
+
if (arg === 'install') {
|
|
128
|
+
mode = 'install';
|
|
129
|
+
installArgv = argv.slice(i + 1);
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
if (
|
|
133
|
+
arg === 'tree'
|
|
134
|
+
|| arg === 'styles'
|
|
135
|
+
|| arg === 'facets'
|
|
136
|
+
|| arg === 'ux'
|
|
137
|
+
|| arg === 'all'
|
|
138
|
+
|| arg === 'report'
|
|
139
|
+
|| arg === 'read'
|
|
140
|
+
) {
|
|
141
|
+
mode = arg;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (arg === '--quiet' || arg === '-q') {
|
|
145
|
+
quiet = true;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (arg === '--file') {
|
|
149
|
+
const next = argv[++i];
|
|
150
|
+
if (next) readFile = next;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (arg === '--section') {
|
|
154
|
+
const next = argv[++i];
|
|
155
|
+
if (next) readSection = next as ScanReportArraySection;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (arg === '--offset') {
|
|
159
|
+
const next = argv[++i];
|
|
160
|
+
if (next != null && next !== '') readOffset = Number(next);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (arg === '--limit') {
|
|
164
|
+
const next = argv[++i];
|
|
165
|
+
if (next != null && next !== '') readLimit = Number(next);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (arg === '--lines') {
|
|
169
|
+
const next = argv[++i];
|
|
170
|
+
if (next) readLines = next;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (arg === '--fail-on-warn') {
|
|
174
|
+
failOnWarn = true;
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (arg === '--all-files' || arg === '--include-non-ocp') {
|
|
178
|
+
onlyLibraryConsumers = false;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (arg === '--framework') {
|
|
182
|
+
const next = argv[++i] as UiscopeFramework | undefined;
|
|
183
|
+
if (next === 'vue' || next === 'react') frameworks.push(next);
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (arg === '--preset') {
|
|
187
|
+
const next = argv[++i];
|
|
188
|
+
if (next) presets.push(next);
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (arg === '--library-consumers-only') {
|
|
192
|
+
onlyLibraryConsumers = true;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (arg === '--json') {
|
|
196
|
+
json = true;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (arg === '--enhance-all') {
|
|
200
|
+
enhanceAll = true;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (arg === '--with-styles') {
|
|
204
|
+
withStyles = true;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (arg === '--root') {
|
|
208
|
+
const next = argv[++i];
|
|
209
|
+
if (next) roots.push(next);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (arg === '--threshold') {
|
|
213
|
+
const next = argv[++i];
|
|
214
|
+
if (next) threshold = Number(next);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (arg === '--style-exclude') {
|
|
218
|
+
const next = argv[++i];
|
|
219
|
+
if (next) styleExcludes.push(next);
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (arg === '--config') {
|
|
223
|
+
const next = argv[++i];
|
|
224
|
+
if (next) configFile = next;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (arg === '--no-config') {
|
|
228
|
+
noConfig = true;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (arg === '--library-root') {
|
|
232
|
+
const next = argv[++i];
|
|
233
|
+
if (next) libraryRoots.push(next);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
if (arg === '--library-import') {
|
|
237
|
+
const next = argv[++i];
|
|
238
|
+
if (next) libraryImports.push(next);
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (arg === '--consumer-wrapper') {
|
|
242
|
+
const next = argv[++i];
|
|
243
|
+
if (next) consumerWrappers.push(next);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (arg === '--ignore') {
|
|
247
|
+
const next = argv[++i];
|
|
248
|
+
if (next) ignoreGlobs.push(next);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (arg === '--nest-depth') {
|
|
252
|
+
const next = argv[++i];
|
|
253
|
+
if (next != null && next !== '') nestDepth = Number(next);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (arg === '--template-warn-rate') {
|
|
257
|
+
const next = argv[++i];
|
|
258
|
+
if (next != null && next !== '') templateWarnRate = Number(next);
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (arg === '--multi-facet-warn') {
|
|
262
|
+
const next = argv[++i];
|
|
263
|
+
if (next != null && next !== '') multiFacetWarn = Number(next);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if (arg === '--no-warn-orphan-styles') {
|
|
267
|
+
noWarnOrphanStyles = true;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (arg === '--enhance') {
|
|
271
|
+
const next = argv[++i];
|
|
272
|
+
if (next) enhanceTokens.push(next);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (arg === '--out') {
|
|
276
|
+
const next = argv[++i];
|
|
277
|
+
if (next) outFile = next;
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
if (arg === '--facet-limit') {
|
|
281
|
+
const next = argv[++i];
|
|
282
|
+
if (next != null && next !== '') facetLimit = Number(next);
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
if (arg === '--help' || arg === '-h') {
|
|
286
|
+
printHelp();
|
|
287
|
+
process.exit(0);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const parsedEnhance = parseEnhanceCliArgs(enhanceTokens);
|
|
292
|
+
return {
|
|
293
|
+
mode,
|
|
294
|
+
installOpts: mode === 'install' ? parseInstallArgs(installArgv) : undefined,
|
|
295
|
+
roots,
|
|
296
|
+
failOnWarn,
|
|
297
|
+
onlyLibraryConsumers,
|
|
298
|
+
json,
|
|
299
|
+
threshold,
|
|
300
|
+
styleExcludes,
|
|
301
|
+
outFile,
|
|
302
|
+
facetLimit,
|
|
303
|
+
enhance: parsedEnhance.ids,
|
|
304
|
+
enhanceAll: enhanceAll || parsedEnhance.all,
|
|
305
|
+
enhanceInvalid: parsedEnhance.invalid,
|
|
306
|
+
withStyles,
|
|
307
|
+
quiet,
|
|
308
|
+
readFile,
|
|
309
|
+
readSection,
|
|
310
|
+
readOffset,
|
|
311
|
+
readLimit,
|
|
312
|
+
readLines,
|
|
313
|
+
configFile,
|
|
314
|
+
noConfig,
|
|
315
|
+
libraryRoots,
|
|
316
|
+
libraryImports,
|
|
317
|
+
consumerWrappers,
|
|
318
|
+
ignoreGlobs,
|
|
319
|
+
nestDepth,
|
|
320
|
+
templateWarnRate,
|
|
321
|
+
multiFacetWarn,
|
|
322
|
+
noWarnOrphanStyles,
|
|
323
|
+
frameworks,
|
|
324
|
+
presets,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async function resolveBaseScanConfig(
|
|
329
|
+
cwd: string,
|
|
330
|
+
configFile: string | undefined,
|
|
331
|
+
noConfig: boolean,
|
|
332
|
+
): Promise<UiscopeConfig> {
|
|
333
|
+
if (noConfig) {
|
|
334
|
+
if (configFile) return loadScanConfig(cwd, configFile);
|
|
335
|
+
return { ...DEFAULT_CONFIG };
|
|
336
|
+
}
|
|
337
|
+
return loadScanConfig(cwd, configFile);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
async function main() {
|
|
341
|
+
const args = parseArgs(process.argv.slice(2));
|
|
342
|
+
const cwd = process.cwd();
|
|
343
|
+
|
|
344
|
+
if (args.mode === 'install') {
|
|
345
|
+
const helpWanted = process.argv.slice(2).some((a) => a === '-h' || a === '--help');
|
|
346
|
+
if (helpWanted) {
|
|
347
|
+
printInstallHints();
|
|
348
|
+
process.exit(0);
|
|
349
|
+
}
|
|
350
|
+
process.exit(runInstallSkill(cwd, args.installOpts ?? {}));
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (args.enhanceInvalid.length) {
|
|
354
|
+
console.error(`${cliTag()} 未知增强项: ${args.enhanceInvalid.join(', ')}`);
|
|
355
|
+
console.error(`可选: ${OPTIONAL_ENHANCEMENT_IDS.join(', ')} 或 --enhance-all`);
|
|
356
|
+
process.exit(1);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (args.mode === 'read') {
|
|
360
|
+
if (!args.readFile) {
|
|
361
|
+
console.error(`${cliTag()} read 需要 --file <path>`);
|
|
362
|
+
process.exit(1);
|
|
363
|
+
}
|
|
364
|
+
const file = path.resolve(cwd, args.readFile);
|
|
365
|
+
if (!fs.existsSync(file)) {
|
|
366
|
+
console.error(`${cliTag()} 文件不存在: ${file}`);
|
|
367
|
+
process.exit(1);
|
|
368
|
+
}
|
|
369
|
+
if (args.readLines) {
|
|
370
|
+
const m = args.readLines.match(/^(\d+)-(\d+)$/);
|
|
371
|
+
if (!m) {
|
|
372
|
+
console.error(`${cliTag()} --lines 格式应为 start-end,如 1-120`);
|
|
373
|
+
process.exit(1);
|
|
374
|
+
}
|
|
375
|
+
const slice = readTextReportLinesSync(file, {
|
|
376
|
+
startLine: Number(m[1]),
|
|
377
|
+
endLine: Number(m[2]),
|
|
378
|
+
});
|
|
379
|
+
if (args.json) {
|
|
380
|
+
console.log(JSON.stringify(slice, null, 2));
|
|
381
|
+
} else {
|
|
382
|
+
console.log(
|
|
383
|
+
`# lines ${slice.startLine}-${slice.endLine} / ${slice.totalLines}\n\n${slice.text}`,
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (args.readSection) {
|
|
389
|
+
if (!/\.json$/i.test(file)) {
|
|
390
|
+
console.error(`${cliTag()} --section 仅用于 ScanResult .json 侧车`);
|
|
391
|
+
process.exit(1);
|
|
392
|
+
}
|
|
393
|
+
const slice = readScanReportSectionFromFile(file, args.readSection, {
|
|
394
|
+
offset: args.readOffset,
|
|
395
|
+
limit: args.readLimit,
|
|
396
|
+
});
|
|
397
|
+
console.log(JSON.stringify(slice, null, 2));
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
if (/\.md$/i.test(file)) {
|
|
401
|
+
console.error(`${cliTag()} Markdown 请用 --lines <start-end>,或改用同名 .json 统计条数`);
|
|
402
|
+
process.exit(1);
|
|
403
|
+
}
|
|
404
|
+
const counts = getScanReportCountsFromFile(file);
|
|
405
|
+
console.log(JSON.stringify(counts, null, 2));
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if (args.mode === 'report') {
|
|
410
|
+
const scanPkgDir = scanPackageDir();
|
|
411
|
+
const defs = await loadMaintenanceScenarioDefs(scanPkgDir);
|
|
412
|
+
const scenarios = [];
|
|
413
|
+
for (const def of defs) {
|
|
414
|
+
let base = await loadScanConfig(def.cwd);
|
|
415
|
+
if (args.presets.length) {
|
|
416
|
+
base = mergeConfig(base, {
|
|
417
|
+
presets: [...new Set([...(base.presets ?? []), ...args.presets])],
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
scenarios.push(await runScenario(base, def));
|
|
421
|
+
}
|
|
422
|
+
const md = renderMarkdownReport({
|
|
423
|
+
generatedAt: new Date().toISOString(),
|
|
424
|
+
cwd,
|
|
425
|
+
scenarios,
|
|
426
|
+
facetFileLimit: args.facetLimit,
|
|
427
|
+
});
|
|
428
|
+
const out = path.resolve(cwd, args.outFile ?? defaultMaintenanceReportBasename());
|
|
429
|
+
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
430
|
+
fs.writeFileSync(out, md, 'utf8');
|
|
431
|
+
console.log(`${cliTag()} 报告已写入 → ${out}`);
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
let base = await resolveBaseScanConfig(cwd, args.configFile, args.noConfig);
|
|
436
|
+
if (args.presets.length) {
|
|
437
|
+
base = mergeConfig(base, { presets: [...new Set([...(base.presets ?? []), ...args.presets])] });
|
|
438
|
+
}
|
|
439
|
+
if (args.frameworks.length) {
|
|
440
|
+
base = mergeConfig(base, { frameworks: args.frameworks });
|
|
441
|
+
}
|
|
442
|
+
base = await applyPresets(cwd, base);
|
|
443
|
+
|
|
444
|
+
const presetIds = base.presets ?? [];
|
|
445
|
+
const config = mergeCliIntoConfig(cwd, base, presetIds, {
|
|
446
|
+
roots: args.roots,
|
|
447
|
+
libraryRoots: args.libraryRoots,
|
|
448
|
+
libraryImports: args.libraryImports,
|
|
449
|
+
consumerWrappers: args.consumerWrappers,
|
|
450
|
+
ignoreGlobs: args.ignoreGlobs,
|
|
451
|
+
onlyLibraryConsumers: args.onlyLibraryConsumers,
|
|
452
|
+
threshold: args.threshold,
|
|
453
|
+
styleExcludes: args.styleExcludes,
|
|
454
|
+
nestDepth: args.nestDepth,
|
|
455
|
+
templateWarnRate: args.templateWarnRate,
|
|
456
|
+
multiFacetWarn: args.multiFacetWarn,
|
|
457
|
+
noWarnOrphanStyles: args.noWarnOrphanStyles,
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
const thresholds = resolveThresholds(config);
|
|
461
|
+
const scanMode = args.mode as ScanMode;
|
|
462
|
+
|
|
463
|
+
const onProgress =
|
|
464
|
+
args.quiet || args.json ? undefined : createTerminalProgressReporter();
|
|
465
|
+
|
|
466
|
+
const result = await runScan({
|
|
467
|
+
cwd,
|
|
468
|
+
config,
|
|
469
|
+
mode: scanMode,
|
|
470
|
+
onlyLibraryTrees: resolveOnlyLibraryConsumers(config),
|
|
471
|
+
enhance: args.enhance.length ? args.enhance : undefined,
|
|
472
|
+
enhanceAll: args.enhanceAll,
|
|
473
|
+
withStyles: args.withStyles,
|
|
474
|
+
onProgress,
|
|
475
|
+
frameworks: args.frameworks.length ? args.frameworks : undefined,
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
const enhancementsRequested = args.enhanceAll
|
|
479
|
+
? 'all'
|
|
480
|
+
: args.enhance.length
|
|
481
|
+
? args.enhance.join(',')
|
|
482
|
+
: 'core-ux(仅内置)';
|
|
483
|
+
|
|
484
|
+
const wroteOut = Boolean(args.outFile && (scanMode === 'ux' || scanMode === 'facets'));
|
|
485
|
+
|
|
486
|
+
if (args.outFile && scanMode === 'ux') {
|
|
487
|
+
const out = path.resolve(cwd, args.outFile);
|
|
488
|
+
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
489
|
+
const md = formatUxScanReport(result, {
|
|
490
|
+
generatedAt: new Date().toISOString(),
|
|
491
|
+
cwd,
|
|
492
|
+
roots: config.roots,
|
|
493
|
+
enhancementsRequested,
|
|
494
|
+
});
|
|
495
|
+
fs.writeFileSync(out, md, 'utf8');
|
|
496
|
+
console.log(`${cliTag()} UX 报告 → ${out}`);
|
|
497
|
+
const jsonSidecar = out.replace(/\.md$/i, '.json');
|
|
498
|
+
fs.writeFileSync(jsonSidecar, JSON.stringify(result, null, 2), 'utf8');
|
|
499
|
+
console.log(`${cliTag()} UX JSON → ${jsonSidecar}`);
|
|
500
|
+
console.log(`\n${formatScanStatsBanner(result)}`);
|
|
501
|
+
console.log('\n' + formatFacetSummary(result.facetReports, args.facetLimit ?? 20));
|
|
502
|
+
console.log('\n' + formatUxFindings(result.uxFindings, 30));
|
|
503
|
+
console.log(`\n${cliTag()} 已执行增强: ${result.enhancementsRun.join(', ')}`);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if (args.outFile && scanMode === 'facets') {
|
|
507
|
+
const out = path.resolve(cwd, args.outFile);
|
|
508
|
+
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
509
|
+
const facetCap = args.facetLimit ?? 0;
|
|
510
|
+
const md = [
|
|
511
|
+
'# UIScope 切面报告',
|
|
512
|
+
'',
|
|
513
|
+
`- 生成时间:${new Date().toISOString()}`,
|
|
514
|
+
`- 工作目录:\`${cwd}\``,
|
|
515
|
+
`- 扫描根:${config.roots.map((r) => `\`${r}\``).join(', ')}`,
|
|
516
|
+
'',
|
|
517
|
+
formatFacetReportMarkdown('切面明细', result.facetReports, result.stats, {
|
|
518
|
+
maxFiles: facetCap,
|
|
519
|
+
}),
|
|
520
|
+
].join('\n\n');
|
|
521
|
+
fs.writeFileSync(out, md, 'utf8');
|
|
522
|
+
console.log(`${cliTag()} 切面报告 → ${out}`);
|
|
523
|
+
const jsonSidecar = out.replace(/\.md$/i, '.json');
|
|
524
|
+
fs.writeFileSync(jsonSidecar, JSON.stringify(result, null, 2), 'utf8');
|
|
525
|
+
console.log(`${cliTag()} 切面 JSON → ${jsonSidecar}`);
|
|
526
|
+
console.log(`\n${formatScanStatsBanner(result)}`);
|
|
527
|
+
console.log('\n' + formatFacetSummary(result.facetReports, args.facetLimit ?? 20));
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (args.json) {
|
|
531
|
+
console.log(JSON.stringify(result, null, 2));
|
|
532
|
+
} else if (!wroteOut) {
|
|
533
|
+
if (args.mode === 'tree') {
|
|
534
|
+
console.log(formatAllTrees(result.trees, false));
|
|
535
|
+
} else if (args.mode === 'ux') {
|
|
536
|
+
console.log(formatScanStatsBanner(result));
|
|
537
|
+
console.log(formatFacetSummary(result.facetReports, args.facetLimit ?? 20));
|
|
538
|
+
console.log('\n' + formatUxFindings(result.uxFindings, 40));
|
|
539
|
+
if (result.enhancementsRun.length) {
|
|
540
|
+
console.log(`\n${cliTag()} 已执行增强: ${result.enhancementsRun.join(', ')}`);
|
|
541
|
+
}
|
|
542
|
+
} else {
|
|
543
|
+
if (args.mode !== 'styles' && args.mode !== 'facets') {
|
|
544
|
+
console.log(formatAllTrees(result.trees, false));
|
|
545
|
+
}
|
|
546
|
+
if (args.mode === 'facets' || args.mode === 'all') {
|
|
547
|
+
console.log(`\n${formatScanStatsBanner(result)}`);
|
|
548
|
+
console.log('\n' + formatFacetSummary(result.facetReports, args.facetLimit));
|
|
549
|
+
if (result.uxFindings.length) {
|
|
550
|
+
console.log('\n' + formatUxFindings(result.uxFindings));
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
if (args.mode === 'styles' || args.mode === 'all') {
|
|
554
|
+
console.log('\n' + formatStyleHits(result.styleOverrides, thresholds.style.overrideRuleCount));
|
|
555
|
+
const traced = result.styleChains.filter((c) => c.overrideCount > 0).slice(0, 5);
|
|
556
|
+
if (traced.length) {
|
|
557
|
+
console.log(`\n${cliTag()} 样式溯源链(前 5):`);
|
|
558
|
+
for (const chain of traced) {
|
|
559
|
+
console.log(`\n.${chain.className}:`);
|
|
560
|
+
for (const line of chain.chain) console.log(line);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (result.warnings.length) {
|
|
566
|
+
console.log('\n' + result.warnings.join('\n'));
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
if (args.failOnWarn && result.warnings.length) {
|
|
571
|
+
process.exit(1);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
main().catch((error: unknown) => {
|
|
576
|
+
console.error(error instanceof Error ? error.message : error);
|
|
577
|
+
process.exit(1);
|
|
578
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { UiscopeConfig } from '../types.js';
|
|
2
|
+
import { mergeConfig } from '../config/load-config.js';
|
|
3
|
+
import { resolveAutoLibraryRoots } from '../config/resolve-preset-library-roots.js';
|
|
4
|
+
|
|
5
|
+
export interface CliScanOptions {
|
|
6
|
+
roots: string[];
|
|
7
|
+
libraryRoots: string[];
|
|
8
|
+
libraryImports: string[];
|
|
9
|
+
consumerWrappers: string[];
|
|
10
|
+
ignoreGlobs: string[];
|
|
11
|
+
onlyLibraryConsumers?: boolean;
|
|
12
|
+
threshold?: number;
|
|
13
|
+
styleExcludes: string[];
|
|
14
|
+
nestDepth?: number;
|
|
15
|
+
templateWarnRate?: number;
|
|
16
|
+
multiFacetWarn?: number;
|
|
17
|
+
noWarnOrphanStyles?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function mergeCliIntoConfig(
|
|
21
|
+
cwd: string,
|
|
22
|
+
base: UiscopeConfig,
|
|
23
|
+
presetIds: readonly string[],
|
|
24
|
+
cli: CliScanOptions,
|
|
25
|
+
): UiscopeConfig {
|
|
26
|
+
let libraryRoots = [...(base.libraryRoots ?? [])];
|
|
27
|
+
if (libraryRoots.length === 0 && cli.libraryRoots.length === 0) {
|
|
28
|
+
libraryRoots = resolveAutoLibraryRoots(cwd, presetIds);
|
|
29
|
+
}
|
|
30
|
+
libraryRoots = [...new Set([...libraryRoots, ...cli.libraryRoots])];
|
|
31
|
+
|
|
32
|
+
const libraryImportSources = cli.libraryImports.length
|
|
33
|
+
? [...new Set([...(base.libraryImportSources ?? []), ...cli.libraryImports])]
|
|
34
|
+
: base.libraryImportSources;
|
|
35
|
+
|
|
36
|
+
const consumerWrapperTags = cli.consumerWrappers.length
|
|
37
|
+
? [...new Set([...(base.consumerWrapperTags ?? []), ...cli.consumerWrappers])]
|
|
38
|
+
: base.consumerWrapperTags;
|
|
39
|
+
|
|
40
|
+
const ignore = cli.ignoreGlobs.length
|
|
41
|
+
? [...new Set([...(base.ignore ?? []), ...cli.ignoreGlobs])]
|
|
42
|
+
: base.ignore;
|
|
43
|
+
|
|
44
|
+
const hasThresholdCli =
|
|
45
|
+
cli.styleExcludes.length > 0
|
|
46
|
+
|| cli.nestDepth != null
|
|
47
|
+
|| cli.templateWarnRate != null
|
|
48
|
+
|| cli.multiFacetWarn != null
|
|
49
|
+
|| cli.noWarnOrphanStyles;
|
|
50
|
+
|
|
51
|
+
const thresholdPatch = hasThresholdCli
|
|
52
|
+
? {
|
|
53
|
+
thresholds: {
|
|
54
|
+
style: {
|
|
55
|
+
...(cli.styleExcludes.length > 0
|
|
56
|
+
? {
|
|
57
|
+
excludeGlobs: [
|
|
58
|
+
...(base.thresholds?.style?.excludeGlobs ?? []),
|
|
59
|
+
...cli.styleExcludes,
|
|
60
|
+
],
|
|
61
|
+
}
|
|
62
|
+
: {}),
|
|
63
|
+
...(cli.noWarnOrphanStyles ? { warnOrphanOverrides: false } : {}),
|
|
64
|
+
},
|
|
65
|
+
tree: cli.nestDepth != null ? { maxLibraryNestDepth: cli.nestDepth } : undefined,
|
|
66
|
+
facet: {
|
|
67
|
+
...(cli.templateWarnRate != null ? { templateWarnRate: cli.templateWarnRate } : {}),
|
|
68
|
+
...(cli.multiFacetWarn != null ? { multiFacetWarnCount: cli.multiFacetWarn } : {}),
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
: {};
|
|
73
|
+
|
|
74
|
+
return mergeConfig(base, {
|
|
75
|
+
roots: cli.roots.length ? cli.roots : base.roots,
|
|
76
|
+
libraryRoots,
|
|
77
|
+
libraryImportSources,
|
|
78
|
+
consumerWrapperTags,
|
|
79
|
+
ignore,
|
|
80
|
+
overrideThreshold: cli.threshold,
|
|
81
|
+
...(cli.onlyLibraryConsumers !== undefined
|
|
82
|
+
? { onlyLibraryConsumers: cli.onlyLibraryConsumers }
|
|
83
|
+
: {}),
|
|
84
|
+
...thresholdPatch,
|
|
85
|
+
});
|
|
86
|
+
}
|