-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
192 lines (159 loc) · 6.47 KB
/
setup.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* 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 package 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 getDashCasePackageName = () => new Promise((res, rej) => {
tryGetInput('package-name', res, [VALIDATORS.hasNoSpaces], 'example-package')
})
const getCamelCasePackageName = (defaultValue) => new Promise((res, rej) => {
tryGetInput('packageName', res, [], defaultValue ?? 'examplePackage')
})
const getNpmPackageName = (defaultValue) => new Promise((res, rej) => {
tryGetInput('npm-package-name', res, [VALIDATORS.hasNoSpaces], defaultValue ?? 'example-package')
})
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 getPackageSlogan = () => new Promise((res, rej) => {
tryGetInput('package-slogan', res, [], 'Delightful Typescript Package')
})
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 renameDirsAndFiles = (
dashCasePackageName,
camelCasePackageName,
) => {
console.log('\n==> Renaming some directories and files...')
const paths = [
'./src/package-name',
'./src/demo/client/components/showcase/packageName.tsx',
'./src/demo/client/components/common/generic/packageName.tsx',
]
console.log(`--> ${paths[0]}`)
fs.renameSync(paths[0], `./src/${dashCasePackageName}`)
console.log(`--> ${paths[1]}`)
fs.renameSync(paths[1], `./src/demo/client/components/showcase/${camelCasePackageName}.tsx`)
console.log(`--> ${paths[2]}`)
fs.renameSync(paths[2], `./src/demo/client/components/common/generic/${camelCasePackageName}.tsx`)
}
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 dashCasePackageName = await getDashCasePackageName()
const defaultCamelCasePackageName = dashCaseToCamelCase(dashCasePackageName)
const camelCasePackageName = await getCamelCasePackageName(defaultCamelCasePackageName)
const pascalCasePackageName = capitalize(camelCasePackageName)
const npmPackageName = await getNpmPackageName(dashCasePackageName)
const licenseName = await getLicenseName()
const licenseEmail = await getLicenseEmail()
const githubUserName = await getGithubUserName()
const packageSlogan = await getPackageSlogan()
const nonDoubleDashedTokenMap = {
'package-name': dashCasePackageName,
'packageName': camelCasePackageName,
'PackageName': pascalCasePackageName,
'npm-package-name': npmPackageName,
'license-name': licenseName,
'license-email': licenseEmail,
'github-user-name': githubUserName,
'package-slogan': packageSlogan,
}
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/index.ts',
'./src/types.ts',
'./src/package-name/types.ts',
'./src/package-name/index.ts',
'./src/package-name/styles/index.scss',
'./src/package-name/index.spec.ts',
'./src/demo/client/components/showcase/index.tsx',
'./src/demo/client/components/showcase/packageName.tsx',
'./src/demo/client/components/common/generic/packageName.tsx',
'./src/demo/client/components/header/index.tsx',
'./src/demo/client/components/body.tsx',
'./src/demo/client/components/app.tsx',
'./buildScripts/buildComponentStyles.ts',
'./buildScripts/watchClient.ts',
]
await replaceTokensInFiles(filePathsWithDoubleDashTokens, doubleDashedTokenMap)
await replaceTokensInFiles(filePathsWithNonDoubleDashTokens, nonDoubleDashedTokenMap)
renameDirsAndFiles(dashCasePackageName, camelCasePackageName)
await npmInstall()
console.log('\nSetup complete! Run `npm start` to start the demo site. Happy hacking! :)')
r1.close()
}
main()