From 1f6e386d9ba6652d343e924b78e5fe844b736db8 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 15 May 2018 17:57:56 -0400 Subject: [PATCH] build web3js generation from pipeline to code_generator --- lib/contracts/code_generator.js | 48 +++++++++++++++++++++++++++++++++ lib/pipeline/pipeline.js | 45 +------------------------------ 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/lib/contracts/code_generator.js b/lib/contracts/code_generator.js index 6b8306aec..23948dabf 100644 --- a/lib/contracts/code_generator.js +++ b/lib/contracts/code_generator.js @@ -96,6 +96,10 @@ class CodeGenerator { cb(vanillaABI, contractsJSON); }); + this.events.setCommandHandlerOnce('code-generator:web3js', function(cb) { + self.buildWeb3JS(cb); + }); + } generateContext() { @@ -318,6 +322,50 @@ class CodeGenerator { cb(); }); } + + buildWeb3JS(cb) { + const self = this; + let code = ""; + + async.waterfall([ + function getWeb3Location(next) { + self.events.request("version:get:web3", function(web3Version) { + if (web3Version === "1.0.0-beta") { + return next(null, utils.joinPath(fs.embarkPath("js/web3-1.0.min.js"))); + } else { + self.events.request("version:getPackageLocation", "web3", web3Version, function(err, location) { + return next(null, fs.dappPath(location)); + }); + } + }); + }, + function getImports(web3Location, next) { + web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes + code += "\nimport Web3 from '" + web3Location + "';\n"; + + code += "\n if (typeof web3 !== 'undefined') {"; + code += "\n } else {"; + code += "\n var web3 = new Web3();\n"; + code += "\n }"; + + let providerCode = self.generateProvider(false); + code += providerCode; + code += "\nglobal.__embarkContext = __mainContext.__loadManagerInstance;\n"; + code += "\nwindow.web3 = web3;\n"; + code += "\nexport default web3;\n"; + next(); + }, + function makeDirectory(next) { + fs.mkdirp(fs.dappPath(".embark"), (err, _result) => { + next(err); + }); + }, + function writeFile(next) { + fs.writeFile(fs.dappPath(".embark", 'web3_instance.js'), code, next); + } + ], cb); + } + } module.exports = CodeGenerator; diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 6b20a8906..98be95b1b 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -31,7 +31,7 @@ class Pipeline { self.buildContracts(contractsJSON, next); }, function buildWeb3(next) { - self.buildWeb3JS(next); + self.events.request('code-generator:web3js', next); }, function createImportList(next) { importsList["Embark/EmbarkJS"] = fs.dappPath(".embark", 'embark.js'); @@ -211,49 +211,6 @@ class Pipeline { }); } - buildWeb3JS(cb) { - const self = this; - let code = ""; - - async.waterfall([ - function getWeb3Location(next) { - self.events.request("version:get:web3", function(web3Version) { - if (web3Version === "1.0.0-beta") { - return next(null, utils.joinPath(fs.embarkPath("js/web3-1.0.min.js"))); - } else { - self.events.request("version:getPackageLocation", "web3", web3Version, function(err, location) { - return next(null, fs.dappPath(location)); - }); - } - }); - }, - function getImports(web3Location, next) { - web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes - code += "\nimport Web3 from '" + web3Location + "';\n"; - - code += "\n if (typeof web3 !== 'undefined') {"; - code += "\n } else {"; - code += "\n var web3 = new Web3();\n"; - code += "\n }"; - - self.events.request('provider-code', function(providerCode) { - code += providerCode; - code += "\nglobal.__embarkContext = __mainContext.__loadManagerInstance;\n"; - code += "\nwindow.web3 = web3;\n"; - code += "\nexport default web3;\n"; - next(); - }); - }, - function makeDirectory(next) { - fs.mkdirp(fs.dappPath(".embark"), (err, _result) => { - next(err); - }); - }, - function writeFile(next) { - fs.writeFile(fs.dappPath(".embark", 'web3_instance.js'), code, next); - } - ], cb); - } } module.exports = Pipeline;