All files / piscosour/plugins/installer index.js

0% Statements 0/51
0% Branches 0/16
0% Functions 0/16
0% Lines 0/50
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                                                                                                                                                                                     
'use strict';
 
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const semver = require('semver');
 
const requirements = require('../../lib/utils/requirements');
const constants = require('../../lib/utils/constants');
 
const relaunchFile = '.relaunch';
 
module.exports = {
 
  'core-install': function() {
    const getRequirement = (pkg, installable) => {
      const cmd = pkg;
      const options = {
        installer: 'npm',
        listedIn: 'npm'
      };
      const father = this.params.versions[options.installer];
 
      if (semver.valid(installable) || semver.validRange(installable)) {
        options.version = installable;
      } else {
        options.uri = installable;
      }
 
      return { cmd, options, father };
    };
 
 
    const installRequirement = (requirement) => {
      const cmd = requirement.cmd;
      const father = requirement.father;
      const options = _.merge({},
        requirement.options,
        requirement.father);
 
      return Promise.resolve()
        .then(() => requirements.sh(cmd, options, father))
        .then(result => requirements.check(cmd, options, result, father))
        .catch((checked) => {
          return this.systemInstall(cmd, options)
            .then(() => requirements.sh(cmd, options, father, true)
              .catch((e) => this.logger.trace('ignored error! ->', e)));
        });
    };
 
 
    const installRequirements = () => {
      const promises = [];
      Object.getOwnPropertyNames(this.requires).forEach((pkg) => {
        const installable = this.requires[pkg];
        const requirement = getRequirement(pkg, installable);
        promises.push(installRequirement(requirement));
      });
      return Promise.all(promises);
    };
 
 
    const relaunch = () => new Promise((ok, ko) => {
      try {
        this.logger.trace('#green', 'writing .relaunch');
        fs.writeFileSync(relaunchFile, '');
        return ok();
      } catch (e) {
        return ko(e);
      }
    });
 
    const flowInstaller = this._flow && !this.piscoConfig.isInstalledFlow(this._context, this._flow);
    const stepInstaller = this.requires && this.requires !== constants.notBuild && !this.installed && flowInstaller;
    this.params._skip = this.params._skip || flowInstaller;
 
    if (stepInstaller) {
      return Promise.resolve()
        .then(() => installRequirements())
        .then(() => relaunch())
        .then(() => this.logger.info('#magenta', 'core-install', '#green', 'step installed'))
        .catch((err) => {
          throw err;
        });
    } else if (flowInstaller) {
      this.logger.info('#magenta', 'core-install', '#green', 'step already installed');
    }
  }
 
};