1
+
1
2
import { Component , State , ChildElement } from '../interfaces/InterfacesNew' ;
2
3
import HTMLTypes from '../context/HTMLTypes' ;
3
4
5
+
4
6
// generate code based on the component heirarchy
5
- const generateUnformattedCode = ( comps : Component [ ] , componentId : number ) => {
7
+ const generateUnformattedCode = (
8
+ comps : Component [ ] ,
9
+ componentId : number ,
10
+ rootComponents : number [ ] ,
11
+ projectType : string
12
+ ) => {
6
13
const components = [ ...comps ] ;
7
14
// find the component that we're going to generate code for
8
15
const currentComponent = components . find ( elem => elem . id === componentId ) ;
9
16
// find the unique components that we need to import into this component file
10
17
let imports : any = [ ] ;
18
+ let links : boolean = false ;
19
+
20
+ const isRoot = rootComponents . includes ( componentId ) ;
11
21
12
- // get metadata for each child
22
+ // get metadata for each child (e.g. the name/tag of the component/elemnt)
13
23
const getEnrichedChildren = ( currentComponent : Component | ChildElement ) => {
24
+ console . log ( 'enriching component ' , currentComponent ) ;
14
25
const enrichedChildren = currentComponent . children . map ( ( elem : any ) => {
15
26
const child = { ...elem } ;
16
27
if ( child . type === 'Component' ) {
@@ -28,14 +39,22 @@ const generateUnformattedCode = (comps: Component[], componentId: number) => {
28
39
child . children = getEnrichedChildren ( child ) ;
29
40
}
30
41
return child ;
42
+ } else if ( child . type === 'Route Link' ) {
43
+ links = true ;
44
+ child . name = components . find (
45
+ ( comp : Component ) => comp . id === child . typeId
46
+ ) . name ;
47
+ return child ;
31
48
}
32
49
} ) ;
33
50
return enrichedChildren ;
34
51
} ;
35
52
53
+ // write all code that will be under the "return" of the component
36
54
const writeNestedElements = ( enrichedChildren : any ) => {
37
55
return `${ enrichedChildren
38
56
. map ( ( child : any ) => {
57
+ console . log ( 'the child is ' , child ) ;
39
58
if ( child . type === 'Component' ) {
40
59
return `<${ child . name } ${ formatStyles ( child . style ) } />` ;
41
60
} else if ( child . type === 'HTML Element' ) {
@@ -79,6 +98,12 @@ const generateUnformattedCode = (comps: Component[], componentId: number) => {
79
98
return `<${ child . tag } ${ formatStyles ( child . style ) } ></${ child . tag } >` ;
80
99
}
81
100
}
101
+ // route links are only a next.js feature. if the user creates a rotue link and then switches projects, generate code for a normal link instead
102
+ else if ( child . type === 'Route Link' ) {
103
+ return projectType === 'Next.js'
104
+ ? `<Link href="/${ child . name } "><a>${ child . name } </a></Link>`
105
+ : `<a>${ child . name } </a>` ;
106
+ }
82
107
} )
83
108
. join ( '\n' ) } `;
84
109
} ;
@@ -96,19 +121,35 @@ const generateUnformattedCode = (comps: Component[], componentId: number) => {
96
121
const enrichedChildren : any = getEnrichedChildren ( currentComponent ) ;
97
122
98
123
console . log ( enrichedChildren ) ;
124
+ const next = true ;
125
+
126
+ // import statements differ between root (pages) and regular components (components)
127
+ const importsMapped =
128
+ projectType === 'Next.js'
129
+ ? imports
130
+ . map ( ( comp : string ) => {
131
+ return isRoot
132
+ ? `import ${ comp } from '../components/${ comp } '`
133
+ : `import ${ comp } from './${ comp } '` ;
134
+ } )
135
+ . join ( '\n' )
136
+ : imports
137
+ . map ( ( comp : string ) => {
138
+ return `import ${ comp } from './${ comp } .tsx'` ;
139
+ } )
140
+ . join ( '\n' ) ;
99
141
100
142
const stateful = true ;
101
143
const classBased = false ;
102
144
103
- return `
145
+ // create final component code. component code differs between classic react and next.js
146
+ // classic react code
147
+ if ( projectType !== 'Next.js' ) {
148
+ return `
104
149
${ stateful && ! classBased ? `import React, {useState} from 'react';` : '' }
105
150
${ classBased ? `import React, {Component} from 'react';` : '' }
106
151
${ ! stateful && ! classBased ? `import React from 'react';` : '' }
107
- ${ imports
108
- . map ( ( comp : string ) => {
109
- return `import ${ comp } from './${ comp } .tsx'` ;
110
- } )
111
- . join ( '\n' ) }
152
+ ${ importsMapped }
112
153
113
154
${
114
155
classBased
@@ -131,23 +172,48 @@ const generateUnformattedCode = (comps: Component[], componentId: number) => {
131
172
132
173
${ classBased ? `render(): JSX.Element {` : `` }
133
174
175
+ return (
176
+ <div className="${ currentComponent . name } " style={props.style}>
177
+ ${ writeNestedElements ( enrichedChildren ) }
178
+ </div>
179
+ );
180
+ }
181
+ ${ classBased ? `}` : `` }
182
+ export default ${ currentComponent . name } ;
183
+ ` ;
184
+ }
185
+ // next.js component code
186
+ else {
187
+ return `
188
+ import React, { useState } from 'react';
189
+ ${ importsMapped }
190
+ import Head from 'next/head'
191
+ ${ links ? `import Link from 'next/link'` : `` }
134
192
193
+ const ${ currentComponent . name } = (props) => {
194
+
195
+ const [value, setValue] = useState("INITIAL VALUE");
135
196
136
197
return (
198
+ <>
199
+ <Head>
200
+ <title>${ currentComponent . name } </title>
201
+ </Head>
137
202
<div className="${ currentComponent . name } " style={props.style}>
138
203
${ writeNestedElements ( enrichedChildren ) }
139
204
</div>
205
+ </>
140
206
);
141
207
}
142
- ${ classBased ? `}` : `` }
208
+
143
209
export default ${ currentComponent . name } ;
144
210
` ;
211
+ }
145
212
} ;
146
213
147
214
// formats code with prettier linter
148
215
const formatCode = ( code : string ) => {
149
216
return window . api . formatCode ( code ) ;
150
- // return code;
151
217
// return format(code, {
152
218
// singleQuote: true,
153
219
// trailingComma: 'es5',
@@ -158,10 +224,21 @@ const formatCode = (code: string) => {
158
224
} ;
159
225
160
226
// generate code based on component heirarchy and then return the rendered code
161
- const generateCode = ( components : Component [ ] , componentId : number ) => {
162
- return '' ;
163
- // const code = generateUnformattedCode(components, componentId);
164
- // return formatCode(code);
227
+ const generateCode = (
228
+ components : Component [ ] ,
229
+ componentId : number ,
230
+ rootComponents : number [ ] ,
231
+ projectType : string
232
+ ) => {
233
+ console . log ( 'generating next.js code' ) ;
234
+ // return ''
235
+ const code = generateUnformattedCode (
236
+ components ,
237
+ componentId ,
238
+ rootComponents ,
239
+ projectType
240
+ ) ;
241
+ return formatCode ( code ) ;
165
242
} ;
166
243
167
244
export default generateCode ;
0 commit comments