/
fileContent.js
47 lines (34 loc) · 1.21 KB
/
fileContent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// @flow
//this function will return the component template to be populated in the project folder
function fileContent(component: Object): string {
let content: string = "";
const cache: Object = {};
content += "import React, { Component } from 'react';\n";
for (let i: number = 0; i < component.children.length; i++) {
if (!cache[component.children[i].title]) {
cache[component.children[i].title] = true;
content += `import ${component.children[i].title} from './${component.children[i].title}.jsx'\n`;
}
}
if(component.isStateful) {
content += `\nclass ${component.title} extends Component {`;
content += `\n constructor(props) {`;
content += `\n super(props)\n`;
content += ` }\n`;
}
else
content += `\nconst ${component.title} = props => {\n`;
content += ` render() {\n`;
content += ` return (\n`;
content += ` <div>\n`;
for (let i: number = 0; i < component.children.length; i++) {
content += ` <${component.children[i].title} />\n`;
}
content += ` </div>\n`;
content += ` );\n`;
content += ` };\n`;
content += `}\n`;
content += `export default ${component.title};`;
return content;
}
module.exports = fileContent;