Code coverage report for src/module-install.js

Statements: 80% (32 / 40)      Branches: 50% (3 / 6)      Functions: 60% (3 / 5)      Lines: 80% (32 / 40)      Ignored: none     

All files » src/ » module-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 631 1 1 1   1 1 1   1 1       1   1 1 1   1 1 1   1 1 1 1   1 1   1 1     1       1         1 1 1               1     1     1  
var check = require('check-types');
var spawn = require('child_process').spawn;
var q = require('q');
var NPM_PATH = require('./npm-path');
 
function promiseToInstall(opts) {
  var name = opts.name;
  check.verify.string(name, 'expected module name string');
 
  var moduleVersion = name;
  Iif (opts.version) {
    check.verify.string(opts.version, 'expected version string');
    moduleVersion = moduleVersion + '@' + opts.version;
  }
  console.log('  installing', moduleVersion);
 
  var args = ['install'];
  Eif (opts.prefix) {
    check.verify.string(opts.prefix,
      'install folder prefix should be a string, not ' + opts.prefix);
    args.push('-g');
    args.push('--prefix');
    args.push(opts.prefix);
  }
  args.push(moduleVersion);
  var npm = spawn(NPM_PATH, args);
  var output = '';
  var errors = '';
 
  npm.stdout.setEncoding('utf-8');
  npm.stderr.setEncoding('utf-8');
 
  npm.stdout.on('data', function (data) {
    output += data;
  });
 
  npm.stderr.on('data', function (data) {
    errors += data;
  });
 
  npm.on('error', function (err) {
    console.error(err);
    errors += err.toString();
  });
 
  var deferred = q.defer();
  npm.on('exit', function (code) {
    Iif (code) {
      console.error('npm returned', code);
      console.error('errors:\n' + errors);
      deferred.reject({
        code: code,
        errors: errors
      });
    } else {
      deferred.resolve(opts.passThroughData);
    }
  });
  return deferred.promise;
}
 
module.exports = promiseToInstall;