refactor download

This commit is contained in:
Iuri Matias 2017-12-16 12:44:52 -05:00
parent a216aa980f
commit 9d78f995e8
1 changed files with 66 additions and 61 deletions

View File

@ -9,17 +9,7 @@ class Npm {
this.logger = options.logger;
}
// TODO: callback should accept an error
getPackageVersion(packageName, version, returnContent, getFromGit, callback) {
let self = this;
let npmRegistry = "https://registry.npmjs.org/" + packageName + "/" + version;
utils.httpsGet(npmRegistry, function (_err, body) {
let registryJSON = JSON.parse(body);
let tarball = registryJSON.dist.tarball;
if (getFromGit) {
downloadFromGit(registryJSON, packageName, version, returnContent, callback) {
let repoName = registryJSON.repository.url.replace("git+https://github.com/", "").replace(".git","");
let gitHead = registryJSON.gitHead;
@ -53,8 +43,10 @@ class Npm {
});
});
}
}
} else {
downloadFromNpm(registryJSON, packageName, version, returnContent, callback) {
let tarball = registryJSON.dist.tarball;
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
if (fs.existsSync(packageDirectory + "/downloaded_package.tgz")) {
@ -66,7 +58,7 @@ class Npm {
}
} else {
fs.mkdirpSync(packageDirectory);
self.logger.info("downloading " + packageName + " " + version + "....");
this.logger.info("downloading " + packageName + " " + version + "....");
utils.downloadFile(tarball, packageDirectory + "/downloaded_package.tgz", function() {
utils.extractTar(packageDirectory + "/downloaded_package.tgz", packageDirectory, function() {
@ -81,6 +73,19 @@ class Npm {
}
}
// TODO: callback should accept an error
getPackageVersion(packageName, version, returnContent, getFromGit, callback) {
let self = this;
let npmRegistry = "https://registry.npmjs.org/" + packageName + "/" + version;
utils.httpsGet(npmRegistry, function (_err, body) {
let registryJSON = JSON.parse(body);
if (getFromGit) {
self.downloadFromGit(registryJSON, packageName, version, returnContent, callback);
} else {
self.downloadFromNpm(registryJSON, packageName, version, returnContent, callback);
}
});
}
}