/
init_command.js
545 lines (505 loc) · 15.9 KB
/
init_command.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
'use strict';
const os = require('os');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const urllib = require('urllib');
const updater = require('npm-updater');
const mkdirp = require('mkdirp');
const inquirer = require('inquirer');
const yargs = require('yargs');
const glob = require('globby');
const is = require('is-type-of');
const homedir = require('node-homedir');
const compressing = require('compressing');
const rimraf = require('mz-modules/rimraf');
const isTextOrBinary = require('istextorbinary');
const ProxyAgent = require('proxy-agent');
require('colors');
module.exports = class Command {
constructor(options) {
options = options || {};
this.name = options.name || 'egg-init';
this.configName = options.configName || 'egg-init-config';
this.pkgInfo = options.pkgInfo || require('../package.json');
this.needUpdate = options.needUpdate !== false;
this.httpClient = urllib.create();
this.inquirer = inquirer;
this.fileMapping = {
gitignore: '.gitignore',
_gitignore: '.gitignore',
'_.gitignore': '.gitignore',
'_package.json': 'package.json',
'_.eslintignore': '.eslintignore',
'_.npmignore': '.npmignore',
};
}
* run(cwd, args) {
const argv = this.argv = this.getParser().parse(args || []);
this.cwd = cwd;
// console.log('%j', argv);
const proxyHost = process.env.http_proxy || process.env.HTTP_PROXY;
if (proxyHost) {
const proxyAgent = new ProxyAgent(proxyHost);
this.httpClient.agent = proxyAgent;
this.httpClient.httpsAgent = proxyAgent;
this.log(`use http_proxy: ${proxyHost}`);
}
// detect registry url
this.registryUrl = this.getRegistryByType(argv.registry);
this.log(`use registry: ${this.registryUrl}`);
if (this.needUpdate) {
// check update
yield updater({
package: this.pkgInfo,
registry: this.registryUrl,
level: 'major',
});
}
// ask for target dir
this.targetDir = yield this.getTargetDirectory();
// use local template
let templateDir = yield this.getTemplateDir();
if (!templateDir) {
// support --package=<npm name>
let pkgName = this.argv.package;
if (!pkgName) {
// list boilerplate
const boilerplateMapping = yield this.fetchBoilerplateMapping();
// ask for boilerplate
let boilerplate;
if (argv.type && boilerplateMapping.hasOwnProperty(argv.type)) {
boilerplate = boilerplateMapping[argv.type];
} else {
boilerplate = yield this.askForBoilerplateType(boilerplateMapping);
if (!boilerplate) return;
}
this.log(`use boilerplate: ${boilerplate.name}(${boilerplate.package})`);
pkgName = boilerplate.package;
}
// download boilerplate
templateDir = yield this.downloadBoilerplate(pkgName);
}
// copy template
yield this.processFiles(this.targetDir, templateDir);
// done
this.printUsage(this.targetDir);
}
/**
*
* @param {object} obj origin Object
* @param {string} key group by key
* @param {string} otherKey group by other key
* @return {object} result grouped object
*/
groupBy(obj, key, otherKey) {
const result = {};
for (const i in obj) {
let isMatch = false;
for (const j in obj[i]) {
// check if obj[i]'s property is 'key'
if (j === key) {
const mappingItem = obj[i][j];
if (typeof result[mappingItem] === 'undefined') {
result[mappingItem] = {};
}
result[mappingItem][i] = obj[i];
isMatch = true;
break;
}
}
if (!isMatch) {
// obj[i] doesn't have property 'key', then use 'otherKey' to group
if (typeof result[otherKey] === 'undefined') {
result[otherKey] = {};
}
result[otherKey][i] = obj[i];
}
}
return result;
}
/**
* show boilerplate list and let user choose one
*
* @param {Object} mapping - boilerplate config mapping, `{ simple: { "name": "simple", "package": "egg-boilerplate-simple", "description": "Simple egg app boilerplate" } }`
* @return {Object} boilerplate config item
*/
* askForBoilerplateType(mapping) {
// group by category
// group the mapping object by property 'category' or 'other' if item of mapping doesn't have 'category' property
const groupMapping = this.groupBy(mapping, 'category', 'other');
const groupNames = Object.keys(groupMapping);
let group;
if (groupNames.length > 1) {
const answers = yield inquirer.prompt({
name: 'group',
type: 'list',
message: 'Please select a boilerplate category',
choices: groupNames,
pageSize: groupNames.length,
});
group = groupMapping[answers.group];
} else {
group = groupMapping[groupNames[0]];
}
// ask for boilerplate
const choices = Object.keys(group).map(key => {
const item = group[key];
return {
name: `${key} - ${item.description}`,
value: item,
};
});
choices.unshift(new inquirer.Separator());
const { boilerplateInfo } = yield inquirer.prompt({
name: 'boilerplateInfo',
type: 'list',
message: 'Please select a boilerplate type',
choices,
pageSize: choices.length,
});
if (!boilerplateInfo.deprecate) return boilerplateInfo;
// ask for deprecate
const { shouldInstall } = yield inquirer.prompt({
name: 'shouldInstall',
type: 'list',
message: 'It\'s deprecated',
choices: [
{
name: `1. ${boilerplateInfo.deprecate}`,
value: false,
},
{
name: '2. I still want to continue installing',
value: true,
},
],
});
if (shouldInstall) {
return boilerplateInfo;
} else {
console.log(`Exit due to: ${boilerplateInfo.deprecate}`.deprecate);
return;
}
}
/**
* ask user to provide variables which is defined at boilerplate
* @param {String} targetDir - target dir
* @param {String} templateDir - template dir
* @return {Object} variable scope
*/
* askForVariable(targetDir, templateDir) {
let questions;
try {
questions = require(templateDir);
// support function
if (is.function(questions)) {
questions = questions(this);
}
// use target dir name as `name` default
if (questions.name && !questions.name.default) {
questions.name.default = path.basename(targetDir).replace(/^egg-/, '');
}
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
this.log(`load boilerplate config got trouble, skip and use defaults, ${err.message}`.yellow);
}
return {};
}
this.log('collecting boilerplate config...');
const keys = Object.keys(questions);
if (this.argv.silent) {
const result = keys.reduce((result, key) => {
const defaultFn = questions[key].default;
const filterFn = questions[key].filter;
if (typeof defaultFn === 'function') {
result[key] = defaultFn(result) || '';
} else {
result[key] = questions[key].default || '';
}
if (typeof filterFn === 'function') {
result[key] = filterFn(result[key]) || '';
}
return result;
}, {});
this.log('use default due to --silent, %j', result);
return result;
} else {
return yield inquirer.prompt(keys.map(key => {
const question = questions[key];
return {
type: 'input',
name: key,
message: question.description || question.desc,
default: question.default,
filter: question.filter,
};
}));
}
}
/**
* copy boilerplate to target dir with template scope replace
* @param {String} targetDir - target dir
* @param {String} templateDir - template dir, must contain a folder which named `boilerplate`
* @return {Array} file names
*/
* processFiles(targetDir, templateDir) {
const src = path.join(templateDir, 'boilerplate');
const locals = yield this.askForVariable(targetDir, templateDir);
const files = glob.sync('**/*', { cwd: src, dot: true });
files.forEach(file => {
const from = path.join(src, file);
const to = path.join(targetDir, this.replaceTemplate(this.fileMapping[file] || file, locals));
const content = fs.readFileSync(from, { encoding: 'utf-8' });
this.log('write to %s', to);
// check if content is a text file
const result = isTextOrBinary.isTextSync(from, content)
? this.replaceTemplate(content, locals)
: content;
mkdirp.sync(path.dirname(to));
fs.writeFileSync(to, result);
});
return files;
}
/**
* get argv parser
* @return {Object} yargs instance
*/
getParser() {
return yargs
.usage('init egg project from boilerplate.\nUsage: $0 [dir] --type=simple')
.options(this.getParserOptions())
.alias('h', 'help')
.version()
.help();
}
/**
* get yargs options
* @return {Object} opts
*/
getParserOptions() {
return {
type: {
type: 'string',
description: 'boilerplate type',
},
dir: {
type: 'string',
description: 'target directory',
},
force: {
type: 'boolean',
description: 'force to override directory',
alias: 'f',
},
template: {
type: 'string',
description: 'local path to boilerplate',
},
package: {
type: 'string',
description: 'boilerplate package name',
},
registry: {
type: 'string',
description: 'npm registry, support china/npm/custom, default to auto detect',
alias: 'r',
},
silent: {
type: 'boolean',
description: 'don\'t ask, just use default value',
},
};
}
/**
* get registryUrl by short name
* @param {String} key - short name, support `china / npm / npmrc`, default to read from .npmrc
* @return {String} registryUrl
*/
getRegistryByType(key) {
switch (key) {
case 'china':
return 'https://registry.npm.taobao.org';
case 'npm':
return 'https://registry.npmjs.org';
default: {
if (/^https?:/.test(key)) {
return key.replace(/\/$/, '');
} else {
// support .npmrc
const home = homedir();
let url = process.env.npm_registry || process.env.npm_config_registry || 'https://registry.npmjs.org';
if (fs.existsSync(path.join(home, '.cnpmrc')) || fs.existsSync(path.join(home, '.tnpmrc'))) {
url = 'https://registry.npm.taobao.org';
}
url = url.replace(/\/$/, '');
return url;
}
}
}
}
/**
* ask for target directory, will check if dir is valid.
* @return {String} Full path of target directory
*/
* getTargetDirectory() {
const dir = this.argv._[0] || this.argv.dir || '';
let targetDir = path.resolve(this.cwd, dir);
const force = this.argv.force;
const validate = dir => {
// create dir if not exist
if (!fs.existsSync(dir)) {
mkdirp.sync(dir);
return true;
}
// not a directory
if (!fs.statSync(dir).isDirectory()) {
return `${dir} already exists as a file`.red;
}
// check if directory empty
const files = fs.readdirSync(dir).filter(name => name[0] !== '.');
if (files.length > 0) {
if (force) {
this.log(`${dir} already exists and will be override due to --force`.red);
return true;
}
return `${dir} already exists and not empty: ${JSON.stringify(files)}`.red;
}
return true;
};
// if argv dir is invalid, then ask user
const isValid = validate(targetDir);
if (isValid !== true) {
this.log(isValid);
const answer = yield this.inquirer.prompt({
name: 'dir',
message: 'Please enter target dir: ',
default: dir || '.',
filter: dir => path.resolve(this.cwd, dir),
validate,
});
targetDir = answer.dir;
}
this.log(`target dir is ${targetDir}`);
return targetDir;
}
/**
* find template dir from support `--template=`
* @return {String} template files dir
*/
* getTemplateDir() {
let templateDir;
// when use `egg-init --template=PATH`
if (this.argv.template) {
templateDir = path.resolve(this.cwd, this.argv.template);
if (!fs.existsSync(templateDir)) {
this.log(`${templateDir} is not exists`.red);
} else if (!fs.existsSync(path.join(templateDir, 'boilerplate'))) {
this.log(`${templateDir} should contain boilerplate folder`.red);
} else {
this.log(`local template dir is ${templateDir.green}`);
return templateDir;
}
}
}
/**
* fetch boilerplate mapping from `egg-init-config`
* @param {String} [pkgName] - config package name, default to `this.configName`
* @return {Object} boilerplate config mapping, `{ simple: { "name": "simple", "package": "egg-boilerplate-simple", "description": "Simple egg app boilerplate" } }`
*/
* fetchBoilerplateMapping(pkgName) {
const pkgInfo = yield this.getPackageInfo(pkgName || this.configName, true);
const mapping = pkgInfo.config.boilerplate;
Object.keys(mapping).forEach(key => {
const item = mapping[key];
item.name = item.name || key;
item.from = pkgInfo;
});
return mapping;
}
/**
* print usage guide
*/
printUsage() {
this.log(`usage:
- cd ${this.targetDir}
- npm install
- npm start / npm run dev / npm test
`);
}
/**
* replace content with template scope,
* - `{{ test }}` will replace
* - `\{{ test }}` will skip
*
* @param {String} content - template content
* @param {Object} scope - variable scope
* @return {String} new content
*/
replaceTemplate(content, scope) {
return content.toString().replace(/(\\)?{{ *(\w+) *}}/g, (block, skip, key) => {
if (skip) {
return block.substring(skip.length);
}
return scope.hasOwnProperty(key) ? scope[key] : block;
});
}
/**
* download boilerplate by pkgName then extract it
* @param {String} pkgName - boilerplate package name
* @return {String} extract directory
*/
* downloadBoilerplate(pkgName) {
const result = yield this.getPackageInfo(pkgName, false);
const tgzUrl = result.dist.tarball;
const saveDir = path.join(os.tmpdir(), 'egg-init-boilerplate');
yield rimraf(saveDir);
this.log(`downloading ${tgzUrl}`);
const response = yield this.curl(tgzUrl, { streaming: true, followRedirect: true });
yield compressing.tgz.uncompress(response.res, saveDir);
this.log(`extract to ${saveDir}`);
return path.join(saveDir, '/package');
}
/**
* send curl to remote server
* @param {String} url - target url
* @param {Object} [options] - request options
* @return {Object} response data
*/
* curl(url, options) {
return yield this.httpClient.request(url, options);
}
/**
* get package info from registry
*
* @param {String} pkgName - package name
* @param {Boolean} [withFallback] - when http request fail, whethe to require local
* @return {Object} pkgInfo
*/
* getPackageInfo(pkgName, withFallback) {
this.log(`fetching npm info of ${pkgName}`);
try {
const result = yield this.curl(`${this.registryUrl}/${pkgName}/latest`, {
dataType: 'json',
followRedirect: true,
});
assert(result.status === 200, `npm info ${pkgName} got error: ${result.status}, ${result.data.reason}`);
return result.data;
} catch (err) {
if (withFallback) {
this.log(`use fallback from ${pkgName}`);
return require(`${pkgName}/package.json`);
} else {
throw err;
}
}
}
/**
* log with prefix
*/
log() {
const args = Array.prototype.slice.call(arguments);
args[0] = `[${this.name}] `.blue + args[0];
console.log.apply(console, args);
}
};