Code coverage report for yeoman-generator/lib/env/resolver.js

Statements: 82.61% (38 / 46)      Branches: 50% (6 / 12)      Functions: 84.62% (11 / 13)      Lines: 84.44% (38 / 45)      Ignored: none     

All files » yeoman-generator/lib/env/ » resolver.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  1 1 1 1 1 1                               1 8 8   8 24 216       8 216 96                         1 8 8   8 16 16 40 40       8 40 40 72   40     8               1 96 96 96                                                                       1 13               13   13       13                
'use strict';
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
var glob = require('glob');
var findup = require('findup-sync');
var debug = require('debug')('generators:environment');
 
/**
 * Search for generators and their sub generators.
 *
 * A generator is a `:lookup/:name/index.js` file placed inside an NPM module.
 *
 * Defaults lookups are:
 *   - ./
 *   - generators/
 *   - lib/generators/
 *
 * So this index file `node_modules/generator-dummy/lib/generators/yo/index.js` would be
 * registered as `dummy:yo` generator.
 */
 
exports.lookup = function () {
  var generatorsModules = this._getNpmGenerators();
  var patterns = [];
 
  this.lookups.forEach(function (lookup) {
    generatorsModules.forEach(function (modulePath) {
      patterns.push(path.join(modulePath, lookup) + '/*/index.js');
    });
  });
 
  patterns.forEach(function (pattern) {
    glob.sync(pattern).forEach(function (filename) {
      this._tryRegistering(filename);
    }, this);
  }, this);
};
 
/**
 * Search NPM for every available generators.
 * Generators are NPM modules who's name start with `generator-` and who're placed in the
 * top level `node_module` path. They can be installed globally or locally.
 *
 * @return {Array} List of the generators path
 */
 
exports._getNpmGenerators = function () {
  var modules = [];
  var nodeModules = [];
 
  this.paths.forEach(function (root) {
    var found = findup('node_modules/', { cwd: root });
    while (found && found !== path.dirname(found)) {
      nodeModules.push(found);
      found = findup('node_modules/', { cwd: path.dirname(path.dirname(found)) });
    }
  });
 
  nodeModules.forEach(function (root) {
    Iif (!root) return;
    var found = glob.sync('generator-*', { cwd: root, stat: true }).map(function (match) {
      return fs.realpathSync(path.join(root, match));
    });
    modules = found.concat(modules);
  });
 
  return modules;
};
 
/**
 * Try registering a Generator to this environment.
 * @param  {String} generatorReference A generator reference, usually a file path.
 */
 
exports._tryRegistering = function (generatorReference) {
  try {
    debug('found %s, trying to register', generatorReference);
    this.register(generatorReference);
  } catch (e) {
    console.error('Unable to register %s (Error: %s)', generatorReference, e.message);
  }
};
 
/**
 * Get or create an alias.
 *
 * Alias allows the `get()` and `lookup()` methods to search in alternate
 * filepath for a given namespaces. It's used for example to map `generator-*`
 * npm package to their namespace equivalent (without the generator- prefix),
 * or to default a single namespace like `angular` to `angular:app` or
 * `angular:all`.
 *
 * Given a single argument, this method acts as a getter. When both name and
 * value are provided, acts as a setter and registers that new alias.
 *
 * If multiple alias are defined, then the replacement is recursive, replacing
 * each alias in reverse order.
 *
 * An alias can be a single String or a Regular Expression. The finding is done
 * based on .match().
 *
 * ### Examples:
 *
 *     env.alias(/^([a-zA-Z0-9:\*]+)$/, 'generator-$1');
 *     env.alias(/^([^:]+)$/, '$1:app');
 *     env.alias(/^([^:]+)$/, '$1:all');
 *     env.alias('foo');
 *     // => generator-foo:all
 *
 * @param {String|RegExp} match
 * @param {String} value
 */
 
exports.alias = function alias(match, value) {
  Iif (match && value) {
    this.aliases.push({
      match: match instanceof RegExp ? match : new RegExp('^' + match + '$'),
      value: value
    });
    return this;
  }
 
  var aliases = this.aliases.slice(0).reverse();
 
  var matcher = aliases.filter(function (alias) {
    return alias.match.test(match);
  });
 
  return aliases.reduce(function (res, alias) {
    if (!alias.match.test(res)) {
      return res;
    }
 
    return res.replace(alias.match, alias.value);
  }, match);
};