-
-
Notifications
You must be signed in to change notification settings - Fork 793
/
inquire.ts
88 lines (79 loc) · 2.55 KB
/
inquire.ts
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
import path from 'path'
import semver from 'semver'
import prompts, { PromptObject } from 'prompts'
import validateName from 'validate-npm-package-name'
import { config } from './core'
import { Context } from './types'
/**
* Prompt validater.
*/
export const validater: Record<string, (input: string) => true | string> = {
name: input => {
const result = validateName(input)
if (result.validForNewPackages) return true
/* c8 ignore next */
return result.errors?.join(', ') ?? result.warnings?.join(',') ?? ''
},
version: input => {
const valid = semver.valid(input)
if (valid != null) return true
return `The \`${input}\` is not a semantic version.`
},
email: input => {
const valid = /[^\s]+@[^\s]+\.[^\s]+/.test(input)
return valid || `The \`${input}\` is not a email address.`
},
url: input => {
const valid = /https?:\/\/[^\s]+/.test(input)
return valid || `The \`${input}\` is not a url address.`
}
}
/**
* Return a prompt processor.
* defaults & validater
*/
export const processor = (ctx: Context) => (item: PromptObject) => {
switch (item.name) {
case 'name':
item.validate = item.validate ?? validater.name
item.initial = item.initial ?? path.basename(ctx.dest)
break
case 'version':
item.validate = item.validate ?? validater.version
item.initial = item.initial ?? config.npm?.['init-version'] ?? '0.1.0'
break
case 'author':
item.initial = item.initial ?? config.npm?.['init-author-name'] ?? config.git?.user?.name
break
case 'email':
item.validate = item.validate ?? validater.email
item.initial = item.initial ?? config.npm?.['init-author-email'] ?? config.git?.user?.email
break
case 'url':
item.validate = item.validate ?? validater.url
item.initial = item.initial ?? config.npm?.['init-author-url'] ?? config.git?.user?.url
break
}
}
/**
* Inquire template prompts.
*/
export default async (ctx: Context): Promise<void> => {
// require node >= v8.3.0
console.clear()
// default prompts
if (ctx.config.prompts == null) {
ctx.config.prompts = { name: 'name', type: 'text', message: 'Project name' }
}
if (!Array.isArray(ctx.config.prompts)) {
ctx.config.prompts = [ctx.config.prompts]
}
ctx.config.prompts.forEach(processor(ctx))
// override by options (cli argv)
prompts.override(ctx.options)
/* c8 ignore next 3 */
const onCancel = (): never => {
throw new Error('You have cancelled this task.')
}
Object.assign(ctx.answers, await prompts(ctx.config.prompts, { onCancel }))
}