@fragments-sdk/cli 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +106 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +4783 -0
- package/dist/bin.js.map +1 -0
- package/dist/chunk-4FDQSGKX.js +786 -0
- package/dist/chunk-4FDQSGKX.js.map +1 -0
- package/dist/chunk-7H2MMGYG.js +369 -0
- package/dist/chunk-7H2MMGYG.js.map +1 -0
- package/dist/chunk-BSCG3IP7.js +619 -0
- package/dist/chunk-BSCG3IP7.js.map +1 -0
- package/dist/chunk-LY2CFFPY.js +898 -0
- package/dist/chunk-LY2CFFPY.js.map +1 -0
- package/dist/chunk-MUZ6CM66.js +6636 -0
- package/dist/chunk-MUZ6CM66.js.map +1 -0
- package/dist/chunk-OAENNG3G.js +1489 -0
- package/dist/chunk-OAENNG3G.js.map +1 -0
- package/dist/chunk-XHNKNI6J.js +235 -0
- package/dist/chunk-XHNKNI6J.js.map +1 -0
- package/dist/core-DWKLGY4N.js +68 -0
- package/dist/core-DWKLGY4N.js.map +1 -0
- package/dist/generate-4LQNJ7SX.js +249 -0
- package/dist/generate-4LQNJ7SX.js.map +1 -0
- package/dist/index.d.ts +775 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/init-EMVI47QG.js +416 -0
- package/dist/init-EMVI47QG.js.map +1 -0
- package/dist/mcp-bin.d.ts +1 -0
- package/dist/mcp-bin.js +1117 -0
- package/dist/mcp-bin.js.map +1 -0
- package/dist/scan-4YPRF7FV.js +12 -0
- package/dist/scan-4YPRF7FV.js.map +1 -0
- package/dist/service-QSZMZJBJ.js +208 -0
- package/dist/service-QSZMZJBJ.js.map +1 -0
- package/dist/static-viewer-MIPGZ4Z7.js +12 -0
- package/dist/static-viewer-MIPGZ4Z7.js.map +1 -0
- package/dist/test-SQ5ZHXWU.js +1067 -0
- package/dist/test-SQ5ZHXWU.js.map +1 -0
- package/dist/tokens-HSGMYK64.js +173 -0
- package/dist/tokens-HSGMYK64.js.map +1 -0
- package/dist/viewer-YRF4SQE4.js +11101 -0
- package/dist/viewer-YRF4SQE4.js.map +1 -0
- package/package.json +107 -0
- package/src/ai.ts +266 -0
- package/src/analyze.ts +265 -0
- package/src/bin.ts +916 -0
- package/src/build.ts +248 -0
- package/src/commands/a11y.ts +302 -0
- package/src/commands/add.ts +313 -0
- package/src/commands/audit.ts +195 -0
- package/src/commands/baseline.ts +221 -0
- package/src/commands/build.ts +144 -0
- package/src/commands/compare.ts +337 -0
- package/src/commands/context.ts +107 -0
- package/src/commands/dev.ts +107 -0
- package/src/commands/enhance.ts +858 -0
- package/src/commands/generate.ts +391 -0
- package/src/commands/init.ts +531 -0
- package/src/commands/link/figma.ts +645 -0
- package/src/commands/link/index.ts +10 -0
- package/src/commands/link/storybook.ts +267 -0
- package/src/commands/list.ts +49 -0
- package/src/commands/metrics.ts +114 -0
- package/src/commands/reset.ts +242 -0
- package/src/commands/scan.ts +537 -0
- package/src/commands/storygen.ts +207 -0
- package/src/commands/tokens.ts +251 -0
- package/src/commands/validate.ts +93 -0
- package/src/commands/verify.ts +215 -0
- package/src/core/composition.test.ts +262 -0
- package/src/core/composition.ts +255 -0
- package/src/core/config.ts +84 -0
- package/src/core/constants.ts +111 -0
- package/src/core/context.ts +380 -0
- package/src/core/defineSegment.ts +137 -0
- package/src/core/discovery.ts +337 -0
- package/src/core/figma.ts +263 -0
- package/src/core/fragment-types.ts +214 -0
- package/src/core/generators/context.ts +389 -0
- package/src/core/generators/index.ts +23 -0
- package/src/core/generators/registry.ts +364 -0
- package/src/core/generators/typescript-extractor.ts +374 -0
- package/src/core/importAnalyzer.ts +217 -0
- package/src/core/index.ts +149 -0
- package/src/core/loader.ts +155 -0
- package/src/core/node.ts +63 -0
- package/src/core/parser.ts +551 -0
- package/src/core/previewLoader.ts +172 -0
- package/src/core/schema/fragment.schema.json +189 -0
- package/src/core/schema/registry.schema.json +137 -0
- package/src/core/schema.ts +182 -0
- package/src/core/storyAdapter.test.ts +571 -0
- package/src/core/storyAdapter.ts +761 -0
- package/src/core/token-types.ts +287 -0
- package/src/core/types.ts +754 -0
- package/src/diff.ts +323 -0
- package/src/index.ts +43 -0
- package/src/mcp/__tests__/projectFields.test.ts +130 -0
- package/src/mcp/bin.ts +36 -0
- package/src/mcp/index.ts +8 -0
- package/src/mcp/server.ts +1310 -0
- package/src/mcp/utils.ts +54 -0
- package/src/mcp-bin.ts +36 -0
- package/src/migrate/__tests__/argTypes/argTypes.test.ts +189 -0
- package/src/migrate/__tests__/args/args.test.ts +452 -0
- package/src/migrate/__tests__/meta/meta.test.ts +198 -0
- package/src/migrate/__tests__/stories/stories.test.ts +278 -0
- package/src/migrate/__tests__/utils/utils.test.ts +371 -0
- package/src/migrate/__tests__/values/values.test.ts +303 -0
- package/src/migrate/bin.ts +108 -0
- package/src/migrate/converter.ts +658 -0
- package/src/migrate/detect.ts +196 -0
- package/src/migrate/index.ts +45 -0
- package/src/migrate/migrate.ts +163 -0
- package/src/migrate/parser.ts +1136 -0
- package/src/migrate/report.ts +624 -0
- package/src/migrate/types.ts +169 -0
- package/src/screenshot.ts +249 -0
- package/src/service/__tests__/ast-utils.test.ts +426 -0
- package/src/service/__tests__/enhance-scanner.test.ts +200 -0
- package/src/service/__tests__/figma/figma.test.ts +652 -0
- package/src/service/__tests__/metrics-store.test.ts +409 -0
- package/src/service/__tests__/patch-generator.test.ts +186 -0
- package/src/service/__tests__/props-extractor.test.ts +365 -0
- package/src/service/__tests__/token-registry.test.ts +267 -0
- package/src/service/analytics.ts +659 -0
- package/src/service/ast-utils.ts +444 -0
- package/src/service/browser-pool.ts +339 -0
- package/src/service/capture.ts +267 -0
- package/src/service/diff.ts +279 -0
- package/src/service/enhance/aggregator.ts +489 -0
- package/src/service/enhance/cache.ts +275 -0
- package/src/service/enhance/codebase-scanner.ts +357 -0
- package/src/service/enhance/context-generator.ts +529 -0
- package/src/service/enhance/doc-extractor.ts +523 -0
- package/src/service/enhance/index.ts +131 -0
- package/src/service/enhance/props-extractor.ts +665 -0
- package/src/service/enhance/scanner.ts +445 -0
- package/src/service/enhance/storybook-parser.ts +552 -0
- package/src/service/enhance/types.ts +346 -0
- package/src/service/enhance/variant-renderer.ts +479 -0
- package/src/service/figma.ts +1008 -0
- package/src/service/index.ts +249 -0
- package/src/service/metrics-store.ts +333 -0
- package/src/service/patch-generator.ts +349 -0
- package/src/service/report.ts +854 -0
- package/src/service/storage.ts +401 -0
- package/src/service/token-fixes.ts +281 -0
- package/src/service/token-parser.ts +504 -0
- package/src/service/token-registry.ts +721 -0
- package/src/service/utils.ts +172 -0
- package/src/setup.ts +241 -0
- package/src/shared/command-wrapper.ts +81 -0
- package/src/shared/dev-server-client.ts +199 -0
- package/src/shared/index.ts +8 -0
- package/src/shared/segment-loader.ts +59 -0
- package/src/shared/types.ts +147 -0
- package/src/static-viewer.ts +715 -0
- package/src/test/discovery.ts +172 -0
- package/src/test/index.ts +281 -0
- package/src/test/reporters/console.ts +194 -0
- package/src/test/reporters/json.ts +190 -0
- package/src/test/reporters/junit.ts +186 -0
- package/src/test/runner.ts +598 -0
- package/src/test/types.ts +245 -0
- package/src/test/watch.ts +200 -0
- package/src/validators.ts +152 -0
- package/src/viewer/__tests__/jsx-parser.test.ts +502 -0
- package/src/viewer/__tests__/render-utils.test.ts +232 -0
- package/src/viewer/__tests__/style-utils.test.ts +404 -0
- package/src/viewer/bin.ts +86 -0
- package/src/viewer/cli/health.ts +256 -0
- package/src/viewer/cli/index.ts +33 -0
- package/src/viewer/cli/scan.ts +124 -0
- package/src/viewer/cli/utils.ts +174 -0
- package/src/viewer/components/AccessibilityPanel.tsx +1404 -0
- package/src/viewer/components/ActionCapture.tsx +172 -0
- package/src/viewer/components/ActionsPanel.tsx +371 -0
- package/src/viewer/components/App.tsx +638 -0
- package/src/viewer/components/BottomPanel.tsx +224 -0
- package/src/viewer/components/CodePanel.tsx +589 -0
- package/src/viewer/components/CommandPalette.tsx +336 -0
- package/src/viewer/components/ComponentGraph.tsx +394 -0
- package/src/viewer/components/ComponentHeader.tsx +85 -0
- package/src/viewer/components/ContractPanel.tsx +234 -0
- package/src/viewer/components/ErrorBoundary.tsx +85 -0
- package/src/viewer/components/FigmaEmbed.tsx +231 -0
- package/src/viewer/components/FragmentEditor.tsx +485 -0
- package/src/viewer/components/HealthDashboard.tsx +452 -0
- package/src/viewer/components/HmrStatusIndicator.tsx +71 -0
- package/src/viewer/components/Icons.tsx +417 -0
- package/src/viewer/components/InteractionsPanel.tsx +720 -0
- package/src/viewer/components/IsolatedPreviewFrame.tsx +321 -0
- package/src/viewer/components/IsolatedRender.tsx +111 -0
- package/src/viewer/components/KeyboardShortcutsHelp.tsx +89 -0
- package/src/viewer/components/LandingPage.tsx +441 -0
- package/src/viewer/components/Layout.tsx +22 -0
- package/src/viewer/components/LeftSidebar.tsx +391 -0
- package/src/viewer/components/MultiViewportPreview.tsx +429 -0
- package/src/viewer/components/PreviewArea.tsx +404 -0
- package/src/viewer/components/PreviewFrameHost.tsx +310 -0
- package/src/viewer/components/PreviewPane.tsx +150 -0
- package/src/viewer/components/PreviewToolbar.tsx +176 -0
- package/src/viewer/components/PropsEditor.tsx +512 -0
- package/src/viewer/components/PropsTable.tsx +98 -0
- package/src/viewer/components/RelationsSection.tsx +57 -0
- package/src/viewer/components/ResizablePanel.tsx +328 -0
- package/src/viewer/components/RightSidebar.tsx +118 -0
- package/src/viewer/components/ScreenshotButton.tsx +90 -0
- package/src/viewer/components/Sidebar.tsx +169 -0
- package/src/viewer/components/SkeletonLoader.tsx +156 -0
- package/src/viewer/components/StoryRenderer.tsx +128 -0
- package/src/viewer/components/ThemeProvider.tsx +96 -0
- package/src/viewer/components/Toast.tsx +67 -0
- package/src/viewer/components/TokenStylePanel.tsx +708 -0
- package/src/viewer/components/UsageSection.tsx +95 -0
- package/src/viewer/components/VariantMatrix.tsx +350 -0
- package/src/viewer/components/VariantRenderer.tsx +131 -0
- package/src/viewer/components/VariantTabs.tsx +84 -0
- package/src/viewer/components/ViewportSelector.tsx +165 -0
- package/src/viewer/components/_future/CreatePage.tsx +836 -0
- package/src/viewer/composition-renderer.ts +381 -0
- package/src/viewer/constants/index.ts +1 -0
- package/src/viewer/constants/ui.ts +185 -0
- package/src/viewer/entry.tsx +299 -0
- package/src/viewer/hooks/index.ts +2 -0
- package/src/viewer/hooks/useA11yCache.ts +383 -0
- package/src/viewer/hooks/useA11yService.ts +498 -0
- package/src/viewer/hooks/useActions.ts +138 -0
- package/src/viewer/hooks/useAppState.ts +124 -0
- package/src/viewer/hooks/useFigmaIntegration.ts +132 -0
- package/src/viewer/hooks/useHmrStatus.ts +109 -0
- package/src/viewer/hooks/useKeyboardShortcuts.ts +222 -0
- package/src/viewer/hooks/usePreviewBridge.ts +347 -0
- package/src/viewer/hooks/useScrollSpy.ts +78 -0
- package/src/viewer/hooks/useUrlState.ts +330 -0
- package/src/viewer/hooks/useViewSettings.ts +125 -0
- package/src/viewer/index.html +28 -0
- package/src/viewer/index.ts +14 -0
- package/src/viewer/intelligence/healthReport.ts +505 -0
- package/src/viewer/intelligence/styleDrift.ts +340 -0
- package/src/viewer/intelligence/usageScanner.ts +309 -0
- package/src/viewer/jsx-parser.ts +485 -0
- package/src/viewer/postcss.config.js +6 -0
- package/src/viewer/preview-frame-entry.tsx +25 -0
- package/src/viewer/preview-frame.html +109 -0
- package/src/viewer/render-template.html +68 -0
- package/src/viewer/render-utils.ts +170 -0
- package/src/viewer/server.ts +276 -0
- package/src/viewer/style-utils.ts +414 -0
- package/src/viewer/styles/globals.css +355 -0
- package/src/viewer/tailwind.config.js +37 -0
- package/src/viewer/types/a11y.ts +197 -0
- package/src/viewer/utils/a11y-fixes.ts +471 -0
- package/src/viewer/utils/actionExport.ts +372 -0
- package/src/viewer/utils/colorSchemes.ts +201 -0
- package/src/viewer/utils/detectRelationships.ts +256 -0
- package/src/viewer/vite-plugin.ts +2143 -0
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Health Report Generator
|
|
3
|
+
* Aggregates usage, drift, and documentation data into a comprehensive health report
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { SegmentDefinition } from '../../core/index.js';
|
|
7
|
+
import type { FullScanResult, UsageScanResult } from './usageScanner.js';
|
|
8
|
+
import type { DriftReport, DriftSummary, FullDriftResult } from './styleDrift.js';
|
|
9
|
+
import { aggregateDriftReports, getWorstOffenders } from './styleDrift.js';
|
|
10
|
+
|
|
11
|
+
export interface UsageStats {
|
|
12
|
+
totalComponents: number;
|
|
13
|
+
usedComponents: number;
|
|
14
|
+
unusedComponents: string[];
|
|
15
|
+
mostUsed: Array<{ component: string; count: number }>;
|
|
16
|
+
adoptionRate: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface StyleDriftStats {
|
|
20
|
+
componentsWithDrift: number;
|
|
21
|
+
totalDrifts: number;
|
|
22
|
+
averageCompliance: number;
|
|
23
|
+
byProperty: Record<string, number>;
|
|
24
|
+
bySeverity: {
|
|
25
|
+
high: number;
|
|
26
|
+
medium: number;
|
|
27
|
+
low: number;
|
|
28
|
+
};
|
|
29
|
+
worstOffenders: Array<{
|
|
30
|
+
component: string;
|
|
31
|
+
variant: string;
|
|
32
|
+
driftCount: number;
|
|
33
|
+
complianceScore: number;
|
|
34
|
+
}>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface DocumentationStats {
|
|
38
|
+
documented: number;
|
|
39
|
+
undocumented: string[];
|
|
40
|
+
documentationRate: number;
|
|
41
|
+
missingDescriptions: string[];
|
|
42
|
+
missingUsage: string[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface TokenStats {
|
|
46
|
+
complianceScore: number;
|
|
47
|
+
hardcodedValues: number;
|
|
48
|
+
tokenizedValues: number;
|
|
49
|
+
tokenizationRate: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface HealthReport {
|
|
53
|
+
generatedAt: string;
|
|
54
|
+
overallScore: number;
|
|
55
|
+
grades: {
|
|
56
|
+
usage: 'A' | 'B' | 'C' | 'D' | 'F';
|
|
57
|
+
drift: 'A' | 'B' | 'C' | 'D' | 'F';
|
|
58
|
+
documentation: 'A' | 'B' | 'C' | 'D' | 'F';
|
|
59
|
+
tokens: 'A' | 'B' | 'C' | 'D' | 'F';
|
|
60
|
+
};
|
|
61
|
+
usage: UsageStats;
|
|
62
|
+
styleDrift: StyleDriftStats;
|
|
63
|
+
documentation: DocumentationStats;
|
|
64
|
+
tokens: TokenStats;
|
|
65
|
+
recommendations: string[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface HealthReportOptions {
|
|
69
|
+
/** Segments to analyze */
|
|
70
|
+
segments: Array<{ path: string; segment: SegmentDefinition }>;
|
|
71
|
+
/** Usage scan results (optional) */
|
|
72
|
+
usageScan?: FullScanResult;
|
|
73
|
+
/** Drift reports (optional) */
|
|
74
|
+
driftReports?: DriftReport[];
|
|
75
|
+
/** Token compliance data (optional) */
|
|
76
|
+
tokenData?: {
|
|
77
|
+
complianceScore: number;
|
|
78
|
+
hardcodedCount: number;
|
|
79
|
+
tokenizedCount: number;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Convert a percentage to a letter grade
|
|
85
|
+
*/
|
|
86
|
+
function toGrade(score: number): 'A' | 'B' | 'C' | 'D' | 'F' {
|
|
87
|
+
if (score >= 90) return 'A';
|
|
88
|
+
if (score >= 80) return 'B';
|
|
89
|
+
if (score >= 70) return 'C';
|
|
90
|
+
if (score >= 60) return 'D';
|
|
91
|
+
return 'F';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Calculate usage statistics from scan results
|
|
96
|
+
*/
|
|
97
|
+
function calculateUsageStats(
|
|
98
|
+
segments: Array<{ path: string; segment: SegmentDefinition }>,
|
|
99
|
+
usageScan?: FullScanResult
|
|
100
|
+
): UsageStats {
|
|
101
|
+
const allComponentNames = segments.map((s) => s.segment.meta.name);
|
|
102
|
+
|
|
103
|
+
if (!usageScan) {
|
|
104
|
+
return {
|
|
105
|
+
totalComponents: allComponentNames.length,
|
|
106
|
+
usedComponents: 0,
|
|
107
|
+
unusedComponents: allComponentNames,
|
|
108
|
+
mostUsed: [],
|
|
109
|
+
adoptionRate: 0,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const usageMap = new Map(usageScan.results.map((r) => [r.component, r.totalUsages]));
|
|
114
|
+
|
|
115
|
+
const usedComponents = allComponentNames.filter((name) => usageMap.has(name) && usageMap.get(name)! > 0);
|
|
116
|
+
const unusedComponents = allComponentNames.filter((name) => !usageMap.has(name) || usageMap.get(name) === 0);
|
|
117
|
+
|
|
118
|
+
const mostUsed = usageScan.results
|
|
119
|
+
.filter((r) => allComponentNames.includes(r.component))
|
|
120
|
+
.slice(0, 10)
|
|
121
|
+
.map((r) => ({ component: r.component, count: r.totalUsages }));
|
|
122
|
+
|
|
123
|
+
const adoptionRate = allComponentNames.length > 0
|
|
124
|
+
? Math.round((usedComponents.length / allComponentNames.length) * 100)
|
|
125
|
+
: 0;
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
totalComponents: allComponentNames.length,
|
|
129
|
+
usedComponents: usedComponents.length,
|
|
130
|
+
unusedComponents,
|
|
131
|
+
mostUsed,
|
|
132
|
+
adoptionRate,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Calculate style drift statistics from drift reports
|
|
138
|
+
*/
|
|
139
|
+
function calculateDriftStats(driftReports?: DriftReport[]): StyleDriftStats {
|
|
140
|
+
if (!driftReports || driftReports.length === 0) {
|
|
141
|
+
return {
|
|
142
|
+
componentsWithDrift: 0,
|
|
143
|
+
totalDrifts: 0,
|
|
144
|
+
averageCompliance: 100,
|
|
145
|
+
byProperty: {},
|
|
146
|
+
bySeverity: { high: 0, medium: 0, low: 0 },
|
|
147
|
+
worstOffenders: [],
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const summary = aggregateDriftReports(driftReports);
|
|
152
|
+
const worstOffenders = getWorstOffenders(driftReports, 5);
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
componentsWithDrift: summary.componentsWithDrift,
|
|
156
|
+
totalDrifts: summary.totalDrifts,
|
|
157
|
+
averageCompliance: summary.averageCompliance,
|
|
158
|
+
byProperty: summary.byProperty,
|
|
159
|
+
bySeverity: summary.bySeverity,
|
|
160
|
+
worstOffenders: worstOffenders.map((r) => ({
|
|
161
|
+
component: r.component,
|
|
162
|
+
variant: r.variant,
|
|
163
|
+
driftCount: r.drifts.length,
|
|
164
|
+
complianceScore: r.complianceScore,
|
|
165
|
+
})),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Calculate documentation statistics from segments
|
|
171
|
+
*/
|
|
172
|
+
function calculateDocumentationStats(
|
|
173
|
+
segments: Array<{ path: string; segment: SegmentDefinition }>
|
|
174
|
+
): DocumentationStats {
|
|
175
|
+
const documented: string[] = [];
|
|
176
|
+
const undocumented: string[] = [];
|
|
177
|
+
const missingDescriptions: string[] = [];
|
|
178
|
+
const missingUsage: string[] = [];
|
|
179
|
+
|
|
180
|
+
for (const { segment } of segments) {
|
|
181
|
+
const name = segment.meta.name;
|
|
182
|
+
|
|
183
|
+
// Check for description
|
|
184
|
+
const hasDescription = segment.meta.description && segment.meta.description.trim().length > 10;
|
|
185
|
+
|
|
186
|
+
// Check for usage guidelines
|
|
187
|
+
const hasUsage = segment.usage && (segment.usage.when.length > 0 || segment.usage.whenNot.length > 0);
|
|
188
|
+
|
|
189
|
+
// Check for variants
|
|
190
|
+
const hasVariants = segment.variants && segment.variants.length > 0;
|
|
191
|
+
|
|
192
|
+
if (hasDescription && hasVariants) {
|
|
193
|
+
documented.push(name);
|
|
194
|
+
} else {
|
|
195
|
+
undocumented.push(name);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (!hasDescription) {
|
|
199
|
+
missingDescriptions.push(name);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (!hasUsage) {
|
|
203
|
+
missingUsage.push(name);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const documentationRate = segments.length > 0
|
|
208
|
+
? Math.round((documented.length / segments.length) * 100)
|
|
209
|
+
: 0;
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
documented: documented.length,
|
|
213
|
+
undocumented,
|
|
214
|
+
documentationRate,
|
|
215
|
+
missingDescriptions,
|
|
216
|
+
missingUsage,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Calculate token statistics
|
|
222
|
+
*/
|
|
223
|
+
function calculateTokenStats(tokenData?: {
|
|
224
|
+
complianceScore: number;
|
|
225
|
+
hardcodedCount: number;
|
|
226
|
+
tokenizedCount: number;
|
|
227
|
+
}): TokenStats {
|
|
228
|
+
if (!tokenData) {
|
|
229
|
+
return {
|
|
230
|
+
complianceScore: 100,
|
|
231
|
+
hardcodedValues: 0,
|
|
232
|
+
tokenizedValues: 0,
|
|
233
|
+
tokenizationRate: 100,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const total = tokenData.hardcodedCount + tokenData.tokenizedCount;
|
|
238
|
+
const tokenizationRate = total > 0
|
|
239
|
+
? Math.round((tokenData.tokenizedCount / total) * 100)
|
|
240
|
+
: 100;
|
|
241
|
+
|
|
242
|
+
return {
|
|
243
|
+
complianceScore: tokenData.complianceScore,
|
|
244
|
+
hardcodedValues: tokenData.hardcodedCount,
|
|
245
|
+
tokenizedValues: tokenData.tokenizedCount,
|
|
246
|
+
tokenizationRate,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Generate recommendations based on health data
|
|
252
|
+
*/
|
|
253
|
+
function generateRecommendations(
|
|
254
|
+
usage: UsageStats,
|
|
255
|
+
drift: StyleDriftStats,
|
|
256
|
+
documentation: DocumentationStats,
|
|
257
|
+
tokens: TokenStats
|
|
258
|
+
): string[] {
|
|
259
|
+
const recommendations: string[] = [];
|
|
260
|
+
|
|
261
|
+
// Usage recommendations
|
|
262
|
+
if (usage.unusedComponents.length > 0) {
|
|
263
|
+
if (usage.unusedComponents.length <= 3) {
|
|
264
|
+
recommendations.push(
|
|
265
|
+
`Consider removing or deprecating unused components: ${usage.unusedComponents.join(', ')}`
|
|
266
|
+
);
|
|
267
|
+
} else {
|
|
268
|
+
recommendations.push(
|
|
269
|
+
`${usage.unusedComponents.length} components are not being used. Run \`fragments scan\` for details.`
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (usage.adoptionRate < 50) {
|
|
275
|
+
recommendations.push(
|
|
276
|
+
'Low component adoption rate. Consider documenting migration paths from legacy components.'
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Drift recommendations
|
|
281
|
+
if (drift.bySeverity.high > 0) {
|
|
282
|
+
recommendations.push(
|
|
283
|
+
`${drift.bySeverity.high} high-severity style drift${drift.bySeverity.high > 1 ? 's' : ''} detected. These affect brand colors and typography.`
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (drift.averageCompliance < 80) {
|
|
288
|
+
recommendations.push(
|
|
289
|
+
`Style compliance is at ${drift.averageCompliance}%. Review the Styles panel for fix suggestions.`
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Documentation recommendations
|
|
294
|
+
if (documentation.missingDescriptions.length > 0) {
|
|
295
|
+
if (documentation.missingDescriptions.length <= 3) {
|
|
296
|
+
recommendations.push(
|
|
297
|
+
`Add descriptions to: ${documentation.missingDescriptions.join(', ')}`
|
|
298
|
+
);
|
|
299
|
+
} else {
|
|
300
|
+
recommendations.push(
|
|
301
|
+
`${documentation.missingDescriptions.length} components are missing descriptions.`
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (documentation.missingUsage.length > 0) {
|
|
307
|
+
recommendations.push(
|
|
308
|
+
`${documentation.missingUsage.length} components are missing usage guidelines (when/whenNot).`
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Token recommendations
|
|
313
|
+
if (tokens.hardcodedValues > 0) {
|
|
314
|
+
recommendations.push(
|
|
315
|
+
`${tokens.hardcodedValues} hardcoded value${tokens.hardcodedValues > 1 ? 's' : ''} should use design tokens.`
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (tokens.tokenizationRate < 80) {
|
|
320
|
+
recommendations.push(
|
|
321
|
+
'Token usage is low. Consider running token audits to identify migration opportunities.'
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return recommendations;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Calculate overall health score
|
|
330
|
+
*/
|
|
331
|
+
function calculateOverallScore(
|
|
332
|
+
usage: UsageStats,
|
|
333
|
+
drift: StyleDriftStats,
|
|
334
|
+
documentation: DocumentationStats,
|
|
335
|
+
tokens: TokenStats
|
|
336
|
+
): number {
|
|
337
|
+
// Weighted average of different metrics
|
|
338
|
+
const weights = {
|
|
339
|
+
adoption: 0.2,
|
|
340
|
+
compliance: 0.3,
|
|
341
|
+
documentation: 0.25,
|
|
342
|
+
tokens: 0.25,
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
const scores = {
|
|
346
|
+
adoption: usage.adoptionRate,
|
|
347
|
+
compliance: drift.averageCompliance,
|
|
348
|
+
documentation: documentation.documentationRate,
|
|
349
|
+
tokens: tokens.tokenizationRate,
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const weightedSum =
|
|
353
|
+
scores.adoption * weights.adoption +
|
|
354
|
+
scores.compliance * weights.compliance +
|
|
355
|
+
scores.documentation * weights.documentation +
|
|
356
|
+
scores.tokens * weights.tokens;
|
|
357
|
+
|
|
358
|
+
return Math.round(weightedSum);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Generate a comprehensive health report
|
|
363
|
+
*/
|
|
364
|
+
export function generateHealthReport(options: HealthReportOptions): HealthReport {
|
|
365
|
+
const { segments, usageScan, driftReports, tokenData } = options;
|
|
366
|
+
|
|
367
|
+
const usage = calculateUsageStats(segments, usageScan);
|
|
368
|
+
const styleDrift = calculateDriftStats(driftReports);
|
|
369
|
+
const documentation = calculateDocumentationStats(segments);
|
|
370
|
+
const tokens = calculateTokenStats(tokenData);
|
|
371
|
+
|
|
372
|
+
const overallScore = calculateOverallScore(usage, styleDrift, documentation, tokens);
|
|
373
|
+
const recommendations = generateRecommendations(usage, styleDrift, documentation, tokens);
|
|
374
|
+
|
|
375
|
+
return {
|
|
376
|
+
generatedAt: new Date().toISOString(),
|
|
377
|
+
overallScore,
|
|
378
|
+
grades: {
|
|
379
|
+
usage: toGrade(usage.adoptionRate),
|
|
380
|
+
drift: toGrade(styleDrift.averageCompliance),
|
|
381
|
+
documentation: toGrade(documentation.documentationRate),
|
|
382
|
+
tokens: toGrade(tokens.tokenizationRate),
|
|
383
|
+
},
|
|
384
|
+
usage,
|
|
385
|
+
styleDrift,
|
|
386
|
+
documentation,
|
|
387
|
+
tokens,
|
|
388
|
+
recommendations,
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Format health report as markdown
|
|
394
|
+
*/
|
|
395
|
+
export function formatHealthReportAsMarkdown(report: HealthReport): string {
|
|
396
|
+
const lines: string[] = [
|
|
397
|
+
'# Design System Health Report',
|
|
398
|
+
'',
|
|
399
|
+
`Generated: ${new Date(report.generatedAt).toLocaleString()}`,
|
|
400
|
+
'',
|
|
401
|
+
`## Overall Score: ${report.overallScore}%`,
|
|
402
|
+
'',
|
|
403
|
+
'| Category | Grade | Score |',
|
|
404
|
+
'|----------|-------|-------|',
|
|
405
|
+
`| Usage | ${report.grades.usage} | ${report.usage.adoptionRate}% |`,
|
|
406
|
+
`| Style Compliance | ${report.grades.drift} | ${report.styleDrift.averageCompliance}% |`,
|
|
407
|
+
`| Documentation | ${report.grades.documentation} | ${report.documentation.documentationRate}% |`,
|
|
408
|
+
`| Token Usage | ${report.grades.tokens} | ${report.tokens.tokenizationRate}% |`,
|
|
409
|
+
'',
|
|
410
|
+
];
|
|
411
|
+
|
|
412
|
+
// Usage section
|
|
413
|
+
lines.push('## Usage Statistics');
|
|
414
|
+
lines.push('');
|
|
415
|
+
lines.push(`- **Total Components**: ${report.usage.totalComponents}`);
|
|
416
|
+
lines.push(`- **Used Components**: ${report.usage.usedComponents}`);
|
|
417
|
+
lines.push(`- **Adoption Rate**: ${report.usage.adoptionRate}%`);
|
|
418
|
+
|
|
419
|
+
if (report.usage.mostUsed.length > 0) {
|
|
420
|
+
lines.push('');
|
|
421
|
+
lines.push('### Most Used Components');
|
|
422
|
+
lines.push('');
|
|
423
|
+
for (const item of report.usage.mostUsed.slice(0, 5)) {
|
|
424
|
+
lines.push(`- ${item.component}: ${item.count} usages`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (report.usage.unusedComponents.length > 0) {
|
|
429
|
+
lines.push('');
|
|
430
|
+
lines.push('### Unused Components');
|
|
431
|
+
lines.push('');
|
|
432
|
+
for (const name of report.usage.unusedComponents) {
|
|
433
|
+
lines.push(`- ${name}`);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Style Drift section
|
|
438
|
+
lines.push('');
|
|
439
|
+
lines.push('## Style Drift Analysis');
|
|
440
|
+
lines.push('');
|
|
441
|
+
lines.push(`- **Components with Drift**: ${report.styleDrift.componentsWithDrift}`);
|
|
442
|
+
lines.push(`- **Total Drifts**: ${report.styleDrift.totalDrifts}`);
|
|
443
|
+
lines.push(`- **Average Compliance**: ${report.styleDrift.averageCompliance}%`);
|
|
444
|
+
|
|
445
|
+
if (Object.keys(report.styleDrift.bySeverity).some((k) => report.styleDrift.bySeverity[k as keyof typeof report.styleDrift.bySeverity] > 0)) {
|
|
446
|
+
lines.push('');
|
|
447
|
+
lines.push('### Drift by Severity');
|
|
448
|
+
lines.push('');
|
|
449
|
+
lines.push(`- High: ${report.styleDrift.bySeverity.high}`);
|
|
450
|
+
lines.push(`- Medium: ${report.styleDrift.bySeverity.medium}`);
|
|
451
|
+
lines.push(`- Low: ${report.styleDrift.bySeverity.low}`);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
if (report.styleDrift.worstOffenders.length > 0) {
|
|
455
|
+
lines.push('');
|
|
456
|
+
lines.push('### Components Needing Attention');
|
|
457
|
+
lines.push('');
|
|
458
|
+
for (const item of report.styleDrift.worstOffenders) {
|
|
459
|
+
lines.push(`- **${item.component}** (${item.variant}): ${item.driftCount} drifts, ${item.complianceScore}% compliant`);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// Documentation section
|
|
464
|
+
lines.push('');
|
|
465
|
+
lines.push('## Documentation Coverage');
|
|
466
|
+
lines.push('');
|
|
467
|
+
lines.push(`- **Documented**: ${report.documentation.documented} components`);
|
|
468
|
+
lines.push(`- **Documentation Rate**: ${report.documentation.documentationRate}%`);
|
|
469
|
+
|
|
470
|
+
if (report.documentation.undocumented.length > 0) {
|
|
471
|
+
lines.push('');
|
|
472
|
+
lines.push('### Needs Documentation');
|
|
473
|
+
lines.push('');
|
|
474
|
+
for (const name of report.documentation.undocumented.slice(0, 10)) {
|
|
475
|
+
lines.push(`- ${name}`);
|
|
476
|
+
}
|
|
477
|
+
if (report.documentation.undocumented.length > 10) {
|
|
478
|
+
lines.push(`- ... and ${report.documentation.undocumented.length - 10} more`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Token section
|
|
483
|
+
lines.push('');
|
|
484
|
+
lines.push('## Design Token Usage');
|
|
485
|
+
lines.push('');
|
|
486
|
+
lines.push(`- **Token Compliance**: ${report.tokens.complianceScore}%`);
|
|
487
|
+
lines.push(`- **Tokenized Values**: ${report.tokens.tokenizedValues}`);
|
|
488
|
+
lines.push(`- **Hardcoded Values**: ${report.tokens.hardcodedValues}`);
|
|
489
|
+
|
|
490
|
+
// Recommendations
|
|
491
|
+
if (report.recommendations.length > 0) {
|
|
492
|
+
lines.push('');
|
|
493
|
+
lines.push('## Recommendations');
|
|
494
|
+
lines.push('');
|
|
495
|
+
for (const rec of report.recommendations) {
|
|
496
|
+
lines.push(`- ${rec}`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
lines.push('');
|
|
501
|
+
lines.push('---');
|
|
502
|
+
lines.push('*Generated by Fragments CLI*');
|
|
503
|
+
|
|
504
|
+
return lines.join('\n');
|
|
505
|
+
}
|