forked from transitive-bullshit/create-react-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt-library-params.js
104 lines (96 loc) · 2.5 KB
/
prompt-library-params.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
'use strict'
const path = require('path')
const fs = require('fs')
const inquirer = require('inquirer')
const validateNpmName = require('validate-npm-package-name')
const config = require('./config')
module.exports = async (opts) => {
if (opts.name && !validateNpmName(opts.name).validForNewPackages) {
throw new Error(`invalid package name "${opts.name}"`)
}
if (opts.skipPrompts) {
if (!opts.name) {
throw new Error('invalid input; you must pass a package name with --skip-prompts')
}
Object.keys(opts).forEach((key) => {
const value = opts[key]
if (typeof value === 'function') {
opts[key] = value(opts)
}
})
return opts
} else {
const info = await inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'Package Name',
validate: (name) => {
return name && validateNpmName(name).validForNewPackages
},
default: opts.name
},
{
type: 'input',
name: 'description',
message: 'Package Description',
default: opts.description
},
{
type: 'input',
name: 'author',
message: 'Author\'s GitHub Handle',
default: opts.author
},
{
type: 'input',
name: 'repo',
message: 'GitHub Repo Path',
default: opts.repo
},
{
type: 'input',
name: 'license',
message: 'License',
default: opts.license
},
{
type: 'list',
name: 'manager',
message: 'Package Manager',
choices: [ 'npm', 'yarn' ],
default: opts.manager
},
{
type: 'list',
name: 'template',
message: 'Template',
choices: [ 'default', 'typescript', 'custom' ],
default: opts.template
},
{
type: 'input',
name: 'templatePath',
message: 'Template Path',
when: ({ template }) => template === 'custom',
validate: input => new Promise(resolve => {
const fullPath = path.resolve(process.cwd(), input)
fs.stat(fullPath, (err, stats) => {
if (err) {
return resolve(`Cannot resolve directory at: ${fullPath}`)
}
resolve(true)
})
})
}
])
config.set('author', info.author)
config.set('license', info.license)
config.set('manager', info.manager)
config.set('template', info.template)
return {
...info,
git: opts.git
}
}
}