all files / dist/satellites/ modules.js

75.34% Statements 55/73
57.69% Branches 15/26
86.67% Functions 13/15
71.43% Lines 40/56
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227                        18×                                                                                                                                       18×     18×     18×     18×                                                                                                                                                                                                            
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _fs = require('fs');
 
var _fs2 = _interopRequireDefault(_fs);
 
var _utils = require('../utils');
 
var _utils2 = _interopRequireDefault(_utils);
 
var _child_process = require('child_process');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/**
 * This class is responsible to manage all modules, process
 * the NPM dependencies.
 */
 
var Modules = function () {
 
  /**
   * Create a new class instance.
   *
   * @param api
   */
 
 
  /**
   * Map with the active modules.
   *
   * Keys are the modules slugs and the values are
   * their manifests.
   *
   * @type {Map}
   */
 
  function Modules(api) {
    _classCallCheck(this, Modules);
 
    this.api = null;
    this.activeModules = new Map();
    this.modulesPaths = new Map();
    this.api = api;
  }
 
  /**
   * Load all active modules into memory.
   *
   * The private module is always loaded even if not present on the
   * activeModules property.
   */
 
 
  /**
   * Map with the modules paths.
   *
   * @type {Map}
   */
 
 
  /**
   * API reference object.
   *
   * @type {null}
   */
 
 
  _createClass(Modules, [{
    key: 'loadModules',
    value: function loadModules() {
      var self = this;
 
      // get active modules
      var modules = self.api.config.modules;
 
      // check if the private module folder exists
      Iif (_utils2.default.directoryExists(self.api.scope.rootPath + '/modules/private')) {
        modules.push('private');
      }
 
      // this config is required. If doesn't exists or is an empty array
      // an exception should be raised.
      Iif (modules === undefined || modules.length === 0) {
        next(new Error('At least one module needs to be active.'));
 
        // engine don't finish the starting wet, soo we need to finish the process
        process.exit(1);
      }
 
      // load all modules manifests
      modules.forEach(function (moduleName) {
        // build the full path
        var path = self.api.scope.rootPath + '/modules/' + moduleName;
 
        // get module manifest file content
        var manifest = require(path + '/manifest.json');
 
        // save the module config on the engine instance
        self.activeModules.set(manifest.id, manifest);
 
        // save the module full path
        self.modulesPaths.set(manifest.id, path);
      });
    }
 
    /**
     * Process all NPM dependencies.
     *
     * The npm install command only is executed if the package.json
     * file are not present.
     *
     * @param next    Callback function.
     */
 
  }, {
    key: 'processNpmDependencies',
    value: function processNpmDependencies(next) {
      var self = this;
 
      // if the `package.json` file already exists don't search for NPM dependencies
      Eif (_utils2.default.fileExists(self.api.scope.rootPath + '/package.json')) {
        return next();
      }
 
      // global npm dependencies
      var npmDependencies = {};
 
      // iterate all active modules
      self.activeModules.forEach(function (manifest) {
        // check if the module have NPM dependencies
        if (manifest.npmDependencies !== undefined) {
          // merge the two hashes
          npmDependencies = _utils2.default.hashMerge(npmDependencies, manifest.npmDependencies);
        }
      });
 
      // compile project information
      var projectJson = {
        private: true,
        name: 'stellar-dependencies',
        version: '1.0.0',
        description: 'This is automatically generated don\'t edit',
        dependencies: npmDependencies
      };
 
      // generate project.json file
      _fs2.default.writeFileSync(self.api.scope.rootPath + '/package.json', JSON.stringify(projectJson, null, 2), 'utf8');
 
      self.api.log('updating NPM packages', 'info');
 
      // run npm command
      (0, _child_process.exec)('npm install', function (error) {
        // if an error occurs finish the process
        if (error) {
          self.api.log('An error occurs during the NPM install command', 'emergency');
          process.exit(1);
        }
 
        // load a success message
        self.api.log('NPM dependencies updated!', 'info');
 
        // finish the loading process
        next();
      });
    }
  }]);
 
  return Modules;
}();
 
/**
 * This initializer loads all active modules configs to the
 * engine instance.
 */
 
 
var _class = function () {
  function _class() {
    _classCallCheck(this, _class);
 
    this.loadPriority = 1;
  }
 
  /**
   * Initializer load priority.
   *
   * @type {number}
   */
 
 
  _createClass(_class, [{
    key: 'load',
 
 
    /**
     * Initializer load function.
     *
     * @param api   API reference.
     * @param next  Callback function.
     */
    value: function load(api, next) {
      // instantiate the manager
      api.modules = new Modules(api);
 
      // load modules into memory
      api.modules.loadModules();
 
      // process NPM dependencies
      api.modules.processNpmDependencies(next);
    }
  }]);
 
  return _class;
}();
 
exports.default = _class;
//# sourceMappingURL=modules.js.map