-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-react.js
More file actions
60 lines (47 loc) · 1.53 KB
/
create-react.js
File metadata and controls
60 lines (47 loc) · 1.53 KB
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
48
49
50
51
52
53
54
55
56
57
58
const fs = require('fs')
const inquirer = require('inquirer')
inquirer.prompt([
{ // prompts with list asking for class or functional (or cancel)
type: "list",
name: "method",
message: "What type of component?",
choices: ['class', 'functional', 'cancel']
},
{ // input name of component
type: "input",
name: "name",
message: "Name of component?"
}
]).then(res => {
if (res.name === '' || res.method === 'cancel') { // option to cancel component creation
console.log('cancelled')
return
}
// class component template
const classText = `import React, {Component} from 'react'
class ${res.name} extends Component {
constructor(props) {
super(props)
}
render() {
return (
<div>
</div>
)
}
}
export default ${res.name}`
// functional component template
const functionalText = `import React from 'react'
const ${res.name} = props =>
<div>
</div>
export default ${res.name}`
if (!fs.existsSync(res.name)) {
fs.mkdirSync(res.name) // creates folder
// creates index.js in folder with export default of component file
fs.writeFileSync(`${res.name}/index.js`, `export {default} from './${res.name}'`)
// creates component file in folder where template is chosen based on the method chosen in inquirer prompt
fs.writeFileSync(`${res.name}/${res.name}.js`, res.method === "class" ? classText: functionalText)
}
})