-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
159 lines (128 loc) · 5.04 KB
/
setup.js
File metadata and controls
159 lines (128 loc) · 5.04 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* This script performs various tasks that setup the template.
*
* It replaces instances of placeholder words, runs `npm i`, etc.
*
* To run this script, run `node setup.js` from the app template root directory.
*/
const { exec } = require('child_process')
const fs = require('fs')
const readline = require('readline')
const r1 = readline.createInterface({ input: process.stdin, output: process.stdout })
const capitalize = s=> `${s.charAt(0).toUpperCase()}${s.slice(1)}`
const dashCaseToCamelCase = s => s.replace(/-([a-z0-9])/g, (_, m) => `${m.toUpperCase()}`)
const VALIDATORS = {
isNonEmptyString: { op: s => s != null && s.length > 0, errMsg: 'Cannot be empty' },
hasNoSpaces: { op: s => s.indexOf(' ') === -1, errMsg: 'Cannot have whitespace' },
}
const tryGetInput = (question, onComplete, validators, defaultIfEmpty) => {
const _question = defaultIfEmpty != null ? `${question} [${defaultIfEmpty}]: ` : `${question}: `
r1.question(_question, name => {
const isEmpty = !VALIDATORS.isNonEmptyString.op(name)
if (isEmpty && defaultIfEmpty != null) {
onComplete(defaultIfEmpty)
}
else if (validators != null) {
const errMsgList = validators.reduce((acc, validator) => (validator.op(name) ? acc : acc.concat(validator.errorMsg)), [])
if (errMsgList.length === 0) {
onComplete(name)
}
else {
console.log('Error:', errMsgList.join(', '))
tryGetInput(question, onComplete, validators, defaultIfEmpty)
}
}
else {
onComplete(name)
}
})
}
const getDashCaseAppName = () => new Promise((res, rej) => {
tryGetInput('app-name', res, [VALIDATORS.hasNoSpaces], 'example-app')
})
const getCamelCaseAppName = (defaultValue) => new Promise((res, rej) => {
tryGetInput('appName', res, [], defaultValue ?? 'exampleApp')
})
const getNpmAppName = (defaultValue) => new Promise((res, rej) => {
tryGetInput('npm-app-name', res, [VALIDATORS.hasNoSpaces], defaultValue ?? 'example-app')
})
const getLicenseName = () => new Promise((res, rej) => {
tryGetInput('license-name', res, [], 'Joe Bloggs')
})
const getLicenseEmail = () => new Promise((res, rej) => {
tryGetInput('license-email', res, [VALIDATORS.hasNoSpaces], 'joebloggs@email.com')
})
const getGithubUserName = () => new Promise((res, rej) => {
tryGetInput('github-user-name', res, [VALIDATORS.hasNoSpaces], 'joebloggs')
})
const getAppSlogan = () => new Promise((res, rej) => {
tryGetInput('app-slogan', res, [], 'Delightful App')
})
const _replaceTokensInFiles = (filePaths, tokenMapEntries, i, onComplete) => {
if (i >= filePaths.length) {
onComplete()
return
}
const filePath = filePaths[i]
console.log(`--> ${filePath}`)
const fileText = fs.readFileSync(filePath, 'utf8')
let newFileText = fileText
tokenMapEntries.forEach(tokenMapEntry => {
newFileText = newFileText.replace(new RegExp(tokenMapEntry[0], 'g'), tokenMapEntry[1])
})
fs.writeFileSync(filePath, newFileText)
_replaceTokensInFiles(filePaths, tokenMapEntries, i + 1, onComplete)
}
const replaceTokensInFiles = (filePaths, tokenMap) => new Promise((res, rej) => {
console.log('\n==> Replacing some placeholder words in files...')
const tokenMapEntries = Object.entries(tokenMap)
_replaceTokensInFiles(filePaths, tokenMapEntries, 0, res)
})
const npmInstall = () => new Promise((res, rej) => {
console.log('==> Installing npm dependencies...')
exec('npm i', err => {
if (err != null)
console.log(err)
else
res()
})
})
const main = async () => {
const dashCaseAppName = await getDashCaseAppName()
const defaultCamelCaseAppName = dashCaseToCamelCase(dashCaseAppName)
const camelCaseAppName = await getCamelCaseAppName(defaultCamelCaseAppName)
const pascalCaseAppName = capitalize(camelCaseAppName)
const npmAppName = await getNpmAppName(dashCaseAppName)
const licenseName = await getLicenseName()
const licenseEmail = await getLicenseEmail()
const githubUserName = await getGithubUserName()
const appSlogan = await getAppSlogan()
const nonDoubleDashedTokenMap = {
'app-name': dashCaseAppName,
'appName': camelCaseAppName,
'AppName': pascalCaseAppName,
'npm-app-name': npmAppName,
'license-name': licenseName,
'license-email': licenseEmail,
'github-user-name': githubUserName,
'app-slogan': appSlogan,
}
console.log('Using token map:', JSON.stringify(nonDoubleDashedTokenMap, null, 2))
const doubleDashedTokenMap = {}
Object.keys(nonDoubleDashedTokenMap).forEach(token => doubleDashedTokenMap[`{{${token}}}`] = nonDoubleDashedTokenMap[token])
const filePathsWithDoubleDashTokens = [
'./README.md',
'./package.json',
'./LICENSE',
'./contributing/development.md',
]
const filePathsWithNonDoubleDashTokens = [
'./src/client/components/app.tsx'
]
await replaceTokensInFiles(filePathsWithDoubleDashTokens, doubleDashedTokenMap)
await replaceTokensInFiles(filePathsWithNonDoubleDashTokens, nonDoubleDashedTokenMap)
await npmInstall()
console.log('\nSetup complete! Run `npm start` to start the demo site. Happy hacking! :)')
r1.close()
}
main()