-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
environment.js
510 lines (432 loc) · 14.6 KB
/
environment.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
'use strict';
const fs = require('fs');
const path = require('path');
const EventEmitter = require('events');
const chalk = require('chalk');
const _ = require('lodash');
const GroupedQueue = require('grouped-queue');
const escapeStrRe = require('escape-string-regexp');
const untildify = require('untildify');
const memFs = require('mem-fs');
const debug = require('debug')('yeoman:environment');
const Store = require('./store');
const resolver = require('./resolver');
const TerminalAdapter = require('./adapter');
/**
* `Environment` object is responsible of handling the lifecyle and bootstrap
* of generators in a specific environment (your app).
*
* It provides a high-level API to create and run generators, as well as further
* tuning where and how a generator is resolved.
*
* An environment is created using a list of `arguments` and a Hash of
* `options`. Usually, this is the list of arguments you get back from your CLI
* options parser.
*
* An optional adapter can be passed to provide interaction in non-CLI environment
* (e.g. IDE plugins), otherwise a `TerminalAdapter` is instantiated by default
*
* @constructor
* @mixes env/resolver
* @param {String|Array} args
* @param {Object} opts
* @param {TerminalAdapter} [adaper] - A TerminalAdapter instance or another object
* implementing this adapter interface. This is how
* you'd interface Yeoman with a GUI or an editor.
*/
class Environment extends EventEmitter {
static get queues() {
return [
'initializing',
'prompting',
'configuring',
'default',
'writing',
'conflicts',
'install',
'end'
];
}
/**
* Make sure the Environment present expected methods if an old version is
* passed to a Generator.
* @param {Environment} env
* @return {Environment} The updated env
*/
static enforceUpdate(env) {
if (!env.adapter) {
env.adapter = new TerminalAdapter();
}
if (!env.runLoop) {
env.runLoop = new GroupedQueue([
'initializing',
'prompting',
'configuring',
'default',
'writing',
'conflicts',
'install',
'end'
]);
}
if (!env.sharedFs) {
env.sharedFs = memFs.create();
}
return env;
}
/**
* Factory method to create an environment instance. Take same parameters as the
* Environment constructor.
*
* @see This method take the same arguments as {@link Environment} constructor
*
* @return {Environment} a new Environment instance
*/
static createEnv(args, opts, adapter) {
return new Environment(args, opts, adapter);
}
/**
* Convert a generators namespace to its name
*
* @param {String} namespace
* @return {String}
*/
static namespaceToName(namespace) {
return namespace.split(':')[0];
}
constructor(args, opts, adapter) {
super();
args = args || [];
this.arguments = Array.isArray(args) ? args : args.split(' ');
this.options = opts || {};
this.adapter = adapter || new TerminalAdapter();
this.cwd = this.options.cwd || process.cwd();
this.store = new Store();
this.runLoop = new GroupedQueue(Environment.queues);
this.sharedFs = memFs.create();
// Each composed generator might set listeners on these shared resources. Let's make sure
// Node won't complain about event listeners leaks.
this.runLoop.setMaxListeners(0);
this.sharedFs.setMaxListeners(0);
this.lookups = ['.', 'generators', 'lib/generators'];
this.aliases = [];
this.alias(/^([^:]+)$/, '$1:app');
}
/**
* Error handler taking `err` instance of Error.
*
* The `error` event is emitted with the error object, if no `error` listener
* is registered, then we throw the error.
*
* @param {Object} err
* @return {Error} err
*/
error(err) {
err = err instanceof Error ? err : new Error(err);
if (!this.emit('error', err)) {
throw err;
}
return err;
}
/**
* Outputs the general help and usage. Optionally, if generators have been
* registered, the list of available generators is also displayed.
*
* @param {String} name
*/
help(name) {
name = name || 'init';
const out = [
'Usage: :binary: GENERATOR [args] [options]',
'',
'General options:',
' --help # Print generator\'s options and usage',
' -f, --force # Overwrite files that already exist',
'',
'Please choose a generator below.',
''
];
const ns = this.namespaces();
const groups = {};
for (const namespace of ns) {
const base = namespace.split(':')[0];
if (!groups[base]) {
groups[base] = [];
}
groups[base].push(namespace);
}
for (const key of Object.keys(groups).sort()) {
const group = groups[key];
if (group.length >= 1) {
out.push('', key.charAt(0).toUpperCase() + key.slice(1));
}
for (const ns of groups[key]) {
out.push(` ${ns}`);
}
}
return out.join('\n').replace(/:binary:/g, name);
}
/**
* Registers a specific `generator` to this environment. This generator is stored under
* provided namespace, or a default namespace format if none if available.
*
* @param {String} name - Filepath to the a generator or a npm package name
* @param {String} namespace - Namespace under which register the generator (optional)
* @return {String} namespace - Namespace assigned to the registered generator
*/
register(name, namespace) {
if (typeof name !== 'string') {
return this.error(new Error('You must provide a generator name to register.'));
}
const modulePath = this.resolveModulePath(name);
namespace = namespace || this.namespace(modulePath);
if (!namespace) {
return this.error(new Error('Unable to determine namespace.'));
}
this.store.add(namespace, modulePath);
debug('Registered %s (%s)', namespace, modulePath);
return this;
}
/**
* Register a stubbed generator to this environment. This method allow to register raw
* functions under the provided namespace. `registerStub` will enforce the function passed
* to extend the Base generator automatically.
*
* @param {Function} Generator - A Generator constructor or a simple function
* @param {String} namespace - Namespace under which register the generator
* @return {this}
*/
registerStub(Generator, namespace) {
if (typeof Generator !== 'function') {
return this.error(new Error('You must provide a stub function to register.'));
}
if (typeof namespace !== 'string') {
return this.error(new Error('You must provide a namespace to register.'));
}
this.store.add(namespace, Generator);
return this;
}
/**
* Returns the list of registered namespace.
* @return {Array}
*/
namespaces() {
return this.store.namespaces();
}
/**
* Returns stored generators meta
* @return {Object}
*/
getGeneratorsMeta() {
return this.store.getGeneratorsMeta();
}
/**
* Get registered generators names
*
* @return {Array}
*/
getGeneratorNames() {
return _.uniq(Object.keys(this.getGeneratorsMeta()).map(Environment.namespaceToName));
}
/**
* Get a single generator from the registered list of generators. The lookup is
* based on generator's namespace, "walking up" the namespaces until a matching
* is found. Eg. if an `angular:common` namespace is registered, and we try to
* get `angular:common:all` then we get `angular:common` as a fallback (unless
* an `angular:common:all` generator is registered).
*
* @param {String} namespaceOrPath
* @return {Generator|null} - the generator registered under the namespace
*/
get(namespaceOrPath) {
// Stop the recursive search if nothing is left
if (!namespaceOrPath) {
return;
}
let namespace = namespaceOrPath;
// Legacy yeoman-generator `#hookFor()` function is passing the generator path as part
// of the namespace. If we find a path delimiter in the namespace, then ignore the
// last part of the namespace.
const parts = namespaceOrPath.split(':');
const maybePath = _.last(parts);
if (parts.length > 1 && /[/\\]/.test(maybePath)) {
parts.pop();
// We also want to remove the drive letter on windows
if (maybePath.indexOf('\\') >= 0 && _.last(parts).length === 1) {
parts.pop();
}
namespace = parts.join(':');
}
return this.store.get(namespace) ||
this.store.get(this.alias(namespace)) ||
// Namespace is empty if namespaceOrPath contains a win32 absolute path of the form 'C:\path\to\generator'.
// for this reason we pass namespaceOrPath to the getByPath function.
this.getByPath(namespaceOrPath);
}
/**
* Get a generator by path instead of namespace.
* @param {String} path
* @return {Generator|null} - the generator found at the location
*/
getByPath(path) {
if (fs.existsSync(path)) {
const namespace = this.namespace(path);
this.register(path, namespace);
return this.get(namespace);
}
}
/**
* Create is the Generator factory. It takes a namespace to lookup and optional
* hash of options, that lets you define `arguments` and `options` to
* instantiate the generator with.
*
* An error is raised on invalid namespace.
*
* @param {String} namespace
* @param {Object} options
*/
create(namespace, options) {
options = options || {};
const Generator = this.get(namespace);
if (typeof Generator !== 'function') {
return this.error(
new Error(
chalk.red('You don\'t seem to have a generator with the name “' + namespace + '” installed.') + '\n' +
'But help is on the way:\n\n' +
'You can see available generators via ' +
chalk.yellow('npm search yeoman-generator') + ' or via ' + chalk.yellow('http://yeoman.io/generators/') + '. \n' +
'Install them with ' + chalk.yellow('npm install generator-' + namespace) + '.\n\n' +
'To see all your installed generators run ' + chalk.yellow('yo') + ' without any arguments. ' +
'Adding the ' + chalk.yellow('--help') + ' option will also show subgenerators. \n\n' +
'If ' + chalk.yellow('yo') + ' cannot find the generator, run ' + chalk.yellow('yo doctor') + ' to troubleshoot your system.'
)
);
}
return this.instantiate(Generator, options);
}
/**
* Instantiate a Generator with metadatas
*
* @param {String} namespace
* @param {Object} options
* @param {Array|String} options.arguments Arguments to pass the instance
* @param {Object} options.options Options to pass the instance
*/
instantiate(Generator, options) {
options = options || {};
let args = options.arguments || options.args || _.clone(this.arguments);
args = Array.isArray(args) ? args : args.split(' ');
const opts = options.options || _.clone(this.options);
opts.env = this;
opts.resolved = Generator.resolved || 'unknown';
opts.namespace = Generator.namespace;
return new Generator(args, opts);
}
/**
* Tries to locate and run a specific generator. The lookup is done depending
* on the provided arguments, options and the list of registered generators.
*
* When the environment was unable to resolve a generator, an error is raised.
*
* @param {String|Array} args
* @param {Object} options
* @param {Function} done
*/
run(args, options, done) {
args = args || this.arguments;
if (typeof options === 'function') {
done = options;
options = this.options;
}
if (typeof args === 'function') {
done = args;
options = this.options;
args = this.arguments;
}
args = Array.isArray(args) ? args : args.split(' ');
options = options || this.options;
const name = args.shift();
if (!name) {
return this.error(new Error('Must provide at least one argument, the generator namespace to invoke.'));
}
const generator = this.create(name, {
args,
options
});
if (generator instanceof Error) {
return generator;
}
if (options.help) {
return console.log(generator.help());
}
return generator.run(done);
}
/**
* Given a String `filepath`, tries to figure out the relative namespace.
*
* ### Examples:
*
* this.namespace('backbone/all/index.js');
* // => backbone:all
*
* this.namespace('generator-backbone/model');
* // => backbone:model
*
* this.namespace('backbone.js');
* // => backbone
*
* this.namespace('generator-mocha/backbone/model/index.js');
* // => mocha:backbone:model
*
* @param {String} filepath
*/
namespace(filepath) {
if (!filepath) {
throw new Error('Missing namespace');
}
// Cleanup extension and normalize path for differents OS
let ns = path.normalize(filepath.replace(new RegExp(escapeStrRe(path.extname(filepath)) + '$'), ''));
// Sort lookups by length so biggest are removed first
const lookups = _(this.lookups.concat(['..'])).map(path.normalize).sortBy('length').value().reverse();
// If `ns` contains a lookup dir in its path, remove it.
ns = lookups.reduce((ns, lookup) => {
// Only match full directory (begin with leading slash or start of input, end with trailing slash)
lookup = new RegExp(`(?:\\\\|/|^)${escapeStrRe(lookup)}(?=\\\\|/)`, 'g');
return ns.replace(lookup, '');
}, ns);
const folders = ns.split(path.sep);
const scope = _.findLast(folders, folder => folder.indexOf('@') === 0);
// Cleanup `ns` from unwanted parts and then normalize slashes to `:`
ns = ns
.replace(/(.*generator-)/, '') // Remove before `generator-`
.replace(/[/\\](index|main)$/, '') // Remove `/index` or `/main`
.replace(/^[/\\]+/, '') // Remove leading `/`
.replace(/[/\\]+/g, ':'); // Replace slashes by `:`
if (scope) {
ns = `${scope}/${ns}`;
}
debug('Resolve namespaces for %s: %s', filepath, ns);
return ns;
}
/**
* Resolve a module path
* @param {String} moduleId - Filepath or module name
* @return {String} - The resolved path leading to the module
*/
resolveModulePath(moduleId) {
if (moduleId[0] === '.') {
moduleId = path.resolve(moduleId);
}
if (path.extname(moduleId) === '') {
moduleId += path.sep;
}
return require.resolve(untildify(moduleId));
}
}
Object.assign(Environment.prototype, resolver);
/**
* Expose the utilities on the module
* @see {@link env/util}
*/
Environment.util = require('./util/util');
module.exports = Environment;