@@ -5,23 +5,23 @@ import { exportAssets } from './commands/exportAssets'
55import { exportComponents } from './commands/exportComponents'
66import { getComponentName } from './utils'
77
8+ const DEVUP_COMPONENTS = [
9+ 'Center' ,
10+ 'VStack' ,
11+ 'Flex' ,
12+ 'Grid' ,
13+ 'Box' ,
14+ 'Text' ,
15+ 'Image' ,
16+ ]
17+
818export function extractImports (
919 componentsCodes : ReadonlyArray < readonly [ string , string ] > ,
1020) : string [ ] {
1121 const allCode = componentsCodes . map ( ( [ _ , code ] ) => code ) . join ( '\n' )
1222 const imports = new Set < string > ( )
1323
14- const devupComponents = [
15- 'Center' ,
16- 'VStack' ,
17- 'Flex' ,
18- 'Grid' ,
19- 'Box' ,
20- 'Text' ,
21- 'Image' ,
22- ]
23-
24- for ( const component of devupComponents ) {
24+ for ( const component of DEVUP_COMPONENTS ) {
2525 const regex = new RegExp ( `<${ component } [\\s/>]` , 'g' )
2626 if ( regex . test ( allCode ) ) {
2727 imports . add ( component )
@@ -35,14 +35,54 @@ export function extractImports(
3535 return Array . from ( imports ) . sort ( )
3636}
3737
38+ export function extractCustomComponentImports (
39+ componentsCodes : ReadonlyArray < readonly [ string , string ] > ,
40+ ) : string [ ] {
41+ console . log ( componentsCodes )
42+ const allCode = componentsCodes . map ( ( [ _ , code ] ) => code ) . join ( '\n' )
43+ const customImports = new Set < string > ( )
44+
45+ // Find all component usages in JSX: <ComponentName or <ComponentName>
46+ const componentUsageRegex = / < ( [ A - Z ] [ a - z A - Z 0 - 9 ] * ) / g
47+ const matches = allCode . matchAll ( componentUsageRegex )
48+ for ( const match of matches ) {
49+ const componentName = match [ 1 ]
50+ // Skip devup-ui components and components defined in this code
51+ if ( ! DEVUP_COMPONENTS . includes ( componentName ) ) {
52+ customImports . add ( componentName )
53+ }
54+ }
55+
56+ return Array . from ( customImports ) . sort ( )
57+ }
58+
59+ function generateImportStatements (
60+ componentsCodes : ReadonlyArray < readonly [ string , string ] > ,
61+ ) : string {
62+ const devupImports = extractImports ( componentsCodes )
63+ const customImports = extractCustomComponentImports ( componentsCodes )
64+
65+ const statements : string [ ] = [ ]
66+
67+ if ( devupImports . length > 0 ) {
68+ statements . push (
69+ `import { ${ devupImports . join ( ', ' ) } } from '@devup-ui/react'` ,
70+ )
71+ }
72+
73+ for ( const componentName of customImports ) {
74+ statements . push (
75+ `import { ${ componentName } } from '@/components/${ componentName } '` ,
76+ )
77+ }
78+
79+ return statements . length > 0 ? `${ statements . join ( '\n' ) } \n\n` : ''
80+ }
81+
3882function generateBashCLI (
3983 componentsCodes : ReadonlyArray < readonly [ string , string ] > ,
4084) : string {
41- const imports = extractImports ( componentsCodes )
42- const importStatement =
43- imports . length > 0
44- ? `import { ${ imports . join ( ', ' ) } } from '@devup-ui/react'\n\n`
45- : ''
85+ const importStatement = generateImportStatements ( componentsCodes )
4686
4787 const commands = [
4888 'mkdir -p src/components' ,
@@ -60,11 +100,7 @@ function generateBashCLI(
60100function generatePowerShellCLI (
61101 componentsCodes : ReadonlyArray < readonly [ string , string ] > ,
62102) : string {
63- const imports = extractImports ( componentsCodes )
64- const importStatement =
65- imports . length > 0
66- ? `import { ${ imports . join ( ', ' ) } } from '@devup-ui/react'\n\n`
67- : ''
103+ const importStatement = generateImportStatements ( componentsCodes )
68104
69105 const commands = [
70106 'New-Item -ItemType Directory -Force -Path src\\components | Out-Null' ,
0 commit comments