-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
executable file
·105 lines (92 loc) · 2.54 KB
/
index.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
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
96
97
98
99
100
101
102
103
104
105
#!/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(
`There was an error while trying to create your new React app using create-react-app!`
.red
)
return false
}
await cdIntoNewApp()
await installPackages()
await updateTemplates()
await auditFix()
console.log(`\nCongratulations, you are all done!\n cd => ${appName}\n`.cyan)
}
const createReactApp = () => {
return new Promise((resolve) => {
if (appName) {
shell.exec(`npx create-react-app ${appName}`, (code) => {
console.log('You new Lambda React app has been created!')
console.log(`Exit with code ${code}`)
resolve(true)
})
} else {
console.log('\nYou did not provide an app name!'.red)
console.log('\nProvide an app name using the following format: ')
console.log('\nlambda-react ', 'app-name\n'.cyan)
resolve(false)
}
})
}
const cdIntoNewApp = () => {
return new Promise((resolve) => {
// shell.cd(appDirectory)
shell.exec(`cd ${appName}`)
resolve()
})
}
const installPackages = () => {
return new Promise((resolve) => {
console.log('\nInstalling axios --> install axios dependency\n'.rainbow)
shell.exec(`cd ${appName} && npm install axios`, () => {
console.log('\nAll your packages have been installed!'.green)
resolve()
})
console.log('\nRemoving unused files\n'.cyan)
shell.exec(
`cd ${appName}/src && rm App.test.js logo.svg serviceWorker.js setupTests.js`,
() => {
console.log('\nUnused files have been removed!\n'.green)
}
)
})
}
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()
})
})
}
const auditFix = () => {
return new Promise((resolve) => {
shell.exec(`cd ${appName} && npm audit fix`, () => {
console.log('Ran npm audit fix\n'.brightGreen)
resolve()
})
})
}
run()