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 | const validateLicense = require("validate-npm-package-license");
|
30 | const validateName = require("validate-npm-package-name");
|
31 | const npa = require("npm-package-arg");
|
32 | const semver = require("semver");
|
33 |
|
34 | const niceName = rudeName =>
|
35 | rudeName
|
36 | .replace(/^node-|[.-]js$/g, "")
|
37 | .replace(" ", "-")
|
38 | .toLowerCase();
|
39 |
|
40 | let name = this.package.name || this.basename;
|
41 | let spec;
|
42 | try {
|
43 | spec = npa(name);
|
44 | } catch (e) {
|
45 | spec = {};
|
46 | }
|
47 | let scope = this.config.get("scope");
|
48 | if (scope) {
|
49 | if (scope.charAt(0) !== "@") {
|
50 | scope = `@${scope}`;
|
51 | }
|
52 |
|
53 | if (spec.scope) {
|
54 | name = `${scope}/${spec.name.split("/")[1]}`;
|
55 | } else {
|
56 | name = `${scope}/${name}`;
|
57 | }
|
58 | }
|
59 |
|
60 | exports.name = this.yes
|
61 | ? name
|
62 | : this.prompt("package name", niceName(name), data => {
|
63 | const its = validateName(data);
|
64 | if (its.validForNewPackages) {
|
65 | return data;
|
66 | }
|
67 |
|
68 | const errors = (its.errors || []).concat(its.warnings || []);
|
69 | const er = new Error(`Sorry, ${errors.join(" and ")}.`);
|
70 | er.notValid = true;
|
71 | return er;
|
72 | });
|
73 |
|
74 | const version = this.package.version || this.config.get("init-version") || "1.0.0";
|
75 | exports.version = this.yes
|
76 | ? version
|
77 | : this.prompt("version", version, data => {
|
78 | if (semver.valid(data)) {
|
79 | return data;
|
80 | }
|
81 |
|
82 | const er = new Error(`Invalid version: "${data}"`);
|
83 | er.notValid = true;
|
84 | return er;
|
85 | });
|
86 |
|
87 | if (this.config.get("private")) {
|
88 | exports.private = true;
|
89 | }
|
90 |
|
91 | if (!this.package.description) {
|
92 | exports.description = this.yes ? this.config.get("description") : this.prompt("description");
|
93 | }
|
94 |
|
95 | if (!this.package.keywords) {
|
96 | const keywords = this.config.get("keywords") || "";
|
97 | exports.keywords = this.yes
|
98 | ? keywords
|
99 | : this.prompt("keywords", keywords, data => {
|
100 | if (!data) {
|
101 | return undefined;
|
102 | }
|
103 |
|
104 | if (Array.isArray(data)) {
|
105 |
|
106 | data = data.join(" ");
|
107 | }
|
108 |
|
109 | if (typeof data !== "string") {
|
110 | return data;
|
111 | }
|
112 |
|
113 | return data.split(/[\s,]+/);
|
114 | });
|
115 | }
|
116 |
|
117 | if (!this.package.author) {
|
118 | let authorConfig;
|
119 |
|
120 | if (this.config.get("init-author-name")) {
|
121 | authorConfig = {
|
122 | name: this.config.get("init-author-name"),
|
123 | email: this.config.get("init-author-email"),
|
124 | url: this.config.get("init-author-url"),
|
125 | };
|
126 | }
|
127 |
|
128 | exports.author = authorConfig || (this.yes ? "" : this.prompt("author"));
|
129 | }
|
130 |
|
131 | if (!this.package.homepage) {
|
132 | const homepage = this.config.get("homepage");
|
133 | exports.homepage = this.yes ? homepage : this.prompt("homepage", homepage);
|
134 | }
|
135 |
|
136 | const license = this.package.license || this.config.get("init-license") || "ISC";
|
137 | exports.license = this.yes
|
138 | ? license
|
139 | : this.prompt("license", license, data => {
|
140 | const its = validateLicense(data);
|
141 | if (its.validForNewPackages) {
|
142 | return data;
|
143 | }
|
144 |
|
145 | const errors = (its.errors || []).concat(its.warnings || []);
|
146 | const er = new Error(`Sorry, ${errors.join(" and ")}.`);
|
147 | er.notValid = true;
|
148 | return er;
|
149 | });
|
150 |
|
151 | if (!this.package.main && this.config.get("init-main")) {
|
152 | const mainEntry = this.config.get("init-main");
|
153 |
|
154 | exports.main = this.yes ? mainEntry : this.prompt("entry point", mainEntry);
|
155 | }
|
156 |
|
157 | if (!this.package.module && this.config.get("init-es-module")) {
|
158 | const moduleEntry = this.config.get("init-es-module");
|
159 |
|
160 | exports.module = this.yes ? moduleEntry : this.prompt("module entry", moduleEntry);
|
161 | }
|
162 |
|
163 | if (!this.package.bin && this.config.get("bin")) {
|
164 | exports.bin = this.config.get("bin");
|
165 | }
|
166 |
|
167 | if (!this.package.directories && this.config.get("directories")) {
|
168 | exports.directories = this.config.get("directories");
|
169 | }
|
170 |
|
171 | if (!this.package.files && this.config.get("files")) {
|
172 | exports.files = cb => {
|
173 |
|
174 | process.nextTick(cb, null, this.config.get("files"));
|
175 | };
|
176 | }
|
177 |
|
178 | if (!this.package.publishConfig && this.config.get("publishConfig")) {
|
179 | exports.publishConfig = this.config.get("publishConfig");
|
180 | }
|
181 |
|
182 | if (!this.package.repository) {
|
183 | exports.repository = cb => {
|
184 | let val = this.config.get("repository");
|
185 |
|
186 | if (val && val.match(/^git@github.com:/)) {
|
187 | val = val.replace(/^git@github.com:/, "https://github.com/");
|
188 | }
|
189 |
|
190 | return cb(null, this.yes ? val : this.prompt("git repository", val));
|
191 | };
|
192 | }
|
193 |
|
194 | if (!this.package.scripts) {
|
195 | exports.scripts = {
|
196 | test: 'echo "Error: run tests from root" && exit 1',
|
197 | };
|
198 | }
|
199 |
|
200 | if (!this.package.dependencies && this.config.get("dependencies")) {
|
201 | exports.dependencies = this.config.get("dependencies");
|
202 | }
|