-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
382 lines (349 loc) · 9.19 KB
/
index.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
'use strict';
const path = require('path');
const fs = require('fs-extra');
const opta = require('opta');
const parseList = require('safe-parse-list');
const { create, load } = require('@npmcli/package-json');
const { Loggerr } = require('loggerr');
const packageName = require('./lib/package-name');
const git = require('./lib/git');
const npm = require('./lib/npm');
function initOpts () {
return opta({
commandDescription: 'Generate a package.json',
options: {
cwd: {
description: 'Directory to run in',
default: process.cwd(),
prompt: false,
flag: {
alias: 'd',
defaultDescription: 'process.cwd()',
default: () => process.cwd()
}
},
silent: {
type: 'boolean',
prompt: false,
flag: {
conflicts: ['verbose']
}
},
verbose: {
type: 'boolean',
prompt: false,
flag: {
conflicts: ['silent']
}
},
ignoreExisting: {
description: 'Ignore existing files (& overwrite them)',
default: false,
prompt: false,
flag: {
key: 'ignore-existing'
}
},
name: {
type: 'string',
prompt: {
message: 'Package name:',
validate: npm.validatePackageName,
default: (promptInput, allInput) => {
return packageName(allInput.name, allInput.scope, allInput.cwd);
}
}
},
scope: {
type: 'string',
description: 'Set a scope to be used when suggesting a package name',
prompt: false
},
version: {
type: 'string',
flag: {
key: 'package-version',
alias: 'V'
},
prompt: {
message: 'Initial version:',
default: (promptInput, allInput) => {
return allInput.version || '1.0.0';
}
}
},
description: {
type: 'string',
prompt: {
message: 'Description:'
}
},
author: {
type: 'string',
prompt: {
message: 'Author:',
default: (promptInput, allInput) => {
return allInput.author;
}
}
},
repository: {
type: 'string',
prompt: {
message: 'Repository:',
default: (promptInput, allInput) => {
return allInput.repository;
}
}
},
keywords: {
type: 'string',
prompt: {
message: 'Keywords:',
filter: parseList
}
},
// @TODO create a license generator
license: {
type: 'string',
default: 'ISC',
prompt: {
message: 'License:'
}
},
type: {
type: 'string',
prompt: {
message: 'Module Type:',
type: 'list',
choices: ['commonjs', 'module'],
default: (promptInput, allInput) => {
return allInput.type || 'commonjs';
}
}
},
main: {
type: 'string',
prompt: {
message: 'Main:',
default: (promptInput, allInput) => {
return allInput.main || 'index.js';
}
}
},
private: {
type: 'boolean',
prompt: false
},
dependencies: {
type: 'string',
prompt: {
message: 'Dependencies:',
filter: parseList,
validate: npm.validatePackageSpec
}
},
devDependencies: {
type: 'string',
flag: {
key: 'dev-dependencies'
},
prompt: {
message: 'Dev Dependencies:',
filter: parseList,
validate: npm.validatePackageSpec
}
},
peerDependencies: {
type: 'string',
flag: {
key: 'peer-dependencies'
},
prompt: false
},
// Scripts is an odd one
scripts: {
flag: false,
prompt: false,
default: {
test: 'echo "Error: no test specified" && exit 1'
}
},
// @TODO detect from existing file
spacer: {
type: 'string',
default: 2,
flag: {
defaultDescription: '2 spaces'
},
prompt: false
},
// Tell npm to save exact
saveExact: {
type: 'boolean',
default: false,
flag: {
key: 'save-exact'
},
prompt: false
}
}
});
}
module.exports = main;
async function main (input, _opts = {}) {
const options = initOpts();
options.overrides({
...input,
cwd: input.cwd || process.cwd()
});
let opts = options.values();
const log = _opts.logger || new Loggerr({
level: (opts.silent && 'silent') || (opts.verbose && 'debug') || 'info',
formatter: 'cli'
});
// Read current state and set defaults
const pkg = opts.ignoreExisting ? await create(opts.cwd) : await readPackageJson(options, { log });
await options.prompt({
promptor: _opts.promptor
})();
opts = options.values();
return write(opts, await format(opts, pkg), { log });
}
module.exports.options = initOpts().options;
module.exports.cli = function () {
return initOpts().cli((yargs) => {
yargs.command('$0', 'Generate a package.json', () => {}, main);
});
};
module.exports.readPackageJson = readPackageJson;
async function readPackageJson (options, { log } = {}) {
const opts = options.values();
let packageInstance;
let pkg = {};
try {
packageInstance = await load(opts.cwd, {
create: true
});
pkg = packageInstance.content;
log.debug('Read existing package.json', pkg);
} catch (e) {
// ignore if missing or unreadable
log.error(e);
}
let author;
if (!pkg || !pkg.author) {
const gitAuthor = await git.author({ cwd: opts.cwd });
if (gitAuthor) {
author = gitAuthor;
}
} else if (pkg && typeof pkg.author === 'string') {
author = pkg.author;
} else if (pkg && typeof pkg.author !== 'undefined') {
author = `${pkg.author.name}${pkg.author.email ? ` <${pkg.author.email}>` : ''}`;
}
const repo = await git.repository(opts.cwd, pkg);
// Remove some of the extras that don't make sense here
delete pkg.gitHead;
delete pkg.readme;
delete pkg._id;
// Set defaults from the package.json
options.defaults({
version: pkg.version,
name: pkg.name,
type: pkg.type,
main: pkg.main,
author,
description: pkg.description,
repository: repo,
keywords: pkg.keywords,
scripts: pkg.scripts,
license: pkg.license
});
return packageInstance.update(pkg);
}
module.exports.format = format;
async function format (opts, packageInstance) {
const pkg = packageInstance.content;
// The order here matters
pkg.name = opts.name;
pkg.version = opts.version;
pkg.description = opts.description || '';
pkg.main = opts.main;
pkg.type = opts.type || 'commonjs';
// TODO: extra parsing going on here due to wesleytodd/opta#1
pkg.keywords = uniquify([...(pkg.keywords || []), ...parseList(opts.keywords || [])]);
// Scripts
if (Object.keys(opts.scripts).length) {
pkg.scripts = { ...(pkg.scripts || {}), ...opts.scripts };
}
// TODO: to test the empty string, we need to stub git.author()
pkg.author = opts.author || '';
pkg.license = opts.license;
if (opts.repository) {
if (typeof opts.repository === 'string') {
pkg.repository = {
type: 'git',
url: opts.repository
};
} else {
pkg.repository = opts.repository;
}
}
if (opts.private === true) {
pkg.private = true;
}
if (opts.man && opts.man.length) {
pkg.man = Array.isArray(opts.man) && !opts.man[1] ? opts.man[0] : opts.man;
}
// Format peer deps
if (opts.peerDependencies && opts.peerDependencies.length) {
pkg.peerDependencies = {};
await Promise.all(opts.peerDependencies.map(async (name) => {
// @TODO we should align peer deps with the associated
// devDep or prodDep semver range
const spec = await npm.normalizePackageName(name);
let ver;
switch (spec.type) {
case 'range':
case 'version':
ver = spec.fetchSpec;
break;
default:
ver = '*';
}
pkg.peerDependencies[spec.name] = ver;
}));
}
return packageInstance.update(pkg);
}
module.exports.write = write;
// TODO: look at https://npm.im/json-file-plus for writing
async function write (opts, pkg, { log } = {}) {
const pkgPath = path.resolve(opts.cwd, 'package.json');
// Write package json
log.info(`Writing package.json\n${pkgPath}`);
await pkg.save();
// Run installs
if (opts.dependencies && opts.dependencies.length) {
log.info('Installing dependencies', opts.dependencies);
await npm.install(opts.dependencies, {
save: 'prod',
directory: opts.cwd,
exact: !!opts.saveExact
});
}
if (opts.devDependencies && opts.devDependencies.length) {
log.info('Installing dev dependencies', opts.devDependencies);
await npm.install(opts.devDependencies, {
save: 'dev',
directory: opts.cwd,
exact: !!opts.saveExact
});
}
// Read full package back to return
return fs.readJSON(pkgPath);
}
function uniquify (arr = []) {
return [...new Set(arr)];
}