2017-07-02 17:33:11 +00:00
|
|
|
let utils = require('../utils/utils.js');
|
2017-07-05 12:35:51 +00:00
|
|
|
let fs = require('../core/fs.js');
|
|
|
|
let o_fs = require('fs-extra');
|
|
|
|
|
|
|
|
let http = require('follow-redirects').http;
|
|
|
|
let https = require('follow-redirects').https;
|
|
|
|
var tar = require('tar');
|
2017-07-02 17:33:11 +00:00
|
|
|
|
2017-07-05 22:26:44 +00:00
|
|
|
class Npm {
|
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
this.logger = options.logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPackageVersion(packageName, version, returnContent, callback) {
|
|
|
|
let self = this;
|
2017-07-05 12:35:51 +00:00
|
|
|
let npmRegistry = "https://registry.npmjs.org/" + packageName + "/" + version;
|
2017-07-02 17:33:11 +00:00
|
|
|
|
2017-07-05 12:35:51 +00:00
|
|
|
utils.httpsGet(npmRegistry, function (res) {
|
2017-07-02 17:33:11 +00:00
|
|
|
|
|
|
|
let body = '';
|
|
|
|
res.on('data', function (d) {
|
|
|
|
body += d;
|
|
|
|
});
|
|
|
|
res.on('end', function () {
|
|
|
|
let registryJSON = JSON.parse(body);
|
2017-07-05 12:35:51 +00:00
|
|
|
|
|
|
|
let tarball = registryJSON.dist.tarball;
|
|
|
|
|
|
|
|
var download = function(url, dest, cb) {
|
|
|
|
var file = o_fs.createWriteStream(dest);
|
|
|
|
var request = (url.substring(0,5) === 'https' ? https : http).get(url, function(response) {
|
|
|
|
response.pipe(file);
|
|
|
|
file.on('finish', function() {
|
|
|
|
file.close(cb); // close() is async, call cb after close completes.
|
|
|
|
});
|
|
|
|
}).on('error', function(err) { // Handle errors
|
|
|
|
fs.unlink(dest); // Delete the file async. (But we don't check the result)
|
|
|
|
if (cb) cb(err.message);
|
2017-07-02 17:33:11 +00:00
|
|
|
});
|
2017-07-05 12:35:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
|
|
|
|
|
|
|
|
if (fs.existsSync(packageDirectory + "/downloaded_package.tgz")) {
|
|
|
|
if (returnContent) {
|
|
|
|
let distFile = packageDirectory + returnContent;
|
|
|
|
callback(fs.readFileSync(distFile).toString());
|
|
|
|
} else {
|
|
|
|
callback(packageDirectory);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fs.mkdirpSync(packageDirectory);
|
2017-07-05 22:26:44 +00:00
|
|
|
self.logger.info("downloading " + packageName + " " + version + "....");
|
2017-07-05 12:35:51 +00:00
|
|
|
|
|
|
|
download(tarball, packageDirectory + "/downloaded_package.tgz", function() {
|
|
|
|
o_fs.createReadStream(packageDirectory + '/downloaded_package.tgz').pipe(
|
|
|
|
tar.x({
|
|
|
|
strip: 1,
|
|
|
|
C: packageDirectory
|
|
|
|
}).on('end', function() {
|
|
|
|
if (returnContent) {
|
|
|
|
let distFile = packageDirectory + returnContent;
|
|
|
|
callback(fs.readFileSync(distFile).toString());
|
|
|
|
} else {
|
|
|
|
callback(packageDirectory);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2017-07-02 17:33:11 +00:00
|
|
|
});
|
2017-07-05 12:35:51 +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;
|