All files / util install-dependencies.js

100% Statements 31/31
90.91% Branches 20/22
100% Functions 5/5
100% Lines 23/23
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          12x     39x 39x 1x 1x   38x 38x 32x 32x 32x   6x 6x 6x   38x 38x 38x       38x 38x 1406x 48x   38x 30x 29x        
import debug from 'debug';
import { spawn as yarnOrNPMSpawn, hasYarn } from 'yarn-or-npm';
 
import config from './config';
 
const d = debug('electron-forge:dependency-installer');
 
export default (dir, deps, areDev = false, exact = false) => {
  d('installing', JSON.stringify(deps), 'in:', dir, `dev=${areDev},exact=${exact},withYarn=${hasYarn()}`);
  if (deps.length === 0) {
    d('nothing to install, stopping immediately');
    return Promise.resolve();
  }
  let cmd = ['install'].concat(deps);
  if (hasYarn()) {
    cmd = ['add'].concat(deps);
    if (areDev) cmd.push('--dev');
    if (exact) cmd.push('--exact');
  } else {
    if (exact) cmd.push('--save-exact');
    if (areDev) cmd.push('--save-dev');
    if (!areDev) cmd.push('--save');
  }
  return new Promise((resolve, reject) => {
    d('executing', JSON.stringify(cmd), 'in:', dir);
    const child = yarnOrNPMSpawn(cmd, {
      cwd: dir,
      stdio: config.get('verbose') ? 'inherit' : 'pipe',
    });
    let output = '';
    Eif (!config.get('verbose')) {
      child.stdout.on('data', (data) => { output += data; });
      child.stderr.on('data', (data) => { output += data; });
    }
    child.on('exit', (code) => {
      if (code !== 0) return reject(new Error(`Failed to install modules: ${JSON.stringify(deps)}\n\nWith output: ${output}`));
      resolve();
    });
  });
};