From 31ecaf08373e054d738695b9b8f2f692d106a874 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 2 Jul 2017 13:33:11 -0400 Subject: [PATCH] support to download specified versions of web3 --- lib/core/config.js | 10 ++++++++++ lib/pipeline/npm.js | 34 ++++++++++++++++++++++++++++++++++ lib/utils/utils.js | 2 +- package.json | 1 + test_app/embark.json | 3 ++- 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 lib/pipeline/npm.js diff --git a/lib/core/config.js b/lib/core/config.js index 26dbb3c3..016f9ca0 100644 --- a/lib/core/config.js +++ b/lib/core/config.js @@ -1,6 +1,7 @@ var fs = require('./fs.js'); var Plugins = require('./plugins.js'); var utils = require('../utils/utils.js'); +var npm = require('../pipeline/npm.js'); // TODO: add wrapper for fs so it can also work in the browser // can work with both read and save @@ -248,6 +249,13 @@ Config.prototype.loadFiles = function(files) { readFiles.push({filename: 'embark.js', content: fs.readFileSync(fs.embarkPath("js/build/embark.bundle.js")).toString(), path: fs.embarkPath("js/build/embark.bundle.js")}); } + if (file.indexOf("web3-") == 0) { + let web3Version = file.split('web3-')[1].split(".js")[0]; + npm.getPackageVersion('web3', web3Version, function(web3Content) { + self.logger.error('downloaded web3'); + readFiles.push({filename: file, content: web3Content, path: fs.embarkPath("js/web3.js")}); + }); + } }); // get plugins @@ -280,6 +288,8 @@ Config.prototype.loadFiles = function(files) { originalFiles.filter(function(file) { if (file === 'embark.js') { return; + } else if (file.indexOf("web3-") === 0) { + return; } else if (file === 'abi.js') { readFiles.push({filename: file, content: "", path: file}); } else { diff --git a/lib/pipeline/npm.js b/lib/pipeline/npm.js new file mode 100644 index 00000000..af06de3e --- /dev/null +++ b/lib/pipeline/npm.js @@ -0,0 +1,34 @@ +let utils = require('../utils/utils.js'); + +module.exports = { + // TODO: need to refactor to make it truly generalistic, perhaps by + // downloading the tarball itself + getPackageVersion: function(packageName, version, callback) { + let npmRegistry = "http://registry.npmjs.org/" + packageName + "/" + version; + + utils.httpGet(npmRegistry, function (res) { + + let body = ''; + res.on('data', function (d) { + body += d; + }); + res.on('end', function () { + let registryJSON = JSON.parse(body); + let gitHash = registryJSON.gitHead; + let repo = registryJSON.homepage.split("github.com/")[1]; + + let gitUrl = "http://raw.githubusercontent.com/" + repo + "/" + gitHash + "/dist/" + packageName + ".js" + utils.httpGet(gitUrl, function (res2) { + let body = ''; + res2.on('data', function (d) { + body += d; + }); + res2.on('end', function () { + callback(body); + }); + }); + }); + }); + } +}; + diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 3460f181..354e9508 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -2,7 +2,7 @@ let path = require('path'); let globule = require('globule'); let merge = require('merge'); -let http = require('http'); +let http = require('follow-redirects').http; let shelljs = require('shelljs'); function joinPath() { diff --git a/package.json b/package.json index 6b73ebe8..a1062f9c 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "commander": "^2.8.1", "ethereumjs-testrpc": "3.9.2", "finalhandler": "^0.5.0", + "follow-redirects": "^1.2.4", "fs-extra": "^2.0.0", "globule": "^1.1.0", "merge": "^1.2.0", diff --git a/test_app/embark.json b/test_app/embark.json index c9c850dc..fa679dc7 100644 --- a/test_app/embark.json +++ b/test_app/embark.json @@ -10,7 +10,8 @@ "js/mytoken.js": ["$MyToken", "app/js/token_test.js"], "index.html": "app/index.html", "test.html": "app/test.html", - "test2.html": "app/test2.html" + "test2.html": "app/test2.html", + "js/myweb3.js": "web3-0.18.js" }, "buildDir": "dist/", "config": "config/",