2018-07-20 18:28:46 +00:00
|
|
|
const fs = require('../../core/fs.js');
|
2018-06-13 13:40:19 +00:00
|
|
|
const PluginManager = require('live-plugin-manager-git-fix').PluginManager;
|
2018-06-05 07:15:21 +00:00
|
|
|
require('colors');
|
2018-06-14 08:09:02 +00:00
|
|
|
const NpmTimer = require('./npmTimer');
|
2018-04-20 21:17:49 +00:00
|
|
|
|
2017-07-05 22:26:44 +00:00
|
|
|
class Npm {
|
|
|
|
|
|
|
|
constructor(options) {
|
2018-06-14 08:09:02 +00:00
|
|
|
this._logger = options.logger;
|
|
|
|
this._packageName = options.packageName;
|
|
|
|
this._version = options.version;
|
|
|
|
this._installing = {};
|
2017-07-05 22:26:44 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 08:09:02 +00:00
|
|
|
static getPackagePath(packageName, version){
|
|
|
|
return './.embark/versions/' + packageName + '/' + version + '/' + packageName;
|
2018-06-13 13:16:54 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 08:09:02 +00:00
|
|
|
_isInstalling(packageName, version){
|
|
|
|
return typeof this._installing[packageName + version] != 'undefined';
|
2018-06-13 13:16:54 +00:00
|
|
|
}
|
2018-04-20 21:17:49 +00:00
|
|
|
|
2018-06-13 13:16:54 +00:00
|
|
|
getPackageVersion(packageName, version, callback) {
|
2018-06-14 08:09:02 +00:00
|
|
|
const packagePath = Npm.getPackagePath(packageName, version);
|
2018-04-20 21:17:49 +00:00
|
|
|
|
2018-06-14 08:09:02 +00:00
|
|
|
// check if this package already exists in the filesystem
|
|
|
|
if (fs.existsSync(packagePath)) {
|
|
|
|
return callback(null, packagePath);
|
2018-04-20 23:45:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 08:09:02 +00:00
|
|
|
const pluginManager = new PluginManager({pluginsPath: './.embark/versions/' + packageName + '/' + version + '/'});
|
2018-06-05 07:15:21 +00:00
|
|
|
|
2018-06-14 08:09:02 +00:00
|
|
|
// check if we're already installing this package
|
|
|
|
if(this._isInstalling(packageName, version)){
|
|
|
|
this._installing[packageName + version].push(callback);
|
|
|
|
}else{
|
2018-06-14 23:37:52 +00:00
|
|
|
this._installing[packageName + version] = [callback];
|
2018-06-14 08:09:02 +00:00
|
|
|
|
|
|
|
const timer = new NpmTimer({logger: this._logger, packageName: packageName, version: version});
|
|
|
|
timer.start();
|
|
|
|
|
|
|
|
// do the package download/install
|
|
|
|
pluginManager.install(packageName, version).then((result) => {
|
|
|
|
timer.end();
|
|
|
|
this._installing[packageName + version].forEach((cb) => {
|
|
|
|
cb(null, result.location);
|
|
|
|
});
|
|
|
|
delete this._installing[packageName + version];
|
|
|
|
}).catch(err => {
|
2018-06-14 23:37:52 +00:00
|
|
|
this._installing[packageName + version].forEach((cb) => {
|
|
|
|
cb(err);
|
|
|
|
});
|
2018-06-14 08:09:02 +00:00
|
|
|
});
|
|
|
|
}
|
2017-07-02 17:33:11 +00:00
|
|
|
}
|
2017-07-05 22:26:44 +00:00
|
|
|
}
|
2017-07-02 17:33:11 +00:00
|
|
|
|
2017-07-05 22:26:44 +00:00
|
|
|
module.exports = Npm;
|