embark-area-51/lib/modules/library_manager/npm.js

59 lines
1.8 KiB
JavaScript
Raw Normal View History

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;
require('colors');
2018-07-27 16:30:15 +00:00
const NpmTimer = require('./npmTimer.js');
2018-04-20 21:17:49 +00:00
class Npm {
constructor(options) {
this._logger = options.logger;
this._packageName = options.packageName;
this._version = options.version;
this._installing = {};
}
static getPackagePath(packageName, version){
return './.embark/versions/' + packageName + '/' + version + '/' + packageName;
}
_isInstalling(packageName, version){
return typeof this._installing[packageName + version] != 'undefined';
}
2018-04-20 21:17:49 +00:00
getPackageVersion(packageName, version, callback) {
const packagePath = Npm.getPackagePath(packageName, version);
2018-04-20 21:17:49 +00:00
// check if this package already exists in the filesystem
if (fs.existsSync(packagePath)) {
return callback(null, packagePath);
}
const pluginManager = new PluginManager({pluginsPath: './.embark/versions/' + packageName + '/' + version + '/'});
// check if we're already installing this package
if(this._isInstalling(packageName, version)){
this._installing[packageName + version].push(callback);
}else{
this._installing[packageName + version] = [callback];
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 => {
this._installing[packageName + version].forEach((cb) => {
cb(err);
});
});
}
}
}
module.exports = Npm;