2018-06-05 07:15:21 +00:00
|
|
|
const fs = require('../core/fs.js');
|
|
|
|
const {PerformanceObserver, performance} = require('perf_hooks');
|
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');
|
|
|
|
const _ = require('underscore');
|
2018-04-20 21:17:49 +00:00
|
|
|
|
2017-07-05 22:26:44 +00:00
|
|
|
class Npm {
|
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
this.logger = options.logger;
|
|
|
|
}
|
|
|
|
|
2018-04-20 23:52:51 +00:00
|
|
|
getPackageVersion(packageName, version, callback) {
|
2018-04-20 21:17:49 +00:00
|
|
|
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
|
|
|
|
|
|
|
|
let manager = new PluginManager({pluginsPath: packageDirectory});
|
|
|
|
|
2018-04-20 23:45:57 +00:00
|
|
|
if (fs.existsSync(packageDirectory + packageName)) {
|
|
|
|
return callback(null, packageDirectory + packageName);
|
|
|
|
}
|
|
|
|
|
2018-06-05 07:15:21 +00:00
|
|
|
this.logger.info(__("Downloading {{packageName}} {{version}}...", {packageName: packageName, version: version}));
|
|
|
|
|
|
|
|
const obsMeasure = new PerformanceObserver((items) => {
|
|
|
|
let entry;
|
|
|
|
let strDuration;
|
|
|
|
|
|
|
|
// find any download ongoing measurements we've made
|
|
|
|
entry = _.last(_.where(items.getEntries(), {name: downloadOngoing}));
|
|
|
|
if(entry){
|
|
|
|
// ongoing performance mark
|
|
|
|
strDuration = __('Still downloading {{packageName}} {{version}}... ({{duration}}ms elapsed)', {packageName: packageName, version: version, duration: entry.duration});
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
// otherwise, find our download complete measurement
|
|
|
|
entry = _.last(_.where(items.getEntries(), {name: downloadComplete}));
|
|
|
|
|
|
|
|
if(entry){
|
|
|
|
strDuration = __('Finished downloading {{packageName}} {{version}} in {{duration}}ms', {packageName: packageName, version: version, duration: entry.duration});
|
|
|
|
performance.clearMarks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// log our measurement and make it red if it has taken too long
|
|
|
|
if(entry && strDuration){
|
2018-06-06 02:04:30 +00:00
|
|
|
if(entry.duration > 4000){
|
2018-06-05 07:15:21 +00:00
|
|
|
strDuration = strDuration.red;
|
|
|
|
}
|
|
|
|
this.logger.info(strDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
obsMeasure.observe({entryTypes: ['measure']});
|
|
|
|
|
|
|
|
// define mark and measurement names
|
|
|
|
let startMark = 'downloadStart' + packageName + version;
|
|
|
|
let ongoingMark = 'downloadOngoingMark' + packageName + version;
|
|
|
|
let downloadOngoing = 'downloadOngoing' + packageName + version;
|
|
|
|
let endMark = 'downloadEnd' + packageName + version;
|
|
|
|
let downloadComplete = 'downloadComplete' + packageName + version;
|
|
|
|
|
|
|
|
// mark our start time
|
|
|
|
performance.mark(startMark);
|
|
|
|
|
|
|
|
// function that continually updates the console to show user that we're downloading a library
|
|
|
|
let intOngoingDownload = setInterval(
|
|
|
|
function(){
|
|
|
|
performance.mark(ongoingMark);
|
|
|
|
performance.measure(downloadOngoing, startMark, ongoingMark);
|
|
|
|
}, 750);
|
|
|
|
|
|
|
|
// do the package download/install
|
2018-04-20 21:17:49 +00:00
|
|
|
manager.install(packageName, version).then((result) => {
|
2018-06-05 07:15:21 +00:00
|
|
|
// stop updating console for ongoing download
|
|
|
|
clearInterval(intOngoingDownload);
|
|
|
|
performance.mark(endMark);
|
|
|
|
performance.measure(downloadComplete, startMark, endMark);
|
2018-04-20 23:52:51 +00:00
|
|
|
callback(null , result.location);
|
|
|
|
}).catch(callback);
|
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;
|