-
Notifications
You must be signed in to change notification settings - Fork 0
/
default-input.js
263 lines (222 loc) · 6.43 KB
/
default-input.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
/* globals config, dirname, package, basename, yes, prompt */
const fs = require("fs/promises")
const path = require("path")
const validateLicense = require("validate-npm-package-license")
const validateName = require("validate-npm-package-name")
const npa = require("npm-package-arg")
const semver = require("semver")
// more popular packages should go here, maybe?
const isTestPkg = (p) => !!p.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/)
const invalid = (msg) => Object.assign(new Error(msg), { notValid: true })
const readDeps = (test, excluded) => async () => {
const dirs = await fs.readdir("node_modules").catch(() => null)
if (!dirs) {
return
}
const deps = {}
for (const dir of dirs) {
if (dir.match(/^\./) || test !== isTestPkg(dir) || excluded[dir]) {
continue
}
const dp = path.join(dirname, "node_modules", dir, "package.json")
const p = await fs.readFile(dp, "utf8").then((d) => JSON.parse(d)).catch(() => null)
if (!p || !p.version || p?._requiredBy?.some((r) => r === "#USER")) {
continue
}
deps[dir] = config.get("save-exact") ? p.version : config.get("save-prefix") + p.version
}
return deps
}
const getConfig = (key) => {
// dots take precedence over dashes
const def = config?.defaults?.[`init.${key}`]
const val = config.get(`init.${key}`)
return (val !== def && val) ? val : config.get(`init-${key.replace(/\./g, "-")}`)
}
const getName = () => {
// eslint-disable-next-line
const rawName = package.name || basename
let name = rawName
.replace(/^node-|[.-]js$/g, "")
.replace(/\s+/g, " ")
.replace(/ /g, "-")
.toLowerCase()
let spec
try {
spec = npa(name)
} catch {
spec = {}
}
let scope = config.get("scope")
if (scope) {
if (scope.charAt(0) !== "@") {
scope = "@" + scope
}
if (spec.scope) {
name = scope + "/" + spec.name.split("/")[1]
} else {
name = scope + "/" + name
}
}
return name
}
const name = getName()
exports.name = yes ? name : prompt("package name", name, (data) => {
const its = validateName(data)
if (its.validForNewPackages) {
return data
}
const errors = (its.errors || []).concat(its.warnings || [])
return invalid(`Sorry, ${errors.join(" and ")}.`)
})
const version = package.version || getConfig("version") || "1.0.0"
exports.version = yes ? version : prompt("version", version, (v) => {
if (semver.valid(v)) {
return v
}
return invalid(`Invalid version: "${v}"`)
})
if (!package.description) {
exports.description = yes ? "" : prompt("description")
}
if (!package.main) {
exports.main = async () => {
const files = await fs.readdir(dirname)
.then((list) => list.filter((f) => f.match(/\.js$/)))
.catch(() => [])
let index
if (files.includes("index.js")) {
index = "index.js"
} else if (files.includes("main.js")) {
index = "main.js"
} else if (files.includes(basename + ".js")) {
index = basename + ".js"
} else {
index = files[0] || "index.js"
}
return yes ? index : prompt("entry point", index)
}
}
if (!package.bin) {
exports.bin = async () => {
try {
const d = await fs.readdir(path.resolve(dirname, "bin"))
// just take the first js file we find there, or nada
let r = d.find((f) => f.match(/\.js$/))
if (r) {
r = `bin/${r}`
}
return r
} catch {
// no bins
}
}
}
exports.directories = async () => {
const dirs = await fs.readdir(dirname)
const res = dirs.reduce((acc, d) => {
if (/^examples?$/.test(d)) {
acc.example = d
} else if (/^tests?$/.test(d)) {
acc.test = d
} else if (/^docs?$/.test(d)) {
acc.doc = d
} else if (d === "man") {
acc.man = d
} else if (d === "lib") {
acc.lib = d
}
return acc
}, {})
return Object.keys(res).length === 0 ? undefined : res
}
if (!package.dependencies) {
exports.dependencies = readDeps(false, package.devDependencies || {})
}
if (!package.devDependencies) {
exports.devDependencies = readDeps(true, package.dependencies || {})
}
// MUST have a test script!
if (!package.scripts) {
const scripts = package.scripts || {}
const notest = 'echo "Error: no test specified" && exit 1'
exports.scripts = async () => {
const d = await fs.readdir(path.join(dirname, "node_modules")).catch(() => [])
// check to see what framework is in use, if any
let command
if (!scripts.test || scripts.test === notest) {
const commands = {
tap: "tap test/*.js",
expresso: "expresso test",
mocha: "mocha"
}
for (const [k, v] of Object.entries(commands)) {
if (d.includes(k)) {
command = v
}
}
}
const promptArgs = ["test command", (t) => t || notest]
if (command) {
promptArgs.splice(1, 0, command)
}
scripts.test = yes ? command || notest : prompt(...promptArgs)
return scripts
}
}
if (!package.repository) {
exports.repository = async () => {
const gconf = await fs.readFile(".git/config", "utf8").catch(() => "")
const lines = gconf.split(/\r?\n/)
let url
const i = lines.indexOf('[remote "origin"]')
if (i !== -1) {
url = gconf[i + 1]
if (!url.match(/^\s*url =/)) {
url = gconf[i + 2]
}
if (!url.match(/^\s*url =/)) {
url = null
} else {
url = url.replace(/^\s*url = /, "")
}
}
if (url && url.match(/^[email protected]:/)) {
url = url.replace(/^[email protected]:/, "https://github.com/")
}
return yes ? url || "" : prompt("git repository", url || undefined)
}
}
if (!package.keywords) {
exports.keywords = yes ? "" : prompt("keywords", (data) => {
if (!data) {
return
}
if (Array.isArray(data)) {
data = data.join(" ")
}
if (typeof data !== "string") {
return data
}
return data.split(/[\s,]+/)
})
}
if (!package.author) {
const authorName = getConfig("author.name")
exports.author = authorName
? {
name: authorName,
email: getConfig("author.email"),
url: getConfig("author.url")
}
: yes ? "" : prompt("author")
}
const license = package.license || getConfig("license") || "ISC"
exports.license = yes ? license : prompt("license", license, (data) => {
const its = validateLicense(data)
if (its.validForNewPackages) {
return data
}
const errors = (its.errors || []).concat(its.warnings || [])
return invalid(`Sorry, ${errors.join(" and ")}.`)
})