UNPKG

5.24 kBJavaScriptView Raw
1/**
2Original: https://github.com/npm/init-package-json/blob/c649fbe/default-input.js
3
4The contents of this file are compiled into a function with this signature:
5
6 (function(
7 yes,
8 filename,
9 dirname,
10 basename,
11 package,
12 config,
13 prompt,
14 __filename,
15 __dirname,
16 __basename,
17 module,
18 require,
19 exports
20 ) {
21
22Because of the `package` parameter, we can't "use strict",
23as `package` is a reserved word in strict mode.
24
25Conveniently, all of these parameters are also available on `this`.
26We exploit this fact to avoid eslint breaking on the reserved word.
27*/
28
29const validateLicense = require("validate-npm-package-license");
30const validateName = require("validate-npm-package-name");
31const npa = require("npm-package-arg");
32const semver = require("semver");
33
34const niceName = rudeName =>
35 rudeName
36 .replace(/^node-|[.-]js$/g, "")
37 .replace(" ", "-")
38 .toLowerCase();
39
40let name = this.package.name || this.basename;
41let spec;
42try {
43 spec = npa(name);
44} catch (e) {
45 spec = {};
46}
47let scope = this.config.get("scope");
48if (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
60exports.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
74const version = this.package.version || this.config.get("init-version") || "1.0.0";
75exports.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
87if (this.config.get("private")) {
88 exports.private = true;
89}
90
91if (!this.package.description) {
92 exports.description = this.yes ? this.config.get("description") : this.prompt("description");
93}
94
95if (!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 // eslint-disable-next-line no-param-reassign
106 data = data.join(" ");
107 }
108
109 if (typeof data !== "string") {
110 return data;
111 }
112
113 return data.split(/[\s,]+/);
114 });
115}
116
117if (!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
131if (!this.package.homepage) {
132 const homepage = this.config.get("homepage");
133 exports.homepage = this.yes ? homepage : this.prompt("homepage", homepage);
134}
135
136const license = this.package.license || this.config.get("init-license") || "ISC";
137exports.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
151if (!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
157if (!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
163if (!this.package.bin && this.config.get("bin")) {
164 exports.bin = this.config.get("bin");
165}
166
167if (!this.package.directories && this.config.get("directories")) {
168 exports.directories = this.config.get("directories");
169}
170
171if (!this.package.files && this.config.get("files")) {
172 exports.files = cb => {
173 // callback MUST yield the thread for some inexplicable reason
174 process.nextTick(cb, null, this.config.get("files"));
175 };
176}
177
178if (!this.package.publishConfig && this.config.get("publishConfig")) {
179 exports.publishConfig = this.config.get("publishConfig");
180}
181
182if (!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
194if (!this.package.scripts) {
195 exports.scripts = {
196 test: 'echo "Error: run tests from root" && exit 1',
197 };
198}
199
200if (!this.package.dependencies && this.config.get("dependencies")) {
201 exports.dependencies = this.config.get("dependencies");
202}