@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,65 @@
|
|
|
1
|
+
import type { UiscopeFacetThresholds, UiscopeStyleThresholds, UiscopeThresholds, UiscopeTreeThresholds } from '../types.js';
|
|
2
|
+
|
|
3
|
+
/** 库内组件自检:样式敏感 */
|
|
4
|
+
export const LIB_STYLE_STRICT: UiscopeStyleThresholds = {
|
|
5
|
+
overrideRuleCount: 1,
|
|
6
|
+
excludeGlobs: [],
|
|
7
|
+
warnOrphanOverrides: true,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/** 库内默认 */
|
|
11
|
+
export const LIB_STYLE_DEFAULT: UiscopeStyleThresholds = {
|
|
12
|
+
overrideRuleCount: 4,
|
|
13
|
+
excludeGlobs: [],
|
|
14
|
+
warnOrphanOverrides: true,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** 消费方:约定皮肤单文件内多 variant 不计入散布重载 */
|
|
18
|
+
export const CONSUMER_STYLE_WITH_SKIN: UiscopeStyleThresholds = {
|
|
19
|
+
overrideRuleCount: 8,
|
|
20
|
+
excludeGlobs: [],
|
|
21
|
+
warnOrphanOverrides: false,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const DEFAULT_STYLE_THRESHOLDS: UiscopeStyleThresholds = {
|
|
25
|
+
/** 单 class 规则条数;≥5 可容忍单文件多 variant 块 */
|
|
26
|
+
overrideRuleCount: 5,
|
|
27
|
+
excludeGlobs: [],
|
|
28
|
+
warnOrphanOverrides: true,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const DEFAULT_FACET_THRESHOLDS: UiscopeFacetThresholds = {
|
|
32
|
+
minSubtreeNodes: 6,
|
|
33
|
+
minInteractiveControls: 2,
|
|
34
|
+
minFacetComponents: 4,
|
|
35
|
+
minRepeatedSiblings: 3,
|
|
36
|
+
classOverlapRatio: 0.5,
|
|
37
|
+
templateWarnRate: 0.65,
|
|
38
|
+
multiFacetWarnCount: 4,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const DEFAULT_TREE_THRESHOLDS: UiscopeTreeThresholds = {
|
|
42
|
+
maxLibraryNestDepth: 5,
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const DEFAULT_THRESHOLDS: UiscopeThresholds = {
|
|
46
|
+
style: DEFAULT_STYLE_THRESHOLDS,
|
|
47
|
+
facet: DEFAULT_FACET_THRESHOLDS,
|
|
48
|
+
tree: DEFAULT_TREE_THRESHOLDS,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export function mergeThresholds(
|
|
52
|
+
base: UiscopeThresholds,
|
|
53
|
+
patch?: Partial<{
|
|
54
|
+
style: Partial<UiscopeStyleThresholds>;
|
|
55
|
+
facet: Partial<UiscopeFacetThresholds>;
|
|
56
|
+
tree: Partial<UiscopeTreeThresholds>;
|
|
57
|
+
}>,
|
|
58
|
+
): UiscopeThresholds {
|
|
59
|
+
if (!patch) return base;
|
|
60
|
+
return {
|
|
61
|
+
style: { ...base.style, ...patch.style },
|
|
62
|
+
facet: { ...base.facet, ...patch.facet },
|
|
63
|
+
tree: { ...base.tree, ...patch.tree },
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { UiscopeConfig, UiscopeFramework } from '../types.js';
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_FRAMEWORKS: UiscopeFramework[] = ['vue', 'react'];
|
|
4
|
+
|
|
5
|
+
export function resolveFrameworks(
|
|
6
|
+
config: UiscopeConfig,
|
|
7
|
+
cliFrameworks?: UiscopeFramework[],
|
|
8
|
+
): UiscopeFramework[] {
|
|
9
|
+
const fromCli = cliFrameworks?.length ? cliFrameworks : undefined;
|
|
10
|
+
const fromConfig = config.frameworks?.length ? config.frameworks : undefined;
|
|
11
|
+
const resolved = fromCli ?? fromConfig ?? DEFAULT_FRAMEWORKS;
|
|
12
|
+
return [...new Set(resolved)];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function frameworkIncludes(
|
|
16
|
+
frameworks: UiscopeFramework[],
|
|
17
|
+
target: UiscopeFramework,
|
|
18
|
+
): boolean {
|
|
19
|
+
return frameworks.includes(target);
|
|
20
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { createJiti } from 'jiti';
|
|
4
|
+
import type { UiscopeConfig } from '../types.js';
|
|
5
|
+
import { CONFIG_FILE_CANDIDATES, LEGACY_CONFIG_FILE_CANDIDATES } from '../cli/brand.js';
|
|
6
|
+
import { normalizeLegacyScanConfig } from './normalize-legacy-config.js';
|
|
7
|
+
import { DEFAULT_FRAMEWORKS } from './frameworks.js';
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_CONFIG: UiscopeConfig = {
|
|
10
|
+
roots: ['src'],
|
|
11
|
+
ignore: ['**/node_modules/**', '**/dist/**', '**/.nuxt/**', '**/.output/**'],
|
|
12
|
+
frameworks: DEFAULT_FRAMEWORKS,
|
|
13
|
+
presets: [],
|
|
14
|
+
libraryImportSources: [],
|
|
15
|
+
libraryRoots: [],
|
|
16
|
+
thresholds: {},
|
|
17
|
+
consumerWrapperTags: [],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export async function loadScanConfig(
|
|
21
|
+
cwd: string,
|
|
22
|
+
configFileName?: string,
|
|
23
|
+
): Promise<UiscopeConfig> {
|
|
24
|
+
const candidates = configFileName
|
|
25
|
+
? [configFileName]
|
|
26
|
+
: [...CONFIG_FILE_CANDIDATES, ...LEGACY_CONFIG_FILE_CANDIDATES];
|
|
27
|
+
const jiti = createJiti(cwd, { interopDefault: true });
|
|
28
|
+
for (const name of candidates) {
|
|
29
|
+
const file = path.isAbsolute(name) ? name : path.join(cwd, name);
|
|
30
|
+
if (!fs.existsSync(file)) continue;
|
|
31
|
+
const mod = await jiti.import(file) as { default?: unknown; config?: unknown };
|
|
32
|
+
const raw = mod.default ?? mod.config;
|
|
33
|
+
if (raw && typeof raw === 'object') {
|
|
34
|
+
const normalized = normalizeLegacyScanConfig(raw as Record<string, unknown>);
|
|
35
|
+
return mergeConfig(DEFAULT_CONFIG, normalized);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return { ...DEFAULT_CONFIG };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function mergeConfig(base: UiscopeConfig, patch: Partial<UiscopeConfig>): UiscopeConfig {
|
|
42
|
+
return {
|
|
43
|
+
roots: patch.roots ?? base.roots,
|
|
44
|
+
ignore: patch.ignore ?? base.ignore,
|
|
45
|
+
frameworks: patch.frameworks ?? base.frameworks,
|
|
46
|
+
presets: patch.presets ?? base.presets,
|
|
47
|
+
presetModules: patch.presetModules ?? base.presetModules,
|
|
48
|
+
libraryImportSources: patch.libraryImportSources ?? base.libraryImportSources,
|
|
49
|
+
libraryRoots: patch.libraryRoots ?? base.libraryRoots,
|
|
50
|
+
overrideThreshold: patch.overrideThreshold ?? base.overrideThreshold,
|
|
51
|
+
thresholds: patch.thresholds
|
|
52
|
+
? {
|
|
53
|
+
style: { ...base.thresholds?.style, ...patch.thresholds.style },
|
|
54
|
+
facet: { ...base.thresholds?.facet, ...patch.thresholds.facet },
|
|
55
|
+
tree: { ...base.thresholds?.tree, ...patch.thresholds.tree },
|
|
56
|
+
}
|
|
57
|
+
: base.thresholds,
|
|
58
|
+
onlyLibraryConsumers: patch.onlyLibraryConsumers ?? base.onlyLibraryConsumers,
|
|
59
|
+
consumerWrapperTags: patch.consumerWrapperTags ?? base.consumerWrapperTags,
|
|
60
|
+
enhancements: patch.enhancements
|
|
61
|
+
? {
|
|
62
|
+
...base.enhancements,
|
|
63
|
+
...patch.enhancements,
|
|
64
|
+
actionTags: patch.enhancements.actionTags ?? base.enhancements?.actionTags,
|
|
65
|
+
feedbackTags: patch.enhancements.feedbackTags ?? base.enhancements?.feedbackTags,
|
|
66
|
+
workbenchExpectations:
|
|
67
|
+
patch.enhancements.workbenchExpectations ?? base.enhancements?.workbenchExpectations,
|
|
68
|
+
wrapperRoles: patch.enhancements.wrapperRoles ?? base.enhancements?.wrapperRoles,
|
|
69
|
+
rulePack: patch.enhancements.rulePack
|
|
70
|
+
? { ...base.enhancements?.rulePack, ...patch.enhancements.rulePack }
|
|
71
|
+
: base.enhancements?.rulePack,
|
|
72
|
+
rulePackPath: patch.enhancements.rulePackPath ?? base.enhancements?.rulePackPath,
|
|
73
|
+
}
|
|
74
|
+
: base.enhancements,
|
|
75
|
+
defaultEnhancements: patch.defaultEnhancements ?? base.defaultEnhancements,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function resolveRoots(cwd: string, roots: string[]): string[] {
|
|
80
|
+
return roots.map((r) => path.resolve(cwd, r));
|
|
81
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { UiscopeConfig } from '../types.js';
|
|
2
|
+
|
|
3
|
+
type RawConfig = Record<string, unknown>;
|
|
4
|
+
|
|
5
|
+
export function normalizeLegacyScanConfig(raw: RawConfig): Partial<UiscopeConfig> {
|
|
6
|
+
const patch: Partial<UiscopeConfig> = { ...raw } as Partial<UiscopeConfig>;
|
|
7
|
+
|
|
8
|
+
const ocpSources = raw.ocpImportSources;
|
|
9
|
+
if (Array.isArray(ocpSources) && ocpSources.every((x) => typeof x === 'string')) {
|
|
10
|
+
const existing = patch.libraryImportSources ?? [];
|
|
11
|
+
patch.libraryImportSources = [...new Set([...existing, ...ocpSources])];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (raw.onlyOcpConsumers !== undefined && patch.onlyLibraryConsumers === undefined) {
|
|
15
|
+
patch.onlyLibraryConsumers = Boolean(raw.onlyOcpConsumers);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const thresholds = raw.thresholds;
|
|
19
|
+
if (thresholds && typeof thresholds === 'object') {
|
|
20
|
+
const tree = (thresholds as RawConfig).tree;
|
|
21
|
+
if (tree && typeof tree === 'object') {
|
|
22
|
+
const maxOcp = (tree as RawConfig).maxOcpNestDepth;
|
|
23
|
+
if (typeof maxOcp === 'number') {
|
|
24
|
+
patch.thresholds = {
|
|
25
|
+
...patch.thresholds,
|
|
26
|
+
tree: {
|
|
27
|
+
...patch.thresholds?.tree,
|
|
28
|
+
maxLibraryNestDepth: maxOcp,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
delete (patch as RawConfig).ocpImportSources;
|
|
36
|
+
delete (patch as RawConfig).onlyOcpConsumers;
|
|
37
|
+
|
|
38
|
+
return patch;
|
|
39
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { UiscopeConfig } from '../types.js';
|
|
2
|
+
import type { LibraryStyleWhitelistEntry } from '../manifest/library-style-whitelist.js';
|
|
3
|
+
import type { UxComponentRole } from '../manifest/library-ux-taxonomy.js';
|
|
4
|
+
|
|
5
|
+
/** 组件库扩展 preset:合并进 UiscopeConfig,不侵入业务仓库 */
|
|
6
|
+
export interface UiscopePreset {
|
|
7
|
+
id: string;
|
|
8
|
+
/** 与 config.libraryImportSources 并集 */
|
|
9
|
+
libraryImportSources?: string[];
|
|
10
|
+
styleWhitelist?: readonly LibraryStyleWhitelistEntry[];
|
|
11
|
+
/** 组件名 → UX 角色(设计库维护表) */
|
|
12
|
+
componentRoles?: Record<string, UxComponentRole>;
|
|
13
|
+
configPatch?: Partial<UiscopeConfig>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type PresetModule = { default: UiscopePreset } | UiscopePreset;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { findMonorepoRoot } from '../paths/monorepo-root.js';
|
|
5
|
+
|
|
6
|
+
const OCPUI_MONOREPO_LIBRARY_PATHS = [
|
|
7
|
+
'packages/ocpui/core/vue',
|
|
8
|
+
'packages/core/vue',
|
|
9
|
+
] as const;
|
|
10
|
+
|
|
11
|
+
function toRelative(cwd: string, abs: string): string {
|
|
12
|
+
const rel = path.relative(cwd, abs);
|
|
13
|
+
return rel && !rel.startsWith('..') ? rel : abs;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function firstExistingDir(candidates: string[]): string | undefined {
|
|
17
|
+
for (const c of candidates) {
|
|
18
|
+
if (fs.existsSync(c) && fs.statSync(c).isDirectory()) return c;
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function resolveAutoLibraryRoots(cwd: string, presetIds: readonly string[]): string[] {
|
|
24
|
+
if (!presetIds.includes('ocpui')) return [];
|
|
25
|
+
|
|
26
|
+
const fromPkg: string[] = [];
|
|
27
|
+
try {
|
|
28
|
+
const require = createRequire(path.join(cwd, 'package.json'));
|
|
29
|
+
const vuePkg = require.resolve('@ocpui/vue/package.json');
|
|
30
|
+
const vueDir = path.dirname(vuePkg);
|
|
31
|
+
const hit = firstExistingDir([
|
|
32
|
+
path.join(vueDir, '../core/vue'),
|
|
33
|
+
path.join(vueDir, '../../core/vue'),
|
|
34
|
+
path.join(vueDir, 'node_modules/@ocpui/core/vue'),
|
|
35
|
+
]);
|
|
36
|
+
if (hit) fromPkg.push(toRelative(cwd, hit));
|
|
37
|
+
} catch {}
|
|
38
|
+
if (fromPkg.length) return fromPkg;
|
|
39
|
+
|
|
40
|
+
const mono = findMonorepoRoot(cwd);
|
|
41
|
+
for (const rel of OCPUI_MONOREPO_LIBRARY_PATHS) {
|
|
42
|
+
const abs = path.join(mono, rel);
|
|
43
|
+
if (fs.existsSync(abs) && fs.statSync(abs).isDirectory()) {
|
|
44
|
+
return [toRelative(cwd, abs)];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
4
|
+
import { createJiti } from 'jiti';
|
|
5
|
+
import type { UiscopeConfig } from '../types.js';
|
|
6
|
+
import { mergeConfig } from './load-config.js';
|
|
7
|
+
import type { UiscopePreset } from './preset-types.js';
|
|
8
|
+
import { registerStyleWhitelist } from '../manifest/library-style-whitelist.js';
|
|
9
|
+
import { rebuildLibraryStyleClassManifest } from '../manifest/library-style-classes.js';
|
|
10
|
+
import { registerComponentRoles } from '../manifest/library-ux-taxonomy.js';
|
|
11
|
+
import { resolveAutoLibraryRoots } from './resolve-preset-library-roots.js';
|
|
12
|
+
|
|
13
|
+
const CORE_PKG_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
|
14
|
+
|
|
15
|
+
function builtinPresetFile(presetId: string): string | undefined {
|
|
16
|
+
const file = path.join(CORE_PKG_ROOT, 'presets', presetId, 'index.ts');
|
|
17
|
+
return fs.existsSync(file) ? file : undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function resolvePresetModuleSpec(
|
|
21
|
+
presetId: string,
|
|
22
|
+
presetModules?: Record<string, string>,
|
|
23
|
+
): string {
|
|
24
|
+
if (presetModules?.[presetId]) return presetModules[presetId];
|
|
25
|
+
const builtin = builtinPresetFile(presetId);
|
|
26
|
+
if (builtin) return builtin;
|
|
27
|
+
if (presetId.startsWith('@') || presetId.includes('/')) return presetId;
|
|
28
|
+
return `@algotao/preset-${presetId}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function isFilesystemPresetSpec(spec: string): boolean {
|
|
32
|
+
return (
|
|
33
|
+
spec.startsWith('./')
|
|
34
|
+
|| spec.startsWith('../')
|
|
35
|
+
|| /^[A-Za-z]:[/\\]/.test(spec)
|
|
36
|
+
|| spec.startsWith('/')
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function loadPresetModule(spec: string, consumerCwd: string): Promise<unknown> {
|
|
41
|
+
const targets: string[] = [];
|
|
42
|
+
if (path.isAbsolute(spec) || isFilesystemPresetSpec(spec)) {
|
|
43
|
+
targets.push(path.isAbsolute(spec) ? spec : path.resolve(consumerCwd, spec));
|
|
44
|
+
} else {
|
|
45
|
+
targets.push(spec);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const jitiers = [createJiti(consumerCwd, { interopDefault: true }), createJiti(CORE_PKG_ROOT, { interopDefault: true })];
|
|
49
|
+
let lastError: unknown;
|
|
50
|
+
for (const target of targets) {
|
|
51
|
+
for (const jiti of jitiers) {
|
|
52
|
+
try {
|
|
53
|
+
return await jiti.import(target);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
lastError = e;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!isFilesystemPresetSpec(spec)) {
|
|
59
|
+
try {
|
|
60
|
+
return await import(target);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
lastError = e;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (isFilesystemPresetSpec(spec)) {
|
|
66
|
+
try {
|
|
67
|
+
const abs = path.resolve(consumerCwd, spec);
|
|
68
|
+
return await import(pathToFileURL(abs).href);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
lastError = e;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
throw lastError;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function resolvePreset(
|
|
78
|
+
cwd: string,
|
|
79
|
+
presetId: string,
|
|
80
|
+
presetModules?: Record<string, string>,
|
|
81
|
+
): Promise<UiscopePreset> {
|
|
82
|
+
const spec = resolvePresetModuleSpec(presetId, presetModules);
|
|
83
|
+
try {
|
|
84
|
+
const mod = (await loadPresetModule(spec, cwd)) as { default?: UiscopePreset } | UiscopePreset;
|
|
85
|
+
const preset =
|
|
86
|
+
mod && typeof mod === 'object' && 'default' in mod && mod.default
|
|
87
|
+
? mod.default
|
|
88
|
+
: (mod as UiscopePreset);
|
|
89
|
+
if (!preset?.id) {
|
|
90
|
+
throw new Error(`无效导出(需要 default 含 id)`);
|
|
91
|
+
}
|
|
92
|
+
return preset;
|
|
93
|
+
} catch (cause) {
|
|
94
|
+
const hint = presetId === spec ? spec : `${presetId} → ${spec}`;
|
|
95
|
+
throw new Error(
|
|
96
|
+
`[uiscope] 无法加载 preset(${hint})。请安装对应 npm 包,或在 uiscope.config 中配置 presetModules 映射。${
|
|
97
|
+
cause instanceof Error ? ` ${cause.message}` : ''
|
|
98
|
+
}`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function applyPresets(
|
|
104
|
+
cwd: string,
|
|
105
|
+
config: UiscopeConfig,
|
|
106
|
+
): Promise<UiscopeConfig> {
|
|
107
|
+
const ids = config.presets ?? [];
|
|
108
|
+
if (!ids.length) return config;
|
|
109
|
+
|
|
110
|
+
let merged = { ...config };
|
|
111
|
+
const extraSources: string[] = [];
|
|
112
|
+
for (const id of ids) {
|
|
113
|
+
const preset = await resolvePreset(cwd, id, config.presetModules);
|
|
114
|
+
if (preset.libraryImportSources?.length) {
|
|
115
|
+
extraSources.push(...preset.libraryImportSources);
|
|
116
|
+
}
|
|
117
|
+
if (preset.styleWhitelist?.length) {
|
|
118
|
+
registerStyleWhitelist(preset.styleWhitelist);
|
|
119
|
+
}
|
|
120
|
+
if (preset.componentRoles) {
|
|
121
|
+
registerComponentRoles(preset.componentRoles);
|
|
122
|
+
}
|
|
123
|
+
if (preset.configPatch) {
|
|
124
|
+
merged = mergeConfig(merged, preset.configPatch);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
rebuildLibraryStyleClassManifest();
|
|
129
|
+
|
|
130
|
+
let out = mergeConfig(merged, {
|
|
131
|
+
libraryImportSources: [...new Set([...(merged.libraryImportSources ?? []), ...extraSources])],
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
if (!(out.libraryRoots?.length)) {
|
|
135
|
+
const autoRoots = resolveAutoLibraryRoots(cwd, ids);
|
|
136
|
+
if (autoRoots.length) {
|
|
137
|
+
out = mergeConfig(out, { libraryRoots: autoRoots });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { UiscopeConfig } from '../types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 是否只扫描「消费设计库」的 Vue。
|
|
5
|
+
* - 配置显式设置时优先
|
|
6
|
+
* - 否则:未配置 libraryImportSources → 扫描 roots 下全部 Vue(零配置即可用)
|
|
7
|
+
* - 配置了 libraryImportSources → 默认收窄为消费方(可用 --all-files 放开)
|
|
8
|
+
*/
|
|
9
|
+
export function resolveOnlyLibraryConsumers(config: UiscopeConfig): boolean {
|
|
10
|
+
if (config.onlyLibraryConsumers !== undefined) return config.onlyLibraryConsumers;
|
|
11
|
+
return config.libraryImportSources.length > 0;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { UiscopeConfig, UiscopeThresholds } from '../types.js';
|
|
2
|
+
import { DEFAULT_THRESHOLDS, mergeThresholds } from './defaults.js';
|
|
3
|
+
|
|
4
|
+
export function resolveThresholds(config: UiscopeConfig): UiscopeThresholds {
|
|
5
|
+
let merged = mergeThresholds(DEFAULT_THRESHOLDS, config.thresholds);
|
|
6
|
+
if (config.overrideThreshold != null) {
|
|
7
|
+
merged = mergeThresholds(merged, {
|
|
8
|
+
style: { overrideRuleCount: config.overrideThreshold },
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
return merged;
|
|
12
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import type { FileComponentReport } from '../types.js';
|
|
3
|
+
import { treeHasComponentName } from './tree-walk.js';
|
|
4
|
+
|
|
5
|
+
export function normalizeDir(file: string): string {
|
|
6
|
+
return path.dirname(file.replace(/\\/g, '/'));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function reportsInSameDir(reports: FileComponentReport[], file: string): FileComponentReport[] {
|
|
10
|
+
const dir = normalizeDir(file);
|
|
11
|
+
return reports.filter((r) => normalizeDir(r.file) === dir);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function dirHasFeedbackCoverage(
|
|
15
|
+
reports: FileComponentReport[],
|
|
16
|
+
file: string,
|
|
17
|
+
feedbackTags: Set<string>,
|
|
18
|
+
): boolean {
|
|
19
|
+
const siblings = reportsInSameDir(reports, file);
|
|
20
|
+
for (const r of siblings) {
|
|
21
|
+
if (/Empty(State)?\.vue$/i.test(r.file)) return true;
|
|
22
|
+
if (r.tree && treeHasComponentName(r.tree, feedbackTags)) return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { UxEnhancementId, UxEnhancementOptionalId } from '../types.js';
|
|
2
|
+
import { runActionSemantics } from './rules/action-semantics.js';
|
|
3
|
+
import { runCoreUx } from './rules/core-ux.js';
|
|
4
|
+
import { runFacetHygiene } from './rules/facet-hygiene.js';
|
|
5
|
+
import { runFeedbackStates } from './rules/feedback-states.js';
|
|
6
|
+
import { runFormA11y } from './rules/form-a11y.js';
|
|
7
|
+
import { runHollowShell } from './rules/hollow-shell.js';
|
|
8
|
+
import { runI18nHints } from './rules/i18n-hints.js';
|
|
9
|
+
import { runListTemplating } from './rules/list-templating.js';
|
|
10
|
+
import { runPageRollup } from './rules/page-rollup.js';
|
|
11
|
+
import type { EnhanceContext, EnhancementRunner } from './types.js';
|
|
12
|
+
|
|
13
|
+
export interface EnhancementDef {
|
|
14
|
+
id: UxEnhancementId;
|
|
15
|
+
label: string;
|
|
16
|
+
description: string;
|
|
17
|
+
/** 随 facets / all / ux 默认执行 */
|
|
18
|
+
builtIn: boolean;
|
|
19
|
+
run: EnhancementRunner;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const ENHANCEMENT_REGISTRY: EnhancementDef[] = [
|
|
23
|
+
{
|
|
24
|
+
id: 'core-ux',
|
|
25
|
+
builtIn: true,
|
|
26
|
+
label: '核心 UX 契约',
|
|
27
|
+
description: '筛选条动作/输入、列表数据角色等基础完备性(固定扫描)',
|
|
28
|
+
run: runCoreUx,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: 'feedback-states',
|
|
32
|
+
builtIn: false,
|
|
33
|
+
label: '反馈态',
|
|
34
|
+
description: '列表/工作台是否具备 Empty、Spin、Skeleton(含同目录 *EmptyState)',
|
|
35
|
+
run: runFeedbackStates,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
id: 'hollow-shell',
|
|
39
|
+
builtIn: false,
|
|
40
|
+
label: '空壳容器',
|
|
41
|
+
description: '大子树无交互控件,疑多余包装或遗漏内容',
|
|
42
|
+
run: runHollowShell,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
id: 'page-rollup',
|
|
46
|
+
builtIn: false,
|
|
47
|
+
label: '目录级工作台',
|
|
48
|
+
description: '内置 Event/ExMarket/Product/Contamination 期望 + 配置 workbenchExpectations',
|
|
49
|
+
run: runPageRollup,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: 'facet-hygiene',
|
|
53
|
+
builtIn: false,
|
|
54
|
+
label: '切面卫生',
|
|
55
|
+
description: '同文件并列重复 facet kind',
|
|
56
|
+
run: runFacetHygiene,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
id: 'form-a11y',
|
|
60
|
+
builtIn: false,
|
|
61
|
+
label: '表单可访问性',
|
|
62
|
+
description: 'Input/FormItem 静态 label/aria/placeholder',
|
|
63
|
+
run: runFormA11y,
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: 'action-semantics',
|
|
67
|
+
builtIn: false,
|
|
68
|
+
label: '主操作语义',
|
|
69
|
+
description: '筛选区是否存在 primary/submit 主操作',
|
|
70
|
+
run: runActionSemantics,
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
id: 'list-templating',
|
|
74
|
+
builtIn: false,
|
|
75
|
+
label: '列表模板化',
|
|
76
|
+
description: 'v-for 与重复结构签名',
|
|
77
|
+
run: runListTemplating,
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: 'i18n-hints',
|
|
81
|
+
builtIn: false,
|
|
82
|
+
label: 'i18n 提示',
|
|
83
|
+
description: '模板属性硬编码中文',
|
|
84
|
+
run: runI18nHints,
|
|
85
|
+
},
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
export const OPTIONAL_ENHANCEMENT_IDS: UxEnhancementOptionalId[] = ENHANCEMENT_REGISTRY.filter(
|
|
89
|
+
(e) => !e.builtIn,
|
|
90
|
+
).map((e) => e.id as UxEnhancementOptionalId);
|
|
91
|
+
|
|
92
|
+
export function enhancementById(id: UxEnhancementId): EnhancementDef | undefined {
|
|
93
|
+
return ENHANCEMENT_REGISTRY.find((e) => e.id === id);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function runEnhancementBundle(ctx: EnhanceContext, ids: UxEnhancementId[]) {
|
|
97
|
+
const findings = [];
|
|
98
|
+
const seen = new Set<UxEnhancementId>();
|
|
99
|
+
for (const id of ids) {
|
|
100
|
+
if (seen.has(id)) continue;
|
|
101
|
+
seen.add(id);
|
|
102
|
+
const def = enhancementById(id);
|
|
103
|
+
if (!def) continue;
|
|
104
|
+
findings.push(...def.run(ctx));
|
|
105
|
+
}
|
|
106
|
+
return findings;
|
|
107
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { UxEnhancementId, UxEnhancementOptionalId } from '../types.js';
|
|
2
|
+
import { OPTIONAL_ENHANCEMENT_IDS } from './registry.js';
|
|
3
|
+
|
|
4
|
+
export type ScanModeWithUx = 'facets' | 'all' | 'ux';
|
|
5
|
+
|
|
6
|
+
export function modeSupportsEnhancements(mode: string): mode is ScanModeWithUx {
|
|
7
|
+
return mode === 'facets' || mode === 'all' || mode === 'ux';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ResolveEnhancementsInput {
|
|
11
|
+
mode: string;
|
|
12
|
+
cliEnhance?: UxEnhancementOptionalId[];
|
|
13
|
+
cliEnhanceAll?: boolean;
|
|
14
|
+
configDefault?: UxEnhancementOptionalId[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 单次扫描只解析一次;core-ux 在支持切面的 mode 下始终包含 */
|
|
18
|
+
export function resolveEnhancements(input: ResolveEnhancementsInput): UxEnhancementId[] {
|
|
19
|
+
if (!modeSupportsEnhancements(input.mode)) return [];
|
|
20
|
+
|
|
21
|
+
const optional = new Set<UxEnhancementOptionalId>();
|
|
22
|
+
if (input.cliEnhanceAll) {
|
|
23
|
+
for (const id of OPTIONAL_ENHANCEMENT_IDS) optional.add(id);
|
|
24
|
+
} else {
|
|
25
|
+
for (const id of input.configDefault ?? []) optional.add(id);
|
|
26
|
+
for (const id of input.cliEnhance ?? []) optional.add(id);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return ['core-ux', ...optional];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function parseEnhanceCliArgs(tokens: string[]): {
|
|
33
|
+
ids: UxEnhancementOptionalId[];
|
|
34
|
+
all: boolean;
|
|
35
|
+
invalid: string[];
|
|
36
|
+
} {
|
|
37
|
+
const ids: UxEnhancementOptionalId[] = [];
|
|
38
|
+
const invalid: string[] = [];
|
|
39
|
+
let all = false;
|
|
40
|
+
const allowed = new Set(OPTIONAL_ENHANCEMENT_IDS);
|
|
41
|
+
|
|
42
|
+
for (const raw of tokens) {
|
|
43
|
+
for (const part of raw.split(',').map((s) => s.trim()).filter(Boolean)) {
|
|
44
|
+
if (part === 'all') {
|
|
45
|
+
all = true;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (allowed.has(part as UxEnhancementOptionalId)) {
|
|
49
|
+
ids.push(part as UxEnhancementOptionalId);
|
|
50
|
+
} else {
|
|
51
|
+
invalid.push(part);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return { ids, all, invalid };
|
|
56
|
+
}
|