@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
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { FileFacetReport, ScanResult } from '../types.js';
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_FACET_MARKDOWN_FILE_LIMIT,
|
|
4
|
+
DEFAULT_FACET_SUMMARY_FILE_LIMIT,
|
|
5
|
+
formatFacetTruncationGuide,
|
|
6
|
+
resolveFacetFileLimit,
|
|
7
|
+
} from './facet-output-limits.js';
|
|
8
|
+
|
|
9
|
+
function relPath(file: string): string {
|
|
10
|
+
return file.replace(/\\/g, '/');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function formatFacetMarkdownBlocks(
|
|
14
|
+
facetReports: FileFacetReport[],
|
|
15
|
+
options?: { maxFiles?: number; fileRel?: (file: string) => string },
|
|
16
|
+
): string[] {
|
|
17
|
+
const fileRel = options?.fileRel ?? relPath;
|
|
18
|
+
const lines: string[] = [];
|
|
19
|
+
const withFacets = facetReports
|
|
20
|
+
.filter((f) => f.facetCount > 0)
|
|
21
|
+
.sort((a, b) => b.facetCount - a.facetCount);
|
|
22
|
+
const totalWithFacets = withFacets.length;
|
|
23
|
+
const cap = resolveFacetFileLimit(options?.maxFiles, DEFAULT_FACET_MARKDOWN_FILE_LIMIT);
|
|
24
|
+
const facetFiles = cap == null ? withFacets : withFacets.slice(0, cap);
|
|
25
|
+
|
|
26
|
+
if (!facetFiles.length) {
|
|
27
|
+
lines.push('_未识别切面。_');
|
|
28
|
+
return lines;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
for (const fr of facetFiles) {
|
|
32
|
+
lines.push(`### \`${fileRel(fr.file)}\` — ${fr.facetCount} 切面,均模板化 ${fr.avgTemplatingRate}`, '');
|
|
33
|
+
for (const facet of fr.facets) {
|
|
34
|
+
const roles = Object.entries(facet.stats.roles)
|
|
35
|
+
.filter(([, n]) => n)
|
|
36
|
+
.map(([k, n]) => `${k}:${n}`)
|
|
37
|
+
.join(', ');
|
|
38
|
+
lines.push(
|
|
39
|
+
`- **${facet.kind}** \`${facet.id}\` · ${facet.anchorNode} · nodes ${facet.stats.nodes} · ctrl ${facet.stats.interactiveControls}`,
|
|
40
|
+
);
|
|
41
|
+
if (facet.layoutHints.length) lines.push(` - 布局线索:${facet.layoutHints.join(', ')}`);
|
|
42
|
+
if (roles) lines.push(` - 角色分布:${roles}`);
|
|
43
|
+
if (facet.aria) lines.push(` - a11y:${JSON.stringify(facet.aria)}`);
|
|
44
|
+
if (facet.templatingRate > 0) lines.push(` - 模板化率:**${facet.templatingRate}**`);
|
|
45
|
+
for (const sig of facet.templateSignals) {
|
|
46
|
+
lines.push(` - ${sig.kind}:${sig.note}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
lines.push('');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const limitLabel =
|
|
53
|
+
cap == null
|
|
54
|
+
? '全量(maxFiles=0)'
|
|
55
|
+
: options?.maxFiles != null
|
|
56
|
+
? `--facet-limit ${options.maxFiles}`
|
|
57
|
+
: `默认 ${DEFAULT_FACET_MARKDOWN_FILE_LIMIT}`;
|
|
58
|
+
|
|
59
|
+
lines.push(
|
|
60
|
+
...formatFacetTruncationGuide({
|
|
61
|
+
channel: 'markdown',
|
|
62
|
+
shown: facetFiles.length,
|
|
63
|
+
total: totalWithFacets,
|
|
64
|
+
limitLabel,
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
return lines;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function formatFacetReportMarkdown(
|
|
72
|
+
title: string,
|
|
73
|
+
facetReports: FileFacetReport[],
|
|
74
|
+
stats?: ScanResult['stats'],
|
|
75
|
+
options?: { maxFiles?: number },
|
|
76
|
+
): string {
|
|
77
|
+
const lines: string[] = [`## ${title}`, ''];
|
|
78
|
+
if (stats) {
|
|
79
|
+
lines.push(
|
|
80
|
+
`- 扫描 Vue:**${stats.vueFilesScanned}**(消费方 **${stats.vueFilesLibraryConsumers}**)`,
|
|
81
|
+
`- 切面区域合计:**${stats.facetRegions ?? 0}**(含切面文件 **${facetReports.filter((f) => f.facetCount > 0).length}**)`,
|
|
82
|
+
'',
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
lines.push(...formatFacetMarkdownBlocks(facetReports, { maxFiles: options?.maxFiles }));
|
|
86
|
+
return lines.join('\n');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function formatFacetSummary(reports: FileFacetReport[], limit?: number): string {
|
|
90
|
+
const withFacets = reports.filter((r) => r.facetCount > 0);
|
|
91
|
+
if (!withFacets.length) return '[uiscope:facet] 未识别到交互切面';
|
|
92
|
+
|
|
93
|
+
const cap = resolveFacetFileLimit(limit, DEFAULT_FACET_SUMMARY_FILE_LIMIT);
|
|
94
|
+
const slice = cap == null ? withFacets : withFacets.slice(0, cap);
|
|
95
|
+
|
|
96
|
+
const lines: string[] = ['[uiscope:facet] 切面摘要:'];
|
|
97
|
+
for (const r of slice) {
|
|
98
|
+
const rel = r.file.replace(/\\/g, '/').split('/').slice(-2).join('/');
|
|
99
|
+
lines.push(`\n${rel} — ${r.facetCount} 切面, 均模板化 ${r.avgTemplatingRate}`);
|
|
100
|
+
for (const f of r.facets) {
|
|
101
|
+
const hints = f.layoutHints.length ? ` [${f.layoutHints.join(',')}]` : '';
|
|
102
|
+
const tmpl = f.templatingRate > 0 ? ` tmpl=${f.templatingRate}` : '';
|
|
103
|
+
lines.push(
|
|
104
|
+
` · ${f.kind} @ ${f.anchorNode} nodes=${f.stats.nodes} ctrl=${f.stats.interactiveControls}${hints}${tmpl}`,
|
|
105
|
+
);
|
|
106
|
+
for (const sig of f.templateSignals.slice(0, 2)) {
|
|
107
|
+
lines.push(` - ${sig.kind}: ${sig.note}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const limitLabel =
|
|
113
|
+
cap == null
|
|
114
|
+
? '全量(--facet-limit 0)'
|
|
115
|
+
: limit != null
|
|
116
|
+
? `--facet-limit ${limit}`
|
|
117
|
+
: `默认 ${DEFAULT_FACET_SUMMARY_FILE_LIMIT}`;
|
|
118
|
+
|
|
119
|
+
lines.push(
|
|
120
|
+
...formatFacetTruncationGuide({
|
|
121
|
+
channel: 'terminal',
|
|
122
|
+
shown: slice.length,
|
|
123
|
+
total: withFacets.length,
|
|
124
|
+
limitLabel,
|
|
125
|
+
}),
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
return lines.join('\n');
|
|
129
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { StyleOverrideHit } from '../types.js';
|
|
2
|
+
|
|
3
|
+
export function formatStyleHits(hits: StyleOverrideHit[], threshold: number): string {
|
|
4
|
+
const over = hits.filter((h) => h.count > threshold);
|
|
5
|
+
if (!over.length) {
|
|
6
|
+
return `[uiscope] 未发现超过阈值 ${threshold} 的 设计库样式重载`;
|
|
7
|
+
}
|
|
8
|
+
const lines: string[] = ['[uiscope] 设计库样式重载摘要(超过阈值):'];
|
|
9
|
+
for (const hit of over) {
|
|
10
|
+
lines.push(` .${hit.className} — ${hit.count} 处`);
|
|
11
|
+
for (const occ of hit.occurrences.slice(0, 8)) {
|
|
12
|
+
const line = occ.line != null ? `:${occ.line}` : '';
|
|
13
|
+
lines.push(` - ${occ.file}${line} ${occ.selector}`);
|
|
14
|
+
}
|
|
15
|
+
if (hit.occurrences.length > 8) {
|
|
16
|
+
lines.push(` … 另有 ${hit.occurrences.length - 8} 处`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return lines.join('\n');
|
|
20
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { ComponentTreeNode, FileComponentReport } from '../types.js';
|
|
2
|
+
|
|
3
|
+
function formatAttrs(attrs?: Record<string, string>): string {
|
|
4
|
+
if (!attrs || !Object.keys(attrs).length) return '';
|
|
5
|
+
const parts = Object.entries(attrs)
|
|
6
|
+
.filter(([, v]) => v)
|
|
7
|
+
.map(([k, v]) => `${k}="${v}"`);
|
|
8
|
+
return parts.length ? ` {${parts.join(', ')}}` : '';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function formatTreeNodeLines(node: ComponentTreeNode, indent: number): string[] {
|
|
12
|
+
const pad = ' '.repeat(indent);
|
|
13
|
+
const label =
|
|
14
|
+
node.kind === 'library'
|
|
15
|
+
? `lib:${node.name}`
|
|
16
|
+
: node.kind === 'local'
|
|
17
|
+
? `local:${node.name}`
|
|
18
|
+
: node.kind === 'html'
|
|
19
|
+
? `html:${node.name}`
|
|
20
|
+
: node.kind === 'root'
|
|
21
|
+
? node.name
|
|
22
|
+
: `?${node.name}`;
|
|
23
|
+
const src = node.source ? ` (${node.source})` : '';
|
|
24
|
+
const attrs = formatAttrs(node.attrs);
|
|
25
|
+
const lines = [`${pad}- \`${label}${src}${attrs}\``];
|
|
26
|
+
if (node.libraryDoc) {
|
|
27
|
+
const snippet = node.libraryDoc.split('\n').slice(0, 3).join(' ').trim().slice(0, 160);
|
|
28
|
+
lines.push(`${pad} - _库注释摘要_: ${snippet}${node.libraryDoc.length > 160 ? '…' : ''}`);
|
|
29
|
+
}
|
|
30
|
+
for (const child of node.children) {
|
|
31
|
+
lines.push(...formatTreeNodeLines(child, indent + 1));
|
|
32
|
+
}
|
|
33
|
+
return lines;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function formatNode(node: ComponentTreeNode, indent: number): string[] {
|
|
37
|
+
const pad = ' '.repeat(indent);
|
|
38
|
+
const label =
|
|
39
|
+
node.kind === 'library'
|
|
40
|
+
? `lib:${node.name}`
|
|
41
|
+
: node.kind === 'local'
|
|
42
|
+
? `local:${node.name}`
|
|
43
|
+
: node.kind === 'html'
|
|
44
|
+
? `html:${node.name}`
|
|
45
|
+
: node.kind === 'root'
|
|
46
|
+
? node.name
|
|
47
|
+
: `?${node.name}`;
|
|
48
|
+
const src = node.source ? ` (${node.source})` : '';
|
|
49
|
+
const attrs = formatAttrs(node.attrs);
|
|
50
|
+
const lines = [`${pad}${label}${src}${attrs}`];
|
|
51
|
+
if (node.libraryDoc) {
|
|
52
|
+
lines.push(`${pad} doc: ${node.libraryDoc.split('\n')[0]?.slice(0, 100)}`);
|
|
53
|
+
}
|
|
54
|
+
for (const child of node.children) {
|
|
55
|
+
lines.push(...formatNode(child, indent + 1));
|
|
56
|
+
}
|
|
57
|
+
return lines;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function formatFileTree(report: FileComponentReport): string {
|
|
61
|
+
const header = `${report.file}${report.usesLibrary ? ' [uses library]' : ''}`;
|
|
62
|
+
const body = formatNode(report.tree, 1);
|
|
63
|
+
return [header, ...body].join('\n');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function formatAllTrees(reports: FileComponentReport[], onlyLibrary: boolean): string {
|
|
67
|
+
const filtered = onlyLibrary ? reports.filter((r) => r.usesLibrary) : reports;
|
|
68
|
+
if (!filtered.length) return '[uiscope] 未找到使用设计库 的 Vue 文件';
|
|
69
|
+
return filtered.map(formatFileTree).join('\n\n');
|
|
70
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { UxFinding } from '../types.js';
|
|
2
|
+
import { ENHANCEMENT_REGISTRY } from '../enhance/registry.js';
|
|
3
|
+
|
|
4
|
+
export function formatUxFindings(findings: UxFinding[], limit = 20): string {
|
|
5
|
+
if (!findings.length) return '[uiscope:ux] 未命中 UX 规则';
|
|
6
|
+
const lines: string[] = ['[uiscope:ux] 发现项:'];
|
|
7
|
+
for (const f of findings.slice(0, limit)) {
|
|
8
|
+
const opt = f.optimization?.selectable ? ` → 可选优化: ${f.optimization.title}` : '';
|
|
9
|
+
lines.push(` [${f.severity}] ${f.ruleId} · ${f.message}${opt}`);
|
|
10
|
+
}
|
|
11
|
+
if (findings.length > limit) {
|
|
12
|
+
lines.push(` … 另有 ${findings.length - limit} 条(--json 或增大输出限制)`);
|
|
13
|
+
}
|
|
14
|
+
const selectable = findings.filter((f) => f.optimization?.selectable);
|
|
15
|
+
if (selectable.length) {
|
|
16
|
+
lines.push(
|
|
17
|
+
'',
|
|
18
|
+
'optimization 字段供勾选;改代码前须用户批准',
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
return lines.join('\n');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function formatUxFindingsMarkdown(findings: UxFinding[]): string {
|
|
25
|
+
const lines: string[] = ['## UX 扫描发现', ''];
|
|
26
|
+
if (!findings.length) {
|
|
27
|
+
lines.push('_无_');
|
|
28
|
+
return lines.join('\n');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const byEnhancement = new Map<string, UxFinding[]>();
|
|
32
|
+
for (const f of findings) {
|
|
33
|
+
const list = byEnhancement.get(f.enhancement) ?? [];
|
|
34
|
+
list.push(f);
|
|
35
|
+
byEnhancement.set(f.enhancement, list);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
for (const [enhId, list] of byEnhancement) {
|
|
39
|
+
const def = ENHANCEMENT_REGISTRY.find((e) => e.id === enhId);
|
|
40
|
+
lines.push(`### ${def?.label ?? enhId}`, '');
|
|
41
|
+
if (def?.description) lines.push(def.description, '');
|
|
42
|
+
for (const f of list) {
|
|
43
|
+
lines.push(`- **${f.severity}** \`${f.ruleId}\` — ${f.message}`);
|
|
44
|
+
if (f.optimization) {
|
|
45
|
+
lines.push(
|
|
46
|
+
` - 优化(可选): **${f.optimization.title}** — ${f.optimization.description}`,
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
lines.push('');
|
|
51
|
+
}
|
|
52
|
+
return lines.join('\n');
|
|
53
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import readline from 'node:readline';
|
|
3
|
+
import type { ScanResult } from '../types.js';
|
|
4
|
+
|
|
5
|
+
export type ScanReportArraySection =
|
|
6
|
+
| 'trees'
|
|
7
|
+
| 'facetReports'
|
|
8
|
+
| 'uxFindings'
|
|
9
|
+
| 'styleOverrides'
|
|
10
|
+
| 'styleChains'
|
|
11
|
+
| 'warnings'
|
|
12
|
+
| 'enhancementsRun';
|
|
13
|
+
|
|
14
|
+
export interface ScanReportCounts {
|
|
15
|
+
trees: number;
|
|
16
|
+
facetReports: number;
|
|
17
|
+
facetRegions: number;
|
|
18
|
+
uxFindings: number;
|
|
19
|
+
styleOverrides: number;
|
|
20
|
+
styleChains: number;
|
|
21
|
+
warnings: number;
|
|
22
|
+
enhancementsRun: number;
|
|
23
|
+
stats: ScanResult['stats'] | null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ScanReportSliceRange {
|
|
27
|
+
/** 数组区段起始下标,默认 0 */
|
|
28
|
+
offset?: number;
|
|
29
|
+
/** 最多返回条数;省略表示直到区段末尾 */
|
|
30
|
+
limit?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ScanReportSlice<T = unknown> {
|
|
34
|
+
section: ScanReportArraySection;
|
|
35
|
+
total: number;
|
|
36
|
+
offset: number;
|
|
37
|
+
limit: number;
|
|
38
|
+
items: T[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface TextReportLineRange {
|
|
42
|
+
/** 1-based inclusive */
|
|
43
|
+
startLine: number;
|
|
44
|
+
/** 1-based inclusive */
|
|
45
|
+
endLine: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface TextReportSlice {
|
|
49
|
+
totalLines: number;
|
|
50
|
+
startLine: number;
|
|
51
|
+
endLine: number;
|
|
52
|
+
text: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
56
|
+
return typeof value === 'object' && value !== null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function isScanResultPayload(value: unknown): value is ScanResult {
|
|
60
|
+
if (!isRecord(value)) return false;
|
|
61
|
+
return (
|
|
62
|
+
Array.isArray(value.trees)
|
|
63
|
+
&& Array.isArray(value.facetReports)
|
|
64
|
+
&& Array.isArray(value.uxFindings)
|
|
65
|
+
&& Array.isArray(value.warnings)
|
|
66
|
+
&& isRecord(value.stats)
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function readScanReportJsonFile(filePath: string): ScanResult {
|
|
71
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
72
|
+
const parsed: unknown = JSON.parse(raw);
|
|
73
|
+
if (!isScanResultPayload(parsed)) {
|
|
74
|
+
throw new Error(`[uiscope] 非 ScanResult JSON: ${filePath}`);
|
|
75
|
+
}
|
|
76
|
+
return parsed;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function getScanReportCounts(report: ScanResult): ScanReportCounts {
|
|
80
|
+
const facetRegions =
|
|
81
|
+
report.stats?.facetRegions
|
|
82
|
+
?? report.facetReports.reduce((n, r) => n + r.facetCount, 0);
|
|
83
|
+
return {
|
|
84
|
+
trees: report.trees.length,
|
|
85
|
+
facetReports: report.facetReports.length,
|
|
86
|
+
facetRegions,
|
|
87
|
+
uxFindings: report.uxFindings.length,
|
|
88
|
+
styleOverrides: report.styleOverrides.length,
|
|
89
|
+
styleChains: report.styleChains.length,
|
|
90
|
+
warnings: report.warnings.length,
|
|
91
|
+
enhancementsRun: report.enhancementsRun?.length ?? 0,
|
|
92
|
+
stats: report.stats ?? null,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function getScanReportCountsFromFile(filePath: string): ScanReportCounts {
|
|
97
|
+
return getScanReportCounts(readScanReportJsonFile(filePath));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function normalizeRange(
|
|
101
|
+
total: number,
|
|
102
|
+
range?: ScanReportSliceRange,
|
|
103
|
+
): { offset: number; limit: number } {
|
|
104
|
+
const offset = Math.max(0, range?.offset ?? 0);
|
|
105
|
+
if (offset >= total) {
|
|
106
|
+
return { offset, limit: 0 };
|
|
107
|
+
}
|
|
108
|
+
const rest = total - offset;
|
|
109
|
+
const limit =
|
|
110
|
+
range?.limit == null || range.limit < 0
|
|
111
|
+
? rest
|
|
112
|
+
: Math.min(rest, range.limit);
|
|
113
|
+
return { offset, limit };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function sliceScanReportSection<T = unknown>(
|
|
117
|
+
report: ScanResult,
|
|
118
|
+
section: ScanReportArraySection,
|
|
119
|
+
range?: ScanReportSliceRange,
|
|
120
|
+
): ScanReportSlice<T> {
|
|
121
|
+
const all = report[section] as T[];
|
|
122
|
+
const total = all.length;
|
|
123
|
+
const { offset, limit } = normalizeRange(total, range);
|
|
124
|
+
const items = limit > 0 ? all.slice(offset, offset + limit) : [];
|
|
125
|
+
return {
|
|
126
|
+
section,
|
|
127
|
+
total,
|
|
128
|
+
offset,
|
|
129
|
+
limit: items.length,
|
|
130
|
+
items,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function readScanReportSectionFromFile<T = unknown>(
|
|
135
|
+
filePath: string,
|
|
136
|
+
section: ScanReportArraySection,
|
|
137
|
+
range?: ScanReportSliceRange,
|
|
138
|
+
): ScanReportSlice<T> {
|
|
139
|
+
const report = readScanReportJsonFile(filePath);
|
|
140
|
+
return sliceScanReportSection<T>(report, section, range);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 统计 Markdown 报告行数(流式,避免整文件入内存) */
|
|
144
|
+
export async function countTextReportLines(filePath: string): Promise<number> {
|
|
145
|
+
const stream = fs.createReadStream(filePath, { encoding: 'utf8' });
|
|
146
|
+
const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
|
|
147
|
+
let n = 0;
|
|
148
|
+
for await (const _line of rl) {
|
|
149
|
+
n += 1;
|
|
150
|
+
}
|
|
151
|
+
return n;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** 按行号读取 Markdown 报告片段(1-based,含首尾行) */
|
|
155
|
+
export async function readTextReportLines(
|
|
156
|
+
filePath: string,
|
|
157
|
+
range: TextReportLineRange,
|
|
158
|
+
): Promise<TextReportSlice> {
|
|
159
|
+
const startLine = Math.max(1, range.startLine);
|
|
160
|
+
const endLine = Math.max(startLine, range.endLine);
|
|
161
|
+
const stream = fs.createReadStream(filePath, { encoding: 'utf8' });
|
|
162
|
+
const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
|
|
163
|
+
const lines: string[] = [];
|
|
164
|
+
let lineNo = 0;
|
|
165
|
+
let totalLines = 0;
|
|
166
|
+
|
|
167
|
+
for await (const line of rl) {
|
|
168
|
+
lineNo += 1;
|
|
169
|
+
totalLines = lineNo;
|
|
170
|
+
if (lineNo >= startLine && lineNo <= endLine) {
|
|
171
|
+
lines.push(line);
|
|
172
|
+
}
|
|
173
|
+
if (lineNo > endLine) {
|
|
174
|
+
for await (const _rest of rl) {
|
|
175
|
+
totalLines += 1;
|
|
176
|
+
}
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
totalLines,
|
|
183
|
+
startLine,
|
|
184
|
+
endLine: Math.min(endLine, startLine + lines.length - 1),
|
|
185
|
+
text: lines.join('\n'),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** 同步按行读取(适合中等体量;大行数请用 `readTextReportLines`) */
|
|
190
|
+
export function readTextReportLinesSync(
|
|
191
|
+
filePath: string,
|
|
192
|
+
range: TextReportLineRange,
|
|
193
|
+
): TextReportSlice {
|
|
194
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
195
|
+
const allLines = content.split(/\r?\n/);
|
|
196
|
+
const totalLines = allLines.length;
|
|
197
|
+
const startLine = Math.max(1, Math.min(range.startLine, totalLines));
|
|
198
|
+
const endLine = Math.max(startLine, Math.min(range.endLine, totalLines));
|
|
199
|
+
const text = allLines.slice(startLine - 1, endLine).join('\n');
|
|
200
|
+
return { totalLines, startLine, endLine, text };
|
|
201
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { LIB_STYLE_DEFAULT, LIB_STYLE_STRICT } from '../config/defaults.js';
|
|
5
|
+
import { loadScanConfig, mergeConfig } from '../config/load-config.js';
|
|
6
|
+
import { applyPresets } from '../config/resolve-presets.js';
|
|
7
|
+
import { resolveThresholds } from '../config/resolve-thresholds.js';
|
|
8
|
+
import type { UiscopeConfig, ScanScenarioResult } from '../types.js';
|
|
9
|
+
import { findMonorepoRoot } from '../paths/monorepo-root.js';
|
|
10
|
+
import { resolveOnlyLibraryConsumers } from '../config/resolve-scan-scope.js';
|
|
11
|
+
import { runScan, type ScanOptions } from '../run-scan.js';
|
|
12
|
+
import { CONFIG_FILE_CANDIDATES } from '../cli/brand.js';
|
|
13
|
+
|
|
14
|
+
export interface ReportScenarioDef {
|
|
15
|
+
id: string;
|
|
16
|
+
label: string;
|
|
17
|
+
patch: Partial<UiscopeConfig>;
|
|
18
|
+
cwd: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function buildMaintenanceScenarios(
|
|
22
|
+
scanPkgDir: string,
|
|
23
|
+
pkgConfig: UiscopeConfig,
|
|
24
|
+
consumerCwd?: string,
|
|
25
|
+
): ReportScenarioDef[] {
|
|
26
|
+
const scenarios: ReportScenarioDef[] = [];
|
|
27
|
+
|
|
28
|
+
if (pkgConfig.libraryRoots.length > 0) {
|
|
29
|
+
const libraryAbs = path.resolve(scanPkgDir, pkgConfig.libraryRoots[0]);
|
|
30
|
+
if (fs.existsSync(libraryAbs)) {
|
|
31
|
+
const libraryRel = path.relative(scanPkgDir, libraryAbs).replace(/\\/g, '/');
|
|
32
|
+
const scanRoots = pkgConfig.roots.length ? pkgConfig.roots : [libraryRel];
|
|
33
|
+
|
|
34
|
+
scenarios.push(
|
|
35
|
+
{
|
|
36
|
+
id: 'lib-source-all',
|
|
37
|
+
label: '设计库:全部组件 SFC(样式/结构自检)',
|
|
38
|
+
cwd: scanPkgDir,
|
|
39
|
+
patch: {
|
|
40
|
+
roots: scanRoots,
|
|
41
|
+
onlyLibraryConsumers: false,
|
|
42
|
+
thresholds: { style: LIB_STYLE_DEFAULT },
|
|
43
|
+
libraryRoots: pkgConfig.libraryRoots,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
id: 'lib-style-strict',
|
|
48
|
+
label: '设计库:全量 SFC + 样式阈值严格',
|
|
49
|
+
cwd: scanPkgDir,
|
|
50
|
+
patch: {
|
|
51
|
+
roots: scanRoots,
|
|
52
|
+
onlyLibraryConsumers: false,
|
|
53
|
+
thresholds: { style: LIB_STYLE_STRICT },
|
|
54
|
+
libraryRoots: pkgConfig.libraryRoots,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const consumerConfigName = CONFIG_FILE_CANDIDATES.find((name) =>
|
|
62
|
+
consumerCwd ? fs.existsSync(path.join(consumerCwd, name)) : false,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
if (consumerCwd && consumerConfigName) {
|
|
66
|
+
scenarios.push(
|
|
67
|
+
{
|
|
68
|
+
id: 'consumer-strict',
|
|
69
|
+
label: '消费方:仅设计库消费文件 + 较严阈值',
|
|
70
|
+
cwd: consumerCwd,
|
|
71
|
+
patch: {
|
|
72
|
+
onlyLibraryConsumers: true,
|
|
73
|
+
thresholds: { style: { overrideRuleCount: 5 } },
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
id: 'consumer-relaxed',
|
|
78
|
+
label: '消费方:仅设计库消费 + 样式阈值宽松',
|
|
79
|
+
cwd: consumerCwd,
|
|
80
|
+
patch: {
|
|
81
|
+
onlyLibraryConsumers: true,
|
|
82
|
+
thresholds: { style: { overrideRuleCount: 15, warnOrphanOverrides: false } },
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
id: 'consumer-all-vue',
|
|
87
|
+
label: '消费方:全部 Vue + 默认阈值',
|
|
88
|
+
cwd: consumerCwd,
|
|
89
|
+
patch: { onlyLibraryConsumers: false },
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return scenarios;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function runScenario(
|
|
98
|
+
base: UiscopeConfig,
|
|
99
|
+
def: ReportScenarioDef,
|
|
100
|
+
): Promise<ScanScenarioResult> {
|
|
101
|
+
const mergedBase = await applyPresets(def.cwd, base);
|
|
102
|
+
const config = mergeConfig(mergedBase, def.patch);
|
|
103
|
+
const opts: ScanOptions = {
|
|
104
|
+
cwd: def.cwd,
|
|
105
|
+
config,
|
|
106
|
+
mode: 'all',
|
|
107
|
+
onlyLibraryTrees: false,
|
|
108
|
+
};
|
|
109
|
+
const result = await runScan(opts);
|
|
110
|
+
const resolved = resolveThresholds(config);
|
|
111
|
+
return {
|
|
112
|
+
id: def.id,
|
|
113
|
+
label: def.label,
|
|
114
|
+
options: {
|
|
115
|
+
roots: config.roots,
|
|
116
|
+
onlyLibraryConsumers: resolveOnlyLibraryConsumers(config),
|
|
117
|
+
thresholds: resolved,
|
|
118
|
+
...(config.consumerWrapperTags?.length
|
|
119
|
+
? { consumerWrapperTags: config.consumerWrapperTags.join(',') }
|
|
120
|
+
: {}),
|
|
121
|
+
},
|
|
122
|
+
result,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function resolveConsumerCwd(scanPkgDir: string): string | undefined {
|
|
127
|
+
const candidate = path.join(findMonorepoRoot(scanPkgDir), 'apps/web');
|
|
128
|
+
return fs.existsSync(path.join(candidate, 'package.json')) ? candidate : undefined;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function scanPackageDir(): string {
|
|
132
|
+
return path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export async function loadMaintenanceScenarioDefs(scanPkgDir: string): Promise<ReportScenarioDef[]> {
|
|
136
|
+
const consumerCwd = resolveConsumerCwd(scanPkgDir);
|
|
137
|
+
const pkgConfig = await loadScanConfig(scanPkgDir);
|
|
138
|
+
return buildMaintenanceScenarios(scanPkgDir, pkgConfig, consumerCwd);
|
|
139
|
+
}
|