-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathinit.js
502 lines (435 loc) · 11.5 KB
/
init.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
const kleur = require('kleur')
const prompts = require('prompts')
const sh = require('shelljs')
const sortObjectByKeyNameList = require('sort-object-keys')
const replace = require('replace-in-file')
const { resolve, join } = require('path')
const { writeFileSync } = require('fs')
const { fork } = require('child_process')
const { log, error } = console
const ROOT = resolve(__dirname, '..')
const pkg = require('../package.json')
const { CI: IS_CI } = normalizeEnv(process.env)
/**
* @typedef {keyof typeof pkg['devDependencies']} PkgKeys
*/
const rmDirs = ['.git', 'templates']
const rmFiles = [
'.gitattributes',
'scripts/migrate.js',
'scripts/init.js',
'scripts/types.d.ts',
'CHANGELOG.md',
]
const rmPackages = [
'@types/chokidar',
// packages needed for migrate/init script
'replace-in-file',
'json5',
'@types/json5',
'sort-package-json',
'sort-object-keys',
'@types/prompts',
'prompts',
'shelljs',
]
const modifyFiles = [
'.github/CONTRIBUTING.md',
'.github/ISSUE_TEMPLATE.md',
'src/index.ts',
'LICENSE.md',
]
main()
// =============================================================================
// Helpers
// =============================================================================
/**
*
* @param {NodeJS.ProcessEnv} env
* @returns {{[P in keyof NodeJS.ProcessEnv]: string | boolean | undefined}}
*/
function normalizeEnv(env) {
const keys = Object.keys(env)
// prettier-ignore
const normalizedEnvMap = /** @type {{[P in keyof NodeJS.ProcessEnv]: string | boolean | undefined}} */ (keys.reduce(
(acc, nextKey) => {
const value = env[nextKey]
if (value === 'true') {
return { ...acc, [nextKey]: true }
}
if (value === 'false') {
return { ...acc, [nextKey]: false }
}
return acc
},
{}
))
return normalizedEnvMap
}
/**
*
* @param {string[]} value
*/
function normalizeListResponse(value) {
const isEmptyList = value.length === 1 && value[0] === ''
return isEmptyList ? [] : value
}
/**
*
* @param {string} value
*/
function isKebabCase(value) {
const kebabCaseRegex = /^[a-z]+(\-[a-z]+)*$/
return kebabCaseRegex.test(value)
}
/**
*
* @param {string} value
*/
function isTextRequired(value) {
return typeof value === 'string' && value.length > 0
}
/**
*
* @param {string} value
*/
function isEmail(value) {
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return re.test(String(value).toLowerCase())
}
/**
*
* @param {string} value
*/
function isValidScopeName(value) {
return value.startsWith('@') && isKebabCase(value.slice(1))
}
/**
* Omit object key/values and return new one
*
* @param {{[pkgName:string]:string}} objectEntity
* @param {string[]} propsToRemove
* @returns {{[pkgName:string]:string}} deps
*/
function omitPropsFromObject(objectEntity, propsToRemove) {
const objKeys = Object.keys(objectEntity)
return objKeys.reduce((acc, pkgName) => {
if (propsToRemove.includes(pkgName)) {
return acc
}
return { ...acc, [pkgName]: objectEntity[pkgName] }
}, {})
}
function clearConsole() {
process.stdout.write('\x1B[2J\x1B[0f')
}
function checkGit() {
if (!sh.which('git')) {
log(kleur.red('Sorry, this script requires git'))
process.exit(1)
}
}
function initGit() {
const gitInitOutput = /**@type {string} */ (sh.exec(`git init "${ROOT}"`, {
silent: true,
}).stdout)
log(kleur.green(gitInitOutput.replace(/(\n|\r)+/g, '')))
}
function initGitHooks() {
const huskyModule = resolve(ROOT, 'node_modules', 'husky', 'husky')
// Initialize Husky
const childProcess = fork(huskyModule, ['install'], {
silent: true,
})
childProcess
.on('close', () => {
log(kleur.green('Git hooks set up'))
})
.on('error', (err) => {
log(kleur.red('Git hooks set up FAILED 💥'))
error(err)
})
}
/**
* @param {string|string[]} files
* @param {import('./types').LibConfig} config
* @param {Array<[string|RegExp, string]>} extras
*/
function createTemplateVariablesConfig(
files,
{ libraryName, packageName, description, githubName, username, usermail },
extras = []
) {
const extrasConfig = extras.reduce(
(acc, configPair) => {
const [from, to] = configPair
if (from && to) {
acc.from = [...acc.from, from]
acc.to = [...acc.to, to]
}
return acc
},
{
from: /** @type {Array<string|RegExp>} */ ([]),
to: /** @type {Array<string>} */ ([]),
}
)
return {
files,
from: [
/\{library-name\}/g,
/\{package-name\}/g,
/\{library-description\}/g,
/\{github-name\}/g,
/\{username\}/g,
/\{usermail\}/g,
...extrasConfig.from,
],
to: [
libraryName,
packageName,
description,
githubName,
username,
usermail,
...extrasConfig.to,
],
}
}
/**
*
* @param {import('./types').LibConfig} config
*/
function processPgkJson({
packageName,
libraryName,
description,
keywords,
username,
usermail,
githubName,
version = '0.0.0',
}) {
const { devDependencies, scripts } = pkg
const newDevDeps = sortObjectByKeyNameList(
omitPropsFromObject(devDependencies, rmPackages)
)
const newScripts = omitPropsFromObject(scripts, ['postinstall'])
/**
* @type {typeof pkg}
*/
const updatePkg = {
...pkg,
version,
name: packageName,
description,
keywords,
author: `${username} <${usermail}>`,
repository: {
...pkg.repository,
url: `https://github.com/${githubName}/${libraryName}`,
},
scripts: /** @type {any} */ (newScripts),
devDependencies: /** @type {any} */ (newDevDeps),
}
writePackage(updatePkg)
/**
* @param {typeof pkg} pkg
*/
function writePackage(pkg) {
const updatedLibPkgToWrite = JSON.stringify(pkg, null, 2)
writeFileSync(join(ROOT, 'package.json'), updatedLibPkgToWrite)
log(kleur.green('package.json was setup successfully'), '\n')
}
}
/**
* Removes items from the project that aren't needed after the initial setup
*
* @param {import('./types').LibConfig} config
*/
function removeItems(config) {
log(kleur.underline().white('Removed'))
const vsCodeDirToRemove = config.useVSCode ? null : '.vscode'
const rmItems = /** @type {string[]} */ ([
...rmDirs,
...rmFiles,
vsCodeDirToRemove,
].filter(Boolean))
const files = rmItems.map((fileName) => resolve(ROOT, fileName))
// 'rm' command checks the item type before attempting to remove it
sh.rm('-rf', files)
log(kleur.red(rmItems.join('\n')), '\n')
}
/**
* Updates the contents of the template files with the library name or user details
*
* @param {import('./types').LibConfig} config
*/
function modifyContents(config) {
console.log(kleur.underline().white('Modified'))
const files = modifyFiles.map((fileName) => resolve(ROOT, fileName))
const replaceConfig = createTemplateVariablesConfig(files, config, [
[
/Martin\s+Hochel/g, // This is here only for LICENSE.md
`${config.username} <${config.usermail}>`,
],
])
try {
const changedFiles = replace.sync(replaceConfig)
log(kleur.yellow(changedFiles.join('\n')))
} catch (reason) {
error('An error occurred modifying the file: ', reason)
}
log('\n')
}
function getUserInfo() {
const username = /** @type {string} */ (sh.exec('git config user.name', {
silent: true,
}).stdout)
const usermail = /** @type {string} */ (sh.exec('git config user.email', {
silent: true,
}).stdout)
return { username: username.trim(), usermail: usermail.trim() }
}
/**
* @param {Pick<import('./types').LibConfig,'username' | 'usermail'>} config
* @returns {import('prompts').PromptObject[]}
*/
function createQuestions(config) {
return [
{
type: 'text',
name: 'libraryName',
message: 'What do you want the library to be called? (use kebab-case)',
validate: (value) =>
isKebabCase(value) ||
`"kebab-case" uses lowercase letters, and hyphens for any punctuation`,
},
{
type: 'confirm',
name: 'useScope',
message: 'Wanna publish this package under a namespace(like @my-org)',
// prettier-ignore
initial: /**@type any */ (false)
},
{
type: /** @type {import('prompts').ValueOrFunc<import('prompts').PromptType>} */ ((
prev
) => (prev === true ? 'text' : null)),
name: 'scope',
message: 'Scope/namespace (needs to start with @)',
validate: (value) =>
isValidScopeName(value) ||
`namespace needs to start with @ followed by kebab-case`,
},
{
type: 'text',
name: 'description',
message: 'Describe your library in one sentence',
initial: 'A library written in TypeScript',
},
{
type: 'list',
name: 'keywords',
message: 'Provide keywords to improve library discovery (coma separated)',
format: normalizeListResponse,
},
{
type: 'confirm',
name: 'useVSCode',
message:
'Are you using VSCode and wanna include recommended configuration?',
// prettier-ignore
initial: /**@type any */ (false)
},
{
type: 'text',
name: 'username',
message: 'Your name',
initial: config.username,
validate: (value) =>
isTextRequired(value) || `Please provide your name and surname`,
},
{
type: 'text',
name: 'usermail',
message: 'Your email',
initial: config.usermail,
validate: (value) => isEmail(value) || `Please provide valid email`,
},
{
type: 'text',
name: 'githubName',
message: 'Your github account name',
validate: (value) =>
isTextRequired(value) || `Please provide valid github username`,
},
]
}
/**
*
* @param {import('prompts').PromptObject} prompt
* @param {any} answers
* @returns {void | boolean}
*/
function handlePromptExit(prompt, answers) {
error(
kleur
.bold()
.white()
.bgRed('Terminating library init')
)
process.exit(1)
}
/**
*
* @param {import('./types').PromptAnswers} promptAnswers
* @returns {import('./types').LibConfig}
*/
function getLibConfig({ scope, libraryName, ...promptAnswers }) {
const packageName = scope ? `${scope}/${libraryName}` : libraryName
return {
packageName,
libraryName,
...promptAnswers,
}
}
/**
* @param {import('./types').LibConfig} config
*/
function processTemplates(config) {
const templatesDir = resolve(ROOT, 'templates')
const files = ['README.md'].map((fileName) => resolve(templatesDir, fileName))
const replaceConfig = createTemplateVariablesConfig(files, config)
log(kleur.underline().white('Creating files from templates'))
try {
const changedFiles = replace.sync(replaceConfig)
sh.cp('-Rf', `${templatesDir}/*`, `${ROOT}/`)
log(kleur.yellow(changedFiles.join('\n')))
} catch (reason) {
error('An error occurred modifying the file: ', reason)
}
log('\n')
}
async function main() {
if (IS_CI) {
log(kleur.gray('👉 CI: skipping init'))
process.exit(0)
}
clearConsole()
checkGit()
const userInfo = getUserInfo()
const questions = createQuestions(userInfo)
const response = /** @type {import('./types').PromptAnswers} */ (await prompts(
questions,
{ onCancel: handlePromptExit }
))
const config = getLibConfig(response)
processPgkJson(config)
modifyContents(config)
processTemplates(config)
removeItems(config)
initGit()
initGitHooks()
log(kleur.cyan("OK, you're all set. Happy type-safe coding!! 🌊 🏄 🤙 \n"))
}