-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
lerna-module-data.js
206 lines (168 loc) · 5.18 KB
/
lerna-module-data.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
/**
Original: https://github.com/npm/init-package-json/blob/c649fbe/default-input.js
The contents of this file are compiled into a function with this signature:
(function(
yes,
filename,
dirname,
basename,
package,
config,
prompt,
__filename,
__dirname,
__basename,
module,
require,
exports
) {
Because of the `package` parameter, we can't "use strict",
as `package` is a reserved word in strict mode.
Conveniently, all of these parameters are also available on `this`.
We exploit this fact to avoid eslint breaking on the reserved word.
*/
const validateLicense = require("validate-npm-package-license");
const validateName = require("validate-npm-package-name");
const npa = require("npm-package-arg");
const semver = require("semver");
const niceName = (rudeName) =>
rudeName
.replace(/^node-|[.-]js$/g, "")
.replace(" ", "-")
.toLowerCase();
let name = this.package.name || this.basename;
let spec;
try {
spec = npa(name);
} catch (e) {
spec = {};
}
let scope = this.config.get("scope");
if (scope) {
if (scope.charAt(0) !== "@") {
scope = `@${scope}`;
}
if (spec.scope) {
name = `${scope}/${spec.name.split("/")[1]}`;
} else {
name = `${scope}/${name}`;
}
}
exports.name = this.yes
? name
: this.prompt("package name", niceName(name), (data) => {
const its = validateName(data);
if (its.validForNewPackages) {
return data;
}
const errors = (its.errors || []).concat(its.warnings || []);
const er = new Error(`Sorry, ${errors.join(" and ")}.`);
er.notValid = true;
return er;
});
const version = this.package.version || this.config.get("init-version") || "1.0.0";
exports.version = this.yes
? version
: this.prompt("version", version, (data) => {
if (semver.valid(data)) {
return data;
}
const er = new Error(`Invalid version: "${data}"`);
er.notValid = true;
return er;
});
if (this.config.get("private")) {
exports.private = true;
}
if (!this.package.description) {
exports.description = this.yes ? this.config.get("description") : this.prompt("description");
}
if (!this.package.keywords) {
const keywords = this.config.get("keywords") || "";
exports.keywords = this.yes
? keywords
: this.prompt("keywords", keywords, (data) => {
if (!data) {
return undefined;
}
if (Array.isArray(data)) {
// eslint-disable-next-line no-param-reassign
data = data.join(" ");
}
if (typeof data !== "string") {
return data;
}
return data.split(/[\s,]+/);
});
}
if (!this.package.author) {
let authorConfig;
if (this.config.get("init-author-name")) {
authorConfig = {
name: this.config.get("init-author-name"),
email: this.config.get("init-author-email"),
url: this.config.get("init-author-url"),
};
}
exports.author = authorConfig || (this.yes ? "" : this.prompt("author"));
}
if (!this.package.homepage) {
const homepage = this.config.get("homepage");
exports.homepage = this.yes ? homepage : this.prompt("homepage", homepage);
}
const license = this.package.license || this.config.get("init-license") || "ISC";
exports.license = this.yes
? license
: this.prompt("license", license, (data) => {
const its = validateLicense(data);
if (its.validForNewPackages) {
return data;
}
const errors = (its.errors || []).concat(its.warnings || []);
const er = new Error(`Sorry, ${errors.join(" and ")}.`);
er.notValid = true;
return er;
});
if (!this.package.main && this.config.get("init-main")) {
const mainEntry = this.config.get("init-main");
exports.main = this.yes ? mainEntry : this.prompt("entry point", mainEntry);
}
if (!this.package.module && this.config.get("init-es-module")) {
const moduleEntry = this.config.get("init-es-module");
exports.module = this.yes ? moduleEntry : this.prompt("module entry", moduleEntry);
}
if (!this.package.bin && this.config.get("bin")) {
exports.bin = this.config.get("bin");
}
if (!this.package.directories && this.config.get("directories")) {
exports.directories = this.config.get("directories");
}
if (!this.package.files && this.config.get("files")) {
exports.files = (cb) => {
// callback MUST yield the thread for some inexplicable reason
process.nextTick(cb, null, this.config.get("files"));
};
}
if (!this.package.publishConfig && this.config.get("publishConfig")) {
exports.publishConfig = this.config.get("publishConfig");
}
if (!this.package.repository) {
exports.repository = (cb) => {
let val = this.config.get("repository");
if (val && val.match(/^git@github.com:/)) {
val = val.replace(/^git@github.com:/, "https://github.com/");
}
return cb(null, this.yes ? val : this.prompt("git repository", val));
};
}
if (!this.package.scripts) {
exports.scripts = {
test: `node ./__tests__/${name}.test.js`,
};
}
if (!this.package.dependencies && this.config.get("dependencies")) {
exports.dependencies = this.config.get("dependencies");
}
if (this.config.get("esModule")) {
exports.type = "module";
}