Blame view

node_modules/bower/lib/renderers/StandardRenderer.js 13.5 KB
2dda2e10   Administrator   generator ignore
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
  var chalk = require('chalk');
  var path = require('path');
  var mout = require('mout');
  var archy = require('archy');
  var Q = require('q');
  var stringifyObject = require('stringify-object');
  var os = require('os');
  var semverUtils = require('semver-utils');
  var version = require('../version');
  var template = require('../util/template');
  
  function StandardRenderer(command, config) {
      this._sizes = {
          id: 13,    // Id max chars
          label: 20, // Label max chars
          sumup: 5   // Amount to sum when the label exceeds
      };
      this._colors = {
          warn: chalk.yellow,
          error: chalk.red,
          conflict: chalk.magenta,
          debug: chalk.gray,
          default: chalk.cyan
      };
  
      this._command = command;
      this._config = config || {};
  
      if (this.constructor._wideCommands.indexOf(command) === -1) {
          this._compact = true;
      } else {
          this._compact = process.stdout.columns < 120;
      }
  
      var exitOnPipeError = function (err) {
          if (err.code === 'EPIPE') {
              process.exit(0);
          }
      };
  
      // It happens when piping command to "head" util
      process.stdout.on('error', exitOnPipeError);
      process.stderr.on('error', exitOnPipeError);
  }
  
  StandardRenderer.prototype.end = function (data) {
      var method = '_' + mout.string.camelCase(this._command);
  
      if (this[method]) {
          this[method](data);
      }
  };
  
  StandardRenderer.prototype.error = function (err) {
      var str;
      var stack;
  
      this._guessOrigin(err);
  
      err.id = err.code || 'error';
      err.level = 'error';
  
      str = this._prefix(err) + ' ' + err.message.replace(/\r?\n/g, ' ').trim() + '\n';
      this._write(process.stderr, 'bower ' + str);
  
      // Check if additional details were provided
      if (err.details) {
          str = chalk.yellow('\nAdditional error details:\n') + err.details.trim() + '\n';
          this._write(process.stderr, str);
      }
  
      // Print trace if verbose, the error has no code
      // or if the error is a node error
      if (this._config.verbose || !err.code || err.errno) {
          stack = err.fstream_stack || err.stack || 'N/A';
          str = chalk.yellow('\nStack trace:\n');
          str += (Array.isArray(stack) ? stack.join('\n') : stack) + '\n';
          str += chalk.yellow('\nConsole trace:\n');
  
          this._write(process.stderr, str);
          this._write(process.stderr, new Error().stack);
  
          // Print bower version, node version and system info.
          this._write(process.stderr, chalk.yellow('\nSystem info:\n'));
          this._write(process.stderr, 'Bower version: ' + version + '\n');
          this._write(process.stderr, 'Node version: ' + process.versions.node + '\n');
          this._write(process.stderr, 'OS: ' + os.type() + ' ' + os.release() + ' ' + os.arch() + '\n');
      }
  };
  
  StandardRenderer.prototype.log = function (log) {
      var method = '_' + mout.string.camelCase(log.id) + 'Log';
  
      this._guessOrigin(log);
  
      // Call render method for this log entry or the generic one
      if (this[method]) {
          this[method](log);
      } else {
          this._genericLog(log);
      }
  };
  
  StandardRenderer.prototype.prompt = function (prompts) {
      var deferred;
  
      // Strip colors from the prompt if color is disabled
      if (!this._config.color) {
          prompts.forEach(function (prompt) {
              prompt.message = chalk.stripColor(prompt.message);
          });
      }
  
      // Prompt
      deferred = Q.defer();
      var inquirer = require('inquirer');
      inquirer.prompt(prompts, deferred.resolve);
  
      return deferred.promise;
  };
  
  // -------------------------
  
  StandardRenderer.prototype._help = function (data) {
      var str;
      var that = this;
      var specific;
  
      if (!data.command) {
          str = template.render('std/help.std', data);
          that._write(process.stdout, str);
      } else {
          // Check if a specific template exists for the command
          specific = 'std/help-' + data.command.replace(/\s+/g, '/') + '.std';
  
          if (template.exists(specific)) {
              str = template.render(specific, data);
          } else {
              str =  template.render('std/help-generic.std', data);
          }
  
          that._write(process.stdout, str);
      }
  };
  
  StandardRenderer.prototype._install = function (packages) {
      var str = '';
  
      mout.object.forOwn(packages, function (pkg) {
          var cliTree;
  
          // List only 1 level deep dependencies
          mout.object.forOwn(pkg.dependencies, function (dependency) {
              dependency.dependencies = {};
          });
          // Make canonical dir relative
          pkg.canonicalDir = path.relative(this._config.cwd, pkg.canonicalDir);
          // Signal as root
          pkg.root = true;
  
          cliTree = this._tree2archy(pkg);
          str += '\n' + archy(cliTree);
      }, this);
  
      if (str) {
          this._write(process.stdout, str);
      }
  };
  
  StandardRenderer.prototype._update = function (packages) {
      this._install(packages);
  };
  
  StandardRenderer.prototype._list = function (tree) {
      var cliTree;
  
      if (tree.pkgMeta) {
          tree.root = true;
          cliTree = archy(this._tree2archy(tree));
      } else {
          cliTree = stringifyObject(tree, { indent: '  ' }).replace(/[{}]/g, '') + '\n';
      }
  
      this._write(process.stdout, cliTree);
  };
  
  StandardRenderer.prototype._search = function (results) {
      var str = template.render('std/search-results.std', results);
      this._write(process.stdout, str);
  };
  
  StandardRenderer.prototype._info = function (data) {
      var str = '';
      var pkgMeta = data;
      var includeVersions = false;
  
      // If the response is the whole package info, the package meta
      // is under the "latest" property
      if (typeof data === 'object' && data.versions) {
          pkgMeta = data.latest;
          includeVersions = true;
      }
  
      // Render package meta
      if (pkgMeta != null) {
          str += '\n' + this._highlightJson(pkgMeta) + '\n';
      }
  
      // Render the versions at the end
      if (includeVersions) {
          data.hidePreReleases = false;
          data.numPreReleases = 0;
          // If output isn't verbose, hide prereleases
          if (!this._config.verbose) {
              data.versions = mout.array.filter(data.versions, function (version) {
                  version = semverUtils.parse(version);
                  if (!version.release && !version.build) {
                      return true;
                  }
                  data.numPreReleases++;
              });
              data.hidePreReleases = !!data.numPreReleases;
          }
          str += '\n' + template.render('std/info.std', data);
      }
  
      this._write(process.stdout, str);
  };
  
  StandardRenderer.prototype._lookup = function (data) {
      var str = template.render('std/lookup.std', data);
  
      this._write(process.stdout, str);
  };
  
  StandardRenderer.prototype._link = function (data) {
      this._sizes.id = 4;
  
      this.log({
          id: 'link',
          level: 'info',
          message: data.dst + ' > ' + data.src
      });
  
      // Print also a tree of the installed packages
      if (data.installed) {
          this._install(data.installed);
      }
  };
  
  StandardRenderer.prototype._register = function (data) {
      var str;
  
      // If no data passed, it means the user aborted
      if (!data) {
          return;
      }
  
      str = '\n' + template.render('std/register.std', data);
      this._write(process.stdout, str);
  };
  
  StandardRenderer.prototype._cacheList = function (entries) {
      entries.forEach(function (entry) {
          var pkgMeta = entry.pkgMeta;
          var version = pkgMeta.version || pkgMeta._target;
          this._write(process.stdout, pkgMeta.name + '=' + pkgMeta._source + '#' + version + '\n');
      }, this);
  };
  
  // -------------------------
  
  StandardRenderer.prototype._genericLog = function (log) {
      var stream;
      var str;
  
      if (log.level === 'warn') {
          stream = process.stderr;
      } else {
          stream = process.stdout;
      }
  
      str = this._prefix(log) + ' ' + log.message + '\n';
      this._write(stream, 'bower ' + str);
  };
  
  StandardRenderer.prototype._checkoutLog = function (log) {
      if (this._compact) {
          log.message = log.origin.split('#')[0] + '#' + log.message;
      }
  
      this._genericLog(log);
  };
  
  StandardRenderer.prototype._progressLog = function (log) {
      if (this._compact) {
          log.message = log.origin + ' ' + log.message;
      }
  
      this._genericLog(log);
  };
  
  StandardRenderer.prototype._extractLog = function (log) {
      if (this._compact) {
          log.message = log.origin + ' ' + log.message;
      }
  
      this._genericLog(log);
  };
  
  StandardRenderer.prototype._incompatibleLog = function (log) {
      var str;
      var templatePath;
  
      // Generate dependants string for each pick
      log.data.picks.forEach(function (pick) {
          pick.dependants = pick.dependants.map(function (dependant) {
              var release = dependant.pkgMeta._release;
              return dependant.endpoint.name + (release ? '#' + release : '');
          }).join(', ');
      });
  
      templatePath = log.data.suitable ? 'std/conflict-resolved.std' : 'std/conflict.std';
      str = template.render(templatePath, log.data);
  
      this._write(process.stdout, '\n');
      this._write(process.stdout, str);
      this._write(process.stdout, '\n');
  };
  
  StandardRenderer.prototype._solvedLog = function (log) {
      this._incompatibleLog(log);
  };
  
  StandardRenderer.prototype._jsonLog = function (log) {
      this._write(process.stdout, '\n' + this._highlightJson(log.data.json) + '\n\n');
  };
  
  StandardRenderer.prototype._cachedEntryLog = function (log) {
      if (this._compact) {
          log.message = log.origin;
      }
  
      this._genericLog(log);
  };
  
  // -------------------------
  
  StandardRenderer.prototype._guessOrigin = function (log) {
      var data = log.data;
  
      if (!data) {
          return;
      }
  
      if (data.endpoint) {
          log.origin = data.endpoint.name || (data.registry && data.endpoint.source);
  
          // Resort to using the resolver name for unnamed endpoints
          if (!log.origin && data.resolver) {
              log.origin = data.resolver.name;
          }
  
          if (log.origin && data.endpoint.target) {
              log.origin += '#' + data.endpoint.target;
          }
      } else if (data.name) {
          log.origin = data.name;
  
          if (data.version) {
              log.origin += '#' + data.version;
          }
      }
  };
  
  StandardRenderer.prototype._prefix = function (log) {
      var label;
      var length;
      var nrSpaces;
      var id = this.constructor._idMappings[log.id] || log.id;
      var idColor = this._colors[log.level] || this._colors.default;
  
      if (this._compact) {
          // If there's not enough space for the id, adjust it
          // for subsequent logs
          if (id.length > this._sizes.id) {
              this._sizes.id = id.length += this._sizes.sumup;
          }
  
          return idColor(mout.string.rpad(id, this._sizes.id));
      }
  
      // Construct the label
      label = log.origin || '';
      length = id.length + label.length + 1;
      nrSpaces = this._sizes.id + this._sizes.label - length;
  
      // Ensure at least one space between the label and the id
      if (nrSpaces < 1) {
          // Also adjust the label size for subsequent logs
          this._sizes.label = label.length + this._sizes.sumup;
          nrSpaces = this._sizes.id + this._sizes.label - length;
      }
  
      return chalk.green(label) + mout.string.repeat(' ', nrSpaces) + idColor(id);
  };
  
  StandardRenderer.prototype._write = function (stream, str) {
      if (!this._config.color) {
          str = chalk.stripColor(str);
      }
  
      stream.write(str);
  };
  
  StandardRenderer.prototype._highlightJson = function (json) {
      var cardinal = require('cardinal');
  
      return cardinal.highlight(stringifyObject(json, { indent: '  ' }), {
          theme: {
              String: {
                  _default: function (str) {
                      return chalk.cyan(str);
                  }
              },
              Identifier: {
                  _default: function (str) {
                      return chalk.green(str);
                  }
              }
          },
          json: true
      });
  };
  
  StandardRenderer.prototype._tree2archy = function (node) {
      var dependencies = mout.object.values(node.dependencies);
      var version = !node.missing ? node.pkgMeta._release || node.pkgMeta.version : null;
      var label = node.endpoint.name + (version ? '#' + version : '');
      var update;
  
      if (node.root) {
          label += ' ' + node.canonicalDir;
      }
  
      // State labels
      if (node.missing) {
          label += chalk.red(' not installed');
          return label;
      }
  
      if (node.different) {
          label += chalk.red(' different');
      }
  
      if (node.linked) {
          label += chalk.magenta(' linked');
      }
  
      if (node.incompatible) {
          label += chalk.yellow(' incompatible') + ' with ' + node.endpoint.target;
      } else if (node.extraneous) {
          label += chalk.green(' extraneous');
      }
  
      // New versions
      if (node.update) {
          update = '';
  
          if (node.update.target && node.pkgMeta.version !== node.update.target) {
              update += node.update.target + ' available';
          }
  
          if (node.update.latest !== node.update.target) {
              update += (update ? ', ' : '');
              update += 'latest is ' + node.update.latest;
          }
  
          if (update) {
              label += ' (' + chalk.cyan(update) + ')';
          }
      }
  
      if (!dependencies.length) {
          return label;
      }
  
      return {
          label: label,
          nodes: mout.object.values(dependencies).map(this._tree2archy, this)
      };
  };
  
  StandardRenderer._wideCommands = [
      'install',
      'update',
      'link',
      'info',
      'home',
      'register'
  ];
  StandardRenderer._idMappings = {
      'mutual': 'conflict',
      'cached-entry': 'cached'
  };
  
  module.exports = StandardRenderer;