-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (87 loc) · 2.25 KB
/
index.js
File metadata and controls
95 lines (87 loc) · 2.25 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
let shell = require('shelljs')
let colors = require('colors')
let fs = require('fs')
let templates = require('./templates/templates.js')
let appName = process.argv[2]
let appDirectory = `${process.cwd()}/${appName}`
const run = async () => {
let success = await createReactApp()
if (!success) {
console.log(
'Something went wrong while trying to create a new React app using create-react-app'
.red
)
return false
}
await cdIntoNewApp()
await installPackages()
await updateTemplates()
console.log('All done')
}
const createReactApp = () => {
return new Promise((resolve) => {
if (appName) {
shell.exec(`npx create-react-app ${appName}`, (code) => {
console.log('Exited with code ', code)
console.log('Created react app')
resolve(true)
})
} else {
console.log('\nNo app name was provided.'.red)
console.log('\nProvide an app name in the following format: ')
console.log('\ncra-clean ', 'app-name\n'.cyan)
resolve(false)
}
})
}
const cdIntoNewApp = () => {
return new Promise((resolve) => {
shell.cd(appDirectory)
resolve()
})
}
const installPackages = () => {
return new Promise((resolve) => {
console.log(
'\nInstalling react-router, react-router-dom'
.cyan
)
shell.exec(
`npm install -D react-router react-router-dom`,
() => {
console.log('\nFinished installing packages\n'.green)
shell.cd('src');
shell.rm('logo.svg')
shell.mkdir('components')
shell.rm('App.css')
shell.touch('App.css')
shell.cd('..')
resolve()
}
)
})
}
const updateTemplates = () => {
return new Promise((resolve) => {
let promises = []
Object.keys(templates).forEach((fileName, i) => {
promises[i] = new Promise((res) => {
fs.writeFile(
`${appDirectory}/src/${fileName}`,
templates[fileName],
function (err) {
if (err) {
return console.log(err)
}
res()
}
)
})
})
Promise.all(promises).then(() => {
resolve()
})
})
}
run()