Code coverage report for yeoman-generator/lib/actions/install.js

Statements: 95.35% (41 / 43)      Branches: 79.31% (23 / 29)      Functions: 100% (9 / 9)      Lines: 95.35% (41 / 43)      Ignored: none     

All files » yeoman-generator/lib/actions/ » install.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  1 1 1 1   1                     1 10 2 2     10 10 10   10 10   10       10       10     10                                                       1 4               4   4 1         4               4 4 4 3       4 4 4 3       4       4 4     4 3                       1 6                     1 4    
'use strict';
var _ = require('lodash');
var dargs = require('dargs');
var async = require('async');
var chalk = require('chalk');
 
var install = module.exports;
 
/**
 * Combine package manager cmd line arguments and run the `install` command.
 *
 * @param {String} installer Which package manager to use
 * @param {String|Array} paths Packages to install, empty string for `npm install`
 * @param {Object} options Options to invoke `install` with
 * @param {Function} cb
 */
 
install.runInstall = function (installer, paths, options, cb) {
  if (!cb && _.isFunction(options)) {
    cb = options;
    options = {};
  }
 
  options = options || {};
  cb = cb || function () {};
  paths = Array.isArray(paths) ? paths : (paths && paths.split(' ') || []);
 
  this.emit(installer + 'Install', paths);
  var args = ['install'].concat(paths).concat(dargs(options));
 
  this.spawnCommand(installer, args, cb)
    .on('error', cb)
    .on('exit', this.emit.bind(this, installer + 'Install:end', paths))
    .on('exit', function (err) {
      Iif (err === 127) {
        this.log.error('Could not find ' + installer + '. Please install with ' +
                            '`npm install -g ' + installer + '`.');
      }
      cb(err);
    }.bind(this));
 
  return this;
};
 
/**
 * Runs `npm` and `bower` in the generated directory concurrently and prints a
 * message to let the user know.
 *
 * ### Options:
 *
 *   - `npm` Boolean whether to run `npm install` (`true`)
 *   - `bower` Boolean whether to run `bower install` (`true`)
 *   - `skipInstall` Boolean whether to skip automatic installation (`false`)
 *   - `skipMessage` Boolean whether to show the used bower/npm commands (`false`)
 *
 * ### Examples:
 *
 *     this.installDependencies({
 *       bower: true,
 *       npm: true,
 *       skipInstall: false,
 *       callback: function () {
 *         console.log('Everything is ready!');
 *       }
 *     });
 *
 * @param {Object} options
 */
 
install.installDependencies = function (options) {
  var msg = {
    commands: [],
    template: _.template('\n\nI\'m all done. ' +
    '<%= skipInstall ? "Just run" : "Running" %> <%= commands %> ' +
    '<%= skipInstall ? "" : "for you " %>to install the required dependencies.' +
    '<% if (!skipInstall) { %> If this fails, try running the command yourself.<% } %>\n\n')
  };
 
  var commands = [];
 
  if (_.isFunction(options)) {
    options = {
      callback: options
    };
  }
 
  options = _.defaults(options || {}, {
    bower: true,
    npm: true,
    skipInstall: false,
    skipMessage: false,
    callback: function () {}
  });
 
  Eif (options.bower) {
    msg.commands.push('bower install');
    commands.push(function (cb) {
      this.bowerInstall(null, null, cb);
    }.bind(this));
  }
 
  Eif (options.npm) {
    msg.commands.push('npm install');
    commands.push(function (cb) {
      this.npmInstall(null, null, cb);
    }.bind(this));
  }
 
  Iif (msg.commands.length === 0) {
    throw new Error('installDependencies needs at least one of npm or bower to run.');
  }
 
  Eif (!options.skipMessage) {
    console.log(msg.template(_.extend(options, { commands: chalk.yellow.bold(msg.commands.join(' & ')) })));
  }
 
  if (!options.skipInstall) {
    async.parallel(commands, options.callback);
  }
};
 
/**
 * Receives a list of `paths`, and an Hash of `options` to install through bower.
 *
 * @param {String|Array} paths Packages to install
 * @param {Object} options Options to invoke `bower install` with, see `bower help install`
 * @param {Function} cb
 */
 
install.bowerInstall = function install(paths, options, cb) {
  return this.runInstall('bower', paths, options, cb);
};
 
/**
 * Receives a list of `paths`, and an Hash of `options` to install through npm.
 *
 * @param {String|Array} paths Packages to install
 * @param {Object} options Options to invoke `npm install` with, see `npm help install`
 * @param {Function} cb
 */
 
install.npmInstall = function install(paths, options, cb) {
  return this.runInstall('npm', paths, options, cb);
};