@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,531 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* fragments init - Smart interactive initialization
|
|
3
|
+
*
|
|
4
|
+
* Handles three scenarios:
|
|
5
|
+
* 1. Stories found → Configure and load existing stories
|
|
6
|
+
* 2. Components found (no stories) → Auto-generate documentation
|
|
7
|
+
* 3. Fresh project → Guided setup with example component
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readFile, writeFile, mkdir, access } from "node:fs/promises";
|
|
11
|
+
import { resolve, join, relative } from "node:path";
|
|
12
|
+
import { spawn } from "node:child_process";
|
|
13
|
+
import pc from "picocolors";
|
|
14
|
+
import { BRAND } from "../core/index.js";
|
|
15
|
+
import fg from "fast-glob";
|
|
16
|
+
import { input, confirm, select } from "@inquirer/prompts";
|
|
17
|
+
|
|
18
|
+
export interface InitOptions {
|
|
19
|
+
/** Project root directory */
|
|
20
|
+
projectRoot?: string;
|
|
21
|
+
/** Force overwrite existing config */
|
|
22
|
+
force?: boolean;
|
|
23
|
+
/** Non-interactive mode - auto-detect and use defaults */
|
|
24
|
+
yes?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface InitResult {
|
|
28
|
+
success: boolean;
|
|
29
|
+
configPath?: string;
|
|
30
|
+
scenario: "stories" | "components" | "fresh";
|
|
31
|
+
storiesFound: number;
|
|
32
|
+
componentsFound: number;
|
|
33
|
+
errors: string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface DetectionResult {
|
|
37
|
+
storyFiles: string[];
|
|
38
|
+
componentFiles: string[];
|
|
39
|
+
hasConfig: boolean;
|
|
40
|
+
configPath: string | null;
|
|
41
|
+
suggestedComponentPath: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Detect what exists in the project
|
|
46
|
+
*/
|
|
47
|
+
async function detectProject(projectRoot: string): Promise<DetectionResult> {
|
|
48
|
+
console.log(pc.dim("\nScanning project...\n"));
|
|
49
|
+
|
|
50
|
+
// Check for existing config
|
|
51
|
+
const configPath = join(projectRoot, BRAND.configFile);
|
|
52
|
+
const legacyConfigPath = join(projectRoot, BRAND.legacyConfigFile);
|
|
53
|
+
let hasConfig = false;
|
|
54
|
+
let foundConfigPath: string | null = null;
|
|
55
|
+
|
|
56
|
+
try {
|
|
57
|
+
await access(configPath);
|
|
58
|
+
hasConfig = true;
|
|
59
|
+
foundConfigPath = configPath;
|
|
60
|
+
} catch {
|
|
61
|
+
try {
|
|
62
|
+
await access(legacyConfigPath);
|
|
63
|
+
hasConfig = true;
|
|
64
|
+
foundConfigPath = legacyConfigPath;
|
|
65
|
+
} catch {
|
|
66
|
+
// No config
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Scan for story files
|
|
71
|
+
const storyFiles = await fg(
|
|
72
|
+
["**/*.stories.tsx", "**/*.stories.ts", "**/*.stories.jsx", "**/*.stories.js"],
|
|
73
|
+
{
|
|
74
|
+
cwd: projectRoot,
|
|
75
|
+
ignore: ["**/node_modules/**", "**/dist/**", "**/build/**", "**/.storybook/**"],
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
// Scan for component files (React components)
|
|
80
|
+
const componentFiles = await fg(
|
|
81
|
+
["**/components/**/*.tsx", "**/Components/**/*.tsx", "src/**/*.tsx"],
|
|
82
|
+
{
|
|
83
|
+
cwd: projectRoot,
|
|
84
|
+
ignore: [
|
|
85
|
+
"**/node_modules/**",
|
|
86
|
+
"**/dist/**",
|
|
87
|
+
"**/build/**",
|
|
88
|
+
"**/*.stories.*",
|
|
89
|
+
"**/*.test.*",
|
|
90
|
+
"**/*.spec.*",
|
|
91
|
+
"**/*.d.ts",
|
|
92
|
+
"**/index.tsx", // Skip barrel files
|
|
93
|
+
],
|
|
94
|
+
}
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
// Filter to likely component files (capitalized names, not utility files)
|
|
98
|
+
const likelyComponents = componentFiles.filter((f) => {
|
|
99
|
+
const fileName = f.split("/").pop() || "";
|
|
100
|
+
return /^[A-Z]/.test(fileName) && !fileName.includes(".styles");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// Suggest component path based on what we found
|
|
104
|
+
let suggestedComponentPath = "src/components";
|
|
105
|
+
if (likelyComponents.length > 0) {
|
|
106
|
+
const firstComponent = likelyComponents[0];
|
|
107
|
+
const parts = firstComponent.split("/");
|
|
108
|
+
const componentsIndex = parts.findIndex(
|
|
109
|
+
(p) => p.toLowerCase() === "components"
|
|
110
|
+
);
|
|
111
|
+
if (componentsIndex !== -1) {
|
|
112
|
+
suggestedComponentPath = parts.slice(0, componentsIndex + 1).join("/");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
storyFiles,
|
|
118
|
+
componentFiles: likelyComponents,
|
|
119
|
+
hasConfig,
|
|
120
|
+
configPath: foundConfigPath,
|
|
121
|
+
suggestedComponentPath,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Generate config file content
|
|
127
|
+
*/
|
|
128
|
+
function generateConfig(options: {
|
|
129
|
+
includePaths: string[];
|
|
130
|
+
componentPaths: string[];
|
|
131
|
+
framework: string;
|
|
132
|
+
}): string {
|
|
133
|
+
const includeStr = options.includePaths.map((p) => ` '${p}'`).join(",\n");
|
|
134
|
+
const componentStr = options.componentPaths.map((p) => ` '${p}'`).join(",\n");
|
|
135
|
+
|
|
136
|
+
return `import type { FragmentsConfig } from '@fragments/core';
|
|
137
|
+
|
|
138
|
+
const config: FragmentsConfig = {
|
|
139
|
+
// Glob patterns for finding fragment/story files
|
|
140
|
+
include: [
|
|
141
|
+
${includeStr}
|
|
142
|
+
],
|
|
143
|
+
|
|
144
|
+
// Glob patterns to exclude
|
|
145
|
+
exclude: ['**/node_modules/**'],
|
|
146
|
+
|
|
147
|
+
// Glob patterns for finding component files (for auto-documentation)
|
|
148
|
+
components: [
|
|
149
|
+
${componentStr}
|
|
150
|
+
],
|
|
151
|
+
|
|
152
|
+
// Framework (react, vue, svelte)
|
|
153
|
+
framework: '${options.framework}',
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export default config;
|
|
157
|
+
`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Generate example Button component
|
|
162
|
+
*/
|
|
163
|
+
function generateExampleComponent(): string {
|
|
164
|
+
return `import React from 'react';
|
|
165
|
+
|
|
166
|
+
export interface ButtonProps {
|
|
167
|
+
/** Button label */
|
|
168
|
+
children: React.ReactNode;
|
|
169
|
+
/** Visual style variant */
|
|
170
|
+
variant?: 'primary' | 'secondary' | 'ghost';
|
|
171
|
+
/** Button size */
|
|
172
|
+
size?: 'sm' | 'md' | 'lg';
|
|
173
|
+
/** Disabled state */
|
|
174
|
+
disabled?: boolean;
|
|
175
|
+
/** Click handler */
|
|
176
|
+
onClick?: () => void;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function Button({
|
|
180
|
+
children,
|
|
181
|
+
variant = 'primary',
|
|
182
|
+
size = 'md',
|
|
183
|
+
disabled = false,
|
|
184
|
+
onClick,
|
|
185
|
+
}: ButtonProps) {
|
|
186
|
+
const baseStyles = 'inline-flex items-center justify-center font-medium rounded-md transition-colors';
|
|
187
|
+
|
|
188
|
+
const variantStyles = {
|
|
189
|
+
primary: 'bg-blue-600 text-white hover:bg-blue-700',
|
|
190
|
+
secondary: 'bg-gray-200 text-gray-900 hover:bg-gray-300',
|
|
191
|
+
ghost: 'bg-transparent text-gray-700 hover:bg-gray-100',
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
const sizeStyles = {
|
|
195
|
+
sm: 'px-3 py-1.5 text-sm',
|
|
196
|
+
md: 'px-4 py-2 text-base',
|
|
197
|
+
lg: 'px-6 py-3 text-lg',
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
return (
|
|
201
|
+
<button
|
|
202
|
+
className={\`\${baseStyles} \${variantStyles[variant]} \${sizeStyles[size]}\`}
|
|
203
|
+
disabled={disabled}
|
|
204
|
+
onClick={onClick}
|
|
205
|
+
>
|
|
206
|
+
{children}
|
|
207
|
+
</button>
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
`;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Generate example Button fragment file
|
|
215
|
+
*/
|
|
216
|
+
function generateExampleFragment(): string {
|
|
217
|
+
return `import React from 'react';
|
|
218
|
+
import { defineFragment } from '@fragments/core';
|
|
219
|
+
import { Button } from './Button';
|
|
220
|
+
|
|
221
|
+
export default defineFragment({
|
|
222
|
+
component: Button,
|
|
223
|
+
|
|
224
|
+
meta: {
|
|
225
|
+
name: 'Button',
|
|
226
|
+
description: 'Interactive button for triggering actions',
|
|
227
|
+
category: 'Actions',
|
|
228
|
+
status: 'stable',
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
usage: {
|
|
232
|
+
when: [
|
|
233
|
+
'Triggering an action (save, submit, delete)',
|
|
234
|
+
'Form submission',
|
|
235
|
+
'Opening dialogs or menus',
|
|
236
|
+
],
|
|
237
|
+
whenNot: [
|
|
238
|
+
'Simple navigation (use Link)',
|
|
239
|
+
'Toggling state (use Switch)',
|
|
240
|
+
],
|
|
241
|
+
guidelines: [
|
|
242
|
+
'Use Primary for the main action in a context',
|
|
243
|
+
'Only one Primary button per section',
|
|
244
|
+
],
|
|
245
|
+
},
|
|
246
|
+
|
|
247
|
+
props: {
|
|
248
|
+
children: {
|
|
249
|
+
type: 'node',
|
|
250
|
+
required: true,
|
|
251
|
+
description: 'Button label content',
|
|
252
|
+
},
|
|
253
|
+
variant: {
|
|
254
|
+
type: 'enum',
|
|
255
|
+
values: ['primary', 'secondary', 'ghost'],
|
|
256
|
+
default: 'primary',
|
|
257
|
+
description: 'Visual style variant',
|
|
258
|
+
},
|
|
259
|
+
size: {
|
|
260
|
+
type: 'enum',
|
|
261
|
+
values: ['sm', 'md', 'lg'],
|
|
262
|
+
default: 'md',
|
|
263
|
+
description: 'Button size',
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
|
|
267
|
+
variants: [
|
|
268
|
+
{
|
|
269
|
+
name: 'Primary',
|
|
270
|
+
description: 'Default action button',
|
|
271
|
+
render: () => <Button variant="primary">Save Changes</Button>,
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
name: 'Secondary',
|
|
275
|
+
description: 'Less prominent action',
|
|
276
|
+
render: () => <Button variant="secondary">Cancel</Button>,
|
|
277
|
+
},
|
|
278
|
+
{
|
|
279
|
+
name: 'Ghost',
|
|
280
|
+
description: 'Minimal visual weight',
|
|
281
|
+
render: () => <Button variant="ghost">Learn More</Button>,
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
name: 'Sizes',
|
|
285
|
+
description: 'Available size options',
|
|
286
|
+
render: () => (
|
|
287
|
+
<div style={{ display: 'flex', gap: '8px', alignItems: 'center' }}>
|
|
288
|
+
<Button size="sm">Small</Button>
|
|
289
|
+
<Button size="md">Medium</Button>
|
|
290
|
+
<Button size="lg">Large</Button>
|
|
291
|
+
</div>
|
|
292
|
+
),
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
});
|
|
296
|
+
`;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Start the dev server
|
|
301
|
+
*/
|
|
302
|
+
function startDevServer(projectRoot: string): void {
|
|
303
|
+
console.log(pc.dim("\nStarting development server...\n"));
|
|
304
|
+
|
|
305
|
+
// Use process.platform to determine the correct command
|
|
306
|
+
const isWindows = process.platform === "win32";
|
|
307
|
+
const cmd = isWindows ? "npx.cmd" : "npx";
|
|
308
|
+
|
|
309
|
+
const child = spawn(cmd, [BRAND.cliCommand, "dev"], {
|
|
310
|
+
cwd: projectRoot,
|
|
311
|
+
stdio: "inherit",
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
child.on("error", (err) => {
|
|
315
|
+
console.error(pc.red("Failed to start dev server:"), err.message);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Main init function - smart and interactive by default
|
|
321
|
+
*/
|
|
322
|
+
export async function init(options: InitOptions = {}): Promise<InitResult> {
|
|
323
|
+
const projectRoot = resolve(options.projectRoot || process.cwd());
|
|
324
|
+
const errors: string[] = [];
|
|
325
|
+
|
|
326
|
+
console.log(pc.cyan(`\n✨ Welcome to ${BRAND.name}!\n`));
|
|
327
|
+
|
|
328
|
+
// Step 1: Detect what exists
|
|
329
|
+
const detection = await detectProject(projectRoot);
|
|
330
|
+
|
|
331
|
+
// Check for existing config
|
|
332
|
+
if (detection.hasConfig && !options.force) {
|
|
333
|
+
console.log(pc.yellow(`⚠ Config already exists: ${BRAND.configFile}`));
|
|
334
|
+
|
|
335
|
+
if (!options.yes) {
|
|
336
|
+
const overwrite = await confirm({
|
|
337
|
+
message: "Do you want to reinitialize? (This will overwrite your config)",
|
|
338
|
+
default: false,
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
if (!overwrite) {
|
|
342
|
+
console.log(pc.dim(`\nKeeping existing configuration. Run \`${BRAND.cliCommand} dev\` to start.\n`));
|
|
343
|
+
return {
|
|
344
|
+
success: true,
|
|
345
|
+
scenario: "stories",
|
|
346
|
+
storiesFound: detection.storyFiles.length,
|
|
347
|
+
componentsFound: detection.componentFiles.length,
|
|
348
|
+
errors: [],
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Step 2: Determine scenario and show what we found
|
|
355
|
+
let scenario: "stories" | "components" | "fresh";
|
|
356
|
+
|
|
357
|
+
if (detection.storyFiles.length > 0) {
|
|
358
|
+
scenario = "stories";
|
|
359
|
+
console.log(pc.green(`✓ Found ${detection.storyFiles.length} Storybook story file(s)`));
|
|
360
|
+
console.log(pc.dim(` ${detection.storyFiles.slice(0, 3).join("\n ")}`));
|
|
361
|
+
if (detection.storyFiles.length > 3) {
|
|
362
|
+
console.log(pc.dim(` ... and ${detection.storyFiles.length - 3} more`));
|
|
363
|
+
}
|
|
364
|
+
console.log();
|
|
365
|
+
console.log(
|
|
366
|
+
pc.cyan("Great news! ") +
|
|
367
|
+
"Fragments can load your existing stories automatically."
|
|
368
|
+
);
|
|
369
|
+
} else if (detection.componentFiles.length > 0) {
|
|
370
|
+
scenario = "components";
|
|
371
|
+
console.log(pc.green(`✓ Found ${detection.componentFiles.length} component file(s)`));
|
|
372
|
+
console.log(pc.dim(` ${detection.componentFiles.slice(0, 3).join("\n ")}`));
|
|
373
|
+
if (detection.componentFiles.length > 3) {
|
|
374
|
+
console.log(pc.dim(` ... and ${detection.componentFiles.length - 3} more`));
|
|
375
|
+
}
|
|
376
|
+
console.log();
|
|
377
|
+
console.log(
|
|
378
|
+
pc.cyan("No stories found, but that's fine! ") +
|
|
379
|
+
"Fragments can auto-generate documentation from your TypeScript."
|
|
380
|
+
);
|
|
381
|
+
} else {
|
|
382
|
+
scenario = "fresh";
|
|
383
|
+
console.log(pc.yellow("No components or stories found."));
|
|
384
|
+
console.log();
|
|
385
|
+
console.log(pc.cyan("Let's create your first fragment!"));
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
console.log();
|
|
389
|
+
|
|
390
|
+
// Step 3: Gather configuration (interactive unless --yes)
|
|
391
|
+
let componentPath = detection.suggestedComponentPath;
|
|
392
|
+
let runScan = scenario === "components";
|
|
393
|
+
let createExample = scenario === "fresh";
|
|
394
|
+
let startServer = true;
|
|
395
|
+
|
|
396
|
+
if (!options.yes) {
|
|
397
|
+
// Ask about component location
|
|
398
|
+
componentPath = await input({
|
|
399
|
+
message: "Where are your components located?",
|
|
400
|
+
default: detection.suggestedComponentPath,
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
if (scenario === "components") {
|
|
404
|
+
// For component-only projects, ask about scanning
|
|
405
|
+
runScan = await confirm({
|
|
406
|
+
message: "Auto-generate documentation from TypeScript?",
|
|
407
|
+
default: true,
|
|
408
|
+
});
|
|
409
|
+
} else {
|
|
410
|
+
// Fresh project - ask about example
|
|
411
|
+
createExample = await confirm({
|
|
412
|
+
message: "Create an example Button component to get started?",
|
|
413
|
+
default: true,
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Ask about starting the server
|
|
418
|
+
startServer = await confirm({
|
|
419
|
+
message: "Start the viewer now?",
|
|
420
|
+
default: true,
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// Step 4: Create configuration
|
|
425
|
+
console.log(pc.dim("\nCreating configuration...\n"));
|
|
426
|
+
|
|
427
|
+
// Build include patterns
|
|
428
|
+
// Note: Stories are loaded separately by the viewer, not via include patterns
|
|
429
|
+
const includePaths: string[] = [
|
|
430
|
+
`${componentPath}/**/*.fragment.tsx`,
|
|
431
|
+
`${componentPath}/**/*.segment.tsx`, // Legacy support
|
|
432
|
+
];
|
|
433
|
+
|
|
434
|
+
// Create config file
|
|
435
|
+
const configPath = join(projectRoot, BRAND.configFile);
|
|
436
|
+
const configContent = generateConfig({
|
|
437
|
+
includePaths,
|
|
438
|
+
componentPaths: [`${componentPath}/**/*.tsx`],
|
|
439
|
+
framework: "react",
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
try {
|
|
443
|
+
await writeFile(configPath, configContent, "utf-8");
|
|
444
|
+
console.log(pc.green(`✓ Created ${BRAND.configFile}`));
|
|
445
|
+
} catch (e) {
|
|
446
|
+
errors.push(`Failed to create config: ${e}`);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Step 5: Handle scenario-specific setup
|
|
450
|
+
if (scenario === "fresh" && createExample) {
|
|
451
|
+
// Create example component
|
|
452
|
+
const exampleDir = join(projectRoot, componentPath, "Button");
|
|
453
|
+
|
|
454
|
+
try {
|
|
455
|
+
await mkdir(exampleDir, { recursive: true });
|
|
456
|
+
|
|
457
|
+
// Write Button.tsx
|
|
458
|
+
await writeFile(
|
|
459
|
+
join(exampleDir, "Button.tsx"),
|
|
460
|
+
generateExampleComponent(),
|
|
461
|
+
"utf-8"
|
|
462
|
+
);
|
|
463
|
+
console.log(
|
|
464
|
+
pc.green(`✓ Created ${relative(projectRoot, join(exampleDir, "Button.tsx"))}`)
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
// Write Button.fragment.tsx
|
|
468
|
+
await writeFile(
|
|
469
|
+
join(exampleDir, "Button.fragment.tsx"),
|
|
470
|
+
generateExampleFragment(),
|
|
471
|
+
"utf-8"
|
|
472
|
+
);
|
|
473
|
+
console.log(
|
|
474
|
+
pc.green(
|
|
475
|
+
`✓ Created ${relative(projectRoot, join(exampleDir, "Button.fragment.tsx"))}`
|
|
476
|
+
)
|
|
477
|
+
);
|
|
478
|
+
} catch (e) {
|
|
479
|
+
errors.push(`Failed to create example component: ${e}`);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (scenario === "components" && runScan) {
|
|
484
|
+
// Run scan to generate fragments.json
|
|
485
|
+
console.log(pc.dim("\nGenerating documentation from source code...\n"));
|
|
486
|
+
try {
|
|
487
|
+
const { scan } = await import("./scan.js");
|
|
488
|
+
await scan({
|
|
489
|
+
config: configPath,
|
|
490
|
+
verbose: false,
|
|
491
|
+
});
|
|
492
|
+
} catch (e) {
|
|
493
|
+
console.log(
|
|
494
|
+
pc.yellow(`Note: Auto-documentation will run when you start the dev server.`)
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// Step 6: Show next steps or start server
|
|
500
|
+
if (errors.length === 0) {
|
|
501
|
+
console.log(pc.green("\n✓ Setup complete!\n"));
|
|
502
|
+
|
|
503
|
+
if (startServer) {
|
|
504
|
+
const serverMessage =
|
|
505
|
+
scenario === "stories"
|
|
506
|
+
? `Your ${detection.storyFiles.length} stories are loading...`
|
|
507
|
+
: scenario === "components"
|
|
508
|
+
? `Your ${detection.componentFiles.length} components are being documented...`
|
|
509
|
+
: `Your first component is ready!`;
|
|
510
|
+
|
|
511
|
+
console.log(pc.cyan(serverMessage));
|
|
512
|
+
startDevServer(projectRoot);
|
|
513
|
+
} else {
|
|
514
|
+
console.log(pc.cyan("Next steps:"));
|
|
515
|
+
console.log(` 1. Run ${pc.bold(`${BRAND.cliCommand} dev`)} to start the viewer`);
|
|
516
|
+
if (scenario === "fresh") {
|
|
517
|
+
console.log(` 2. Edit ${pc.bold(`${componentPath}/Button/Button.fragment.tsx`)}`);
|
|
518
|
+
}
|
|
519
|
+
console.log();
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
return {
|
|
524
|
+
success: errors.length === 0,
|
|
525
|
+
configPath: errors.length === 0 ? configPath : undefined,
|
|
526
|
+
scenario,
|
|
527
|
+
storiesFound: detection.storyFiles.length,
|
|
528
|
+
componentsFound: detection.componentFiles.length,
|
|
529
|
+
errors,
|
|
530
|
+
};
|
|
531
|
+
}
|