npm.js
 1  const fs = require('../../core/fs.js');
 2  const PluginManager = require('live-plugin-manager-git-fix').PluginManager;
 3  require('colors');
 4  const NpmTimer = require('./npmTimer.js');
 5  
 6  class Npm {
 7  
 8    constructor(options) {
 9      this._logger = options.logger;
10      this._packageName = options.packageName;
11      this._version = options.version;
12      this._installing = {};
13    }
14  
15    static getPackagePath(packageName, version){
16      return './.embark/versions/' + packageName + '/' + version + '/' + packageName;
17    }
18  
19    _isInstalling(packageName, version){
20      return typeof this._installing[packageName + version] !== 'undefined';
21    }
22  
23    getPackageVersion(packageName, version, callback) {
24      const packagePath = Npm.getPackagePath(packageName, version);
25  
26      // check if this package already exists in the filesystem
27      if (fs.existsSync(packagePath)) {
28        return callback(null, packagePath);
29      }
30  
31      const pluginManager = new PluginManager({pluginsPath: './.embark/versions/' + packageName + '/' + version + '/'});
32      
33      // check if we're already installing this package
34      if(this._isInstalling(packageName, version)){
35        this._installing[packageName + version].push(callback);
36      }else{
37        this._installing[packageName + version] = [callback];
38        
39        const timer = new NpmTimer({logger: this._logger, packageName: packageName, version: version});
40        timer.start();
41  
42        // do the package download/install
43        pluginManager.install(packageName, version).then((result) => {        
44            timer.end();
45            this._installing[packageName + version].forEach((cb) => {
46              cb(null, result.location);
47            });
48            delete this._installing[packageName + version];
49        }).catch(err => {
50          this._installing[packageName + version].forEach((cb) => {
51            cb(err);
52          });
53        });
54      }
55    }
56  }
57  
58  module.exports = Npm;