2017-12-05 23:14:46 +00:00
|
|
|
// here be dragons
|
2017-10-17 10:51:52 +00:00
|
|
|
// TODO: this is horrible and needs to be refactored ASAP
|
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');
|
2017-07-02 17:33:11 +00:00
|
|
|
|
2017-07-05 22:26:44 +00:00
|
|
|
class Npm {
|
|
|
|
|
|
|
|
constructor(options) {
|
|
|
|
this.logger = options.logger;
|
|
|
|
}
|
|
|
|
|
2017-12-16 17:44:52 +00:00
|
|
|
downloadFromGit(registryJSON, packageName, version, returnContent, callback) {
|
|
|
|
let repoName = registryJSON.repository.url.replace("git+https://github.com/", "").replace(".git","");
|
|
|
|
let gitHead = registryJSON.gitHead;
|
2017-07-02 17:33:11 +00:00
|
|
|
|
2017-12-16 17:44:52 +00:00
|
|
|
if (!gitHead) {
|
2017-12-16 17:48:59 +00:00
|
|
|
this.logger.error("Could not download " + packageName + " " + version);
|
2017-12-16 17:44:52 +00:00
|
|
|
return callback("error");
|
|
|
|
}
|
2017-07-02 17:33:11 +00:00
|
|
|
|
2017-12-16 17:44:52 +00:00
|
|
|
let fileLocation = "https://raw.githubusercontent.com/" + repoName + "/" + gitHead + "/dist/web3.min.js";
|
2017-07-05 12:35:51 +00:00
|
|
|
|
2017-12-16 17:44:52 +00:00
|
|
|
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
|
|
|
|
if (fs.existsSync(packageDirectory + "/" + packageName + ".js")) {
|
|
|
|
if (returnContent) {
|
|
|
|
let distFile = packageDirectory + packageName + ".js";
|
2017-12-16 22:05:37 +00:00
|
|
|
callback(null, fs.readFileSync(distFile).toString());
|
2017-12-16 17:44:52 +00:00
|
|
|
} else {
|
2017-12-16 22:05:37 +00:00
|
|
|
callback(null, packageDirectory);
|
2017-12-16 17:44:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fs.mkdirpSync(packageDirectory);
|
2017-12-16 17:48:59 +00:00
|
|
|
this.logger.info("downloading " + packageName + " " + version + "....");
|
2017-07-05 12:35:51 +00:00
|
|
|
|
2017-12-16 17:44:52 +00:00
|
|
|
utils.downloadFile(fileLocation, packageDirectory + "/" + packageName + ".js", function() {
|
|
|
|
utils.extractTar(packageDirectory + "/" + packageName + ".js", packageDirectory, function() {
|
2017-12-16 03:05:38 +00:00
|
|
|
if (returnContent) {
|
|
|
|
let distFile = packageDirectory + packageName + ".js";
|
2017-12-16 22:05:37 +00:00
|
|
|
callback(null, fs.readFileSync(distFile).toString());
|
2017-07-05 12:35:51 +00:00
|
|
|
} else {
|
2017-12-16 22:05:37 +00:00
|
|
|
callback(null, packageDirectory);
|
2017-12-16 03:05:38 +00:00
|
|
|
}
|
2017-12-16 17:44:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 11:30:34 +00:00
|
|
|
|
2017-12-16 17:44:52 +00:00
|
|
|
downloadFromNpm(registryJSON, packageName, version, returnContent, callback) {
|
|
|
|
let tarball = registryJSON.dist.tarball;
|
2017-10-06 11:30:34 +00:00
|
|
|
|
2017-12-16 17:44:52 +00:00
|
|
|
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
|
2018-03-22 19:36:20 +00:00
|
|
|
if (fs.existsSync(packageDirectory + "/downloaded_package.tgz") && fs.existsSync(packageDirectory + "package.json")) {
|
2017-12-16 17:44:52 +00:00
|
|
|
if (returnContent) {
|
|
|
|
let distFile = packageDirectory + returnContent;
|
2017-12-16 22:05:37 +00:00
|
|
|
callback(null, fs.readFileSync(distFile).toString());
|
2017-12-16 03:05:38 +00:00
|
|
|
} else {
|
2017-12-16 22:05:37 +00:00
|
|
|
callback(null, packageDirectory);
|
2017-12-16 17:44:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fs.mkdirpSync(packageDirectory);
|
|
|
|
this.logger.info("downloading " + packageName + " " + version + "....");
|
2017-07-05 12:35:51 +00:00
|
|
|
|
2017-12-16 17:44:52 +00:00
|
|
|
utils.downloadFile(tarball, packageDirectory + "/downloaded_package.tgz", function() {
|
|
|
|
utils.extractTar(packageDirectory + "/downloaded_package.tgz", packageDirectory, function() {
|
2017-12-16 03:05:38 +00:00
|
|
|
if (returnContent) {
|
|
|
|
let distFile = packageDirectory + returnContent;
|
2017-12-16 22:05:37 +00:00
|
|
|
callback(null, fs.readFileSync(distFile).toString());
|
2017-10-06 11:30:34 +00:00
|
|
|
} else {
|
2017-12-16 22:05:37 +00:00
|
|
|
callback(null, packageDirectory);
|
2017-12-16 03:05:38 +00:00
|
|
|
}
|
2017-12-16 17:44:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 11:30:34 +00:00
|
|
|
|
2017-12-16 17:44:52 +00:00
|
|
|
// TODO: callback should accept an error
|
|
|
|
getPackageVersion(packageName, version, returnContent, getFromGit, callback) {
|
|
|
|
let self = this;
|
|
|
|
let npmRegistry = "https://registry.npmjs.org/" + packageName + "/" + version;
|
2017-12-16 22:36:52 +00:00
|
|
|
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
|
|
|
|
|
2018-03-22 19:36:20 +00:00
|
|
|
if (fs.existsSync(packageDirectory) && fs.existsSync(packageDirectory + "package.json")) {
|
2017-12-16 22:36:52 +00:00
|
|
|
let content;
|
|
|
|
if (getFromGit && returnContent) {
|
|
|
|
let distFile = packageDirectory + packageName + ".js";
|
|
|
|
content = fs.readFileSync(distFile).toString();
|
|
|
|
} else if (returnContent) {
|
|
|
|
let distFile = packageDirectory + returnContent;
|
|
|
|
content = fs.readFileSync(distFile).toString();
|
|
|
|
} else {
|
|
|
|
content = packageDirectory;
|
|
|
|
}
|
|
|
|
return callback(null, content);
|
|
|
|
}
|
2017-07-05 12:35:51 +00:00
|
|
|
|
2017-12-16 22:05:37 +00:00
|
|
|
utils.httpsGet(npmRegistry, function (err, body) {
|
|
|
|
if (err) {
|
|
|
|
if (err.code === 'ENOTFOUND') {
|
|
|
|
return callback("can't reach " + err.hostname + " to download " + packageName + " " + version + " - are you connected to the internet?");
|
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-12-16 17:44:52 +00:00
|
|
|
let registryJSON = JSON.parse(body);
|
|
|
|
|
|
|
|
if (getFromGit) {
|
|
|
|
self.downloadFromGit(registryJSON, packageName, version, returnContent, callback);
|
|
|
|
} else {
|
|
|
|
self.downloadFromNpm(registryJSON, packageName, version, returnContent, 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;
|