@@ -4,6 +4,80 @@ import { exportDevup, importDevup } from './commands/devup'
44import { exportAssets } from './commands/exportAssets'
55import { exportComponents } from './commands/exportComponents'
66
7+ export function extractImports (
8+ componentsCodes : ReadonlyArray < readonly [ string , string ] > ,
9+ ) : string [ ] {
10+ const allCode = componentsCodes . map ( ( [ _ , code ] ) => code ) . join ( '\n' )
11+ const imports = new Set < string > ( )
12+
13+ const devupComponents = [
14+ 'Center' ,
15+ 'VStack' ,
16+ 'Flex' ,
17+ 'Grid' ,
18+ 'Box' ,
19+ 'Text' ,
20+ 'Image' ,
21+ ]
22+
23+ for ( const component of devupComponents ) {
24+ const regex = new RegExp ( `<${ component } [\\s/>]` , 'g' )
25+ if ( regex . test ( allCode ) ) {
26+ imports . add ( component )
27+ }
28+ }
29+
30+ // keyframes 함수 체크
31+ if ( / k e y f r a m e s \s * \( | k e y f r a m e s ` / . test ( allCode ) ) {
32+ imports . add ( 'keyframes' )
33+ }
34+
35+ return Array . from ( imports ) . sort ( )
36+ }
37+
38+ function generateBashCLI (
39+ componentsCodes : ReadonlyArray < readonly [ string , string ] > ,
40+ ) : string {
41+ const imports = extractImports ( componentsCodes )
42+ const importStatement =
43+ imports . length > 0
44+ ? `import { ${ imports . join ( ', ' ) } } from '@devup-ui/react'\n\n`
45+ : ''
46+
47+ const commands = [
48+ 'mkdir -p src/components' ,
49+ '' ,
50+ ...componentsCodes . map ( ( [ componentName , code ] ) => {
51+ const fullCode = importStatement + code
52+ const escapedCode = fullCode . replace ( / ' / g, "\\'" )
53+ return `echo '${ escapedCode } ' > src/components/${ componentName } .tsx`
54+ } ) ,
55+ ]
56+
57+ return commands . join ( '\n' )
58+ }
59+
60+ function generatePowerShellCLI (
61+ componentsCodes : ReadonlyArray < readonly [ string , string ] > ,
62+ ) : string {
63+ const imports = extractImports ( componentsCodes )
64+ const importStatement =
65+ imports . length > 0
66+ ? `import { ${ imports . join ( ', ' ) } } from '@devup-ui/react'\n\n`
67+ : ''
68+
69+ const commands = [
70+ 'New-Item -ItemType Directory -Force -Path src\\components | Out-Null' ,
71+ '' ,
72+ ...componentsCodes . map ( ( [ componentName , code ] ) => {
73+ const fullCode = importStatement + code
74+ return `@'\n${ fullCode } \n'@ | Out-File -FilePath src\\components\\${ componentName } .tsx -Encoding UTF8`
75+ } ) ,
76+ ]
77+
78+ return commands . join ( '\n' )
79+ }
80+
781export function registerCodegen ( ctx : typeof figma ) {
882 if ( ctx . editorType === 'dev' && ctx . mode === 'codegen' ) {
983 ctx . codegen . on ( 'generate' , async ( { node, language } ) => {
@@ -15,7 +89,6 @@ export function registerCodegen(ctx: typeof figma) {
1589 const componentsCodes = codegen . getComponentsCodes ( )
1690 console . info ( `[benchmark] devup-ui end ${ Date . now ( ) - time } ms` )
1791
18- // 반응형 코드 생성 (부모가 Section인 경우)
1992 const parentSection = ResponsiveCodegen . hasParentSection ( node )
2093 let responsiveResult : {
2194 title : string
@@ -60,14 +133,14 @@ export function registerCodegen(ctx: typeof figma) {
60133 code : componentsCodes . map ( ( code ) => code [ 1 ] ) . join ( '\n\n' ) ,
61134 } ,
62135 {
63- title : `${ node . name } - Components CLI` ,
136+ title : `${ node . name } - Components CLI (Bash)` ,
137+ language : 'BASH' ,
138+ code : generateBashCLI ( componentsCodes ) ,
139+ } ,
140+ {
141+ title : `${ node . name } - Components CLI (PowerShell)` ,
64142 language : 'BASH' ,
65- code : componentsCodes
66- . map (
67- ( [ componentName , code ] ) =>
68- `echo '${ code } ' > ${ componentName } .tsx` ,
69- )
70- . join ( '\n' ) ,
143+ code : generatePowerShellCLI ( componentsCodes ) ,
71144 } ,
72145 ] as const )
73146 : [ ] ) ,
0 commit comments