Skip to content

Commit 00d7d2b

Browse files
committed
currently converted 3 files to TypeScript. Need to test on a different computer to verify it is working
1 parent b0ea694 commit 00d7d2b

File tree

2 files changed

+63
-55
lines changed

2 files changed

+63
-55
lines changed

src/utils/componentRender.util.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
const componentRender = (component, data) => {
22
const {
3-
stateful,
4-
id,
5-
position,
6-
childrenArray,
7-
title,
8-
props,
3+
stateful, id, position, childrenArray, title, props,
94
} = component;
105

6+
// need to filter with reduce the import, copy from below
117
if (stateful) {
128
return `
139
import React, { Component } from 'react';
14-
import PropTypes from 'prop-types';
15-
${childrenArray.map(child => `import ${child.componentName} from './${child.componentName}.tsx'`).join('\n')}
10+
${childrenArray
11+
.map(child => `import ${child.componentName} from './${child.componentName}.tsx'`)
12+
.reduce((acc, child) => {
13+
if (!acc.includes(child)) {
14+
acc.push(child);
15+
return acc;
16+
}
17+
return acc;
18+
}, [])
19+
.join('\n')}
1620
1721
class ${title} extends Component {
1822
constructor(props) {
@@ -29,33 +33,34 @@ const componentRender = (component, data) => {
2933
}
3034
}
3135
32-
${title}.propTypes = {
33-
${props.map(p => `${p.key}: PropTypes.${p.type}${p.required ? '.isRequired' : ''},`).join('\n')}
34-
}
35-
3636
export default ${title};
3737
`;
3838
}
3939

4040
return `
4141
import React from 'react';
42-
import PropTypes from 'prop-types';
43-
${childrenArray.map(child => `import ${child.componentName} from './${child.componentName}.tsx'`).join('\n')}
42+
${childrenArray
43+
.map(child => `import ${child.componentName} from './${child.componentName}.tsx'`)
44+
.reduce((acc, child) => {
45+
if (!acc.includes(child)) {
46+
acc.push(child);
47+
return acc;
48+
}
49+
return acc;
50+
}, [])
51+
.join('\n')}
4452
4553
4654
type Props = {
47-
55+
${component.props.map(prop => `${prop.key}: ${prop.type}`).join('\n')}
4856
}
49-
const ${title} = props => (
57+
58+
const ${title} = (props: Props) => (
5059
<div>
5160
${childrenArray.map(child => `<${child.componentName}/>`).join('\n')}
5261
</div>
5362
);
5463
55-
${title}.propTypes = {
56-
${props.map(p => `${p.key}: PropTypes.${p.type}${p.required ? '.isRequired' : ''},`).join('\n')}
57-
}
58-
5964
export default ${title};
6065
`;
6166
};

src/utils/createApplication.util.js

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,49 @@
1010
import fs from 'fs';
1111
import { format } from 'prettier';
1212

13-
function createApplicationUtil({path, appName, genOption, repoUrl}) {
13+
function createIndexHtml() {
14+
const fileName = 'index.html';
15+
const data = `
16+
<!DOCTYPE html>
17+
<html lang="en">
18+
<head>
19+
<meta charset="UTF-8">
20+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
21+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
22+
<title>Document</title>
23+
</head>
24+
<body>
25+
<div id='root'></div>
26+
<script src='bundle.js'></script>
27+
</body>
28+
</html>
29+
`;
30+
fs.writeFile(fileName, format(data, {
31+
singleQuote: true,
32+
trailingComma: 'es5',
33+
bracketSpacing: true,
34+
jsxBracketSameLine: true,
35+
parser: 'babel',
36+
}),
37+
(err) => {
38+
if (err) {
39+
console.log('index.html error:', err.message);
40+
} else {
41+
console.log('index.html written successfully');
42+
}
43+
});
44+
}
45+
46+
async function createApplicationUtil({path, appName, genOption, repoUrl}) {
1447
if (genOption === 1) {
15-
return [
16-
function createIndexHtml() {
17-
const fileName = 'index.html';
18-
const data = `
19-
<!DOCTYPE html>
20-
<html lang="en">
21-
<head>
22-
<meta charset="UTF-8">
23-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
24-
<meta http-equiv="X-UA-Compatible" content="ie=edge">
25-
<title>Document</title>
26-
</head>
27-
<body>
28-
<div id='root'></div>
29-
<script src='bundle.js'></script>
30-
</body>
31-
</html>
32-
`;
33-
fs.writeFile(fileName, format(data, {
34-
singleQuote: true,
35-
trailingComma: 'es5',
36-
bracketSpacing: true,
37-
jsxBracketSameLine: true,
38-
parser: 'babel',
39-
}),
40-
(err) => {
41-
if (err) {
42-
console.log('index.html error:', err.message);
43-
} else {
44-
console.log('index.html written successfully');
45-
}
46-
});
47-
},
48-
];
48+
await createIndexHtml();
4949
}
5050
}
5151
export default createApplicationUtil;
5252

53+
54+
55+
5356
// async function createApplicationUtil({
5457
// path, appName, genOption, repoUrl,
5558
// }) {

0 commit comments

Comments
 (0)