diff --git a/lib/core/engine.js b/lib/core/engine.js index f5fe2ce3..6aa0fc5d 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -10,6 +10,8 @@ const Watch = require('../pipeline/watch.js'); const LibraryManager = require('../versions/library_manager.js'); const utils = require('../utils/utils'); const constants = require('../constants'); +const async = require('async'); +const fs = require('../core/fs.js'); class Engine { constructor(options) { @@ -136,34 +138,73 @@ class Engine { self.events.emit("status", "Building Assets"); const pipelineProcess = require('child_process').fork(utils.joinPath(__dirname, '../pipeline/pipeline.js')); - pipelineProcess.send({action: constants.pipeline.init, options: { - buildDir: self.config.buildDir, - contractsFiles: self.config.contractsFiles, - assetFiles: self.config.assetFiles, - events: self.events, - pipelinePlugins: self.plugins.getPluginsFor('pipeline'), - pluginImports: self.plugins.getPluginsProperty('imports', 'imports') - }}); - - pipelineProcess.on('message', function (msg) { - if (msg.result === constants.pipeline.built) { - if (self.watch) { - self.watch.restart(); // Necessary because changing a file while it is writing can stop it from being watched + async.waterfall([ + function getWeb3Version(next) { + console.log('Waiting for web3 version'); + self.events.request("version:get:web3", function(web3Version) { + console.log('Web3 version', web3Version); + next(null, web3Version); + }); + }, + function getWeb3Location(web3Version, next) { + 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(err, fs.dappPath(location)); + }); } - return self.events.emit('outputDone'); + }, + function getProviderCode(web3Location, next) { + self.events.once('code-generator-ready', function () { + self.events.request('provider-code', function(providerCode) { + next(null, web3Location, providerCode); + }); + }); + } + ], (err, web3Location, providerCode) => { + if (err) { + return self.logger.error(err); } - if (msg.result === constants.pipeline.log) { - self.logger.debug(self.normalizeInput(msg.message)); - } - }); + // Setup Pipeline + pipelineProcess.send({action: constants.pipeline.init, options: { + buildDir: self.config.buildDir, + contractsFiles: self.config.contractsFiles, + assetFiles: self.config.assetFiles, + events: self.events, + pipelinePlugins: self.plugins.getPluginsFor('pipeline'), + pluginImports: self.plugins.getPluginsProperty('imports', 'imports'), + web3Location, + providerCode + }}); - self.events.on('code-generator-ready', function () { - self.events.request('code', function (abi, contractsJSON) { - self.currentAbi = abi; - self.contractsJSON = contractsJSON; - pipelineProcess.send({action: constants.pipeline.build, abi, contractsJSON, path: null}); + pipelineProcess.on('message', function (msg) { + if (msg.result === constants.pipeline.built) { + if (self.watch) { + self.watch.restart(); // Necessary because changing a file while it is writing can stop it from being watched + } + return self.events.emit('outputDone'); + } + + if (msg.result === constants.pipeline.log) { + self.logger.debug(self.normalizeInput(msg.message)); + } }); + + function build() { + self.events.request('code', function (abi, contractsJSON) { + self.currentAbi = abi; + self.contractsJSON = contractsJSON; + pipelineProcess.send({action: constants.pipeline.build, abi, contractsJSON, path: null}); + }); + } + + self.events.on('code-generator-ready', function () { + build(); + }); + + build(); }); } @@ -180,6 +221,7 @@ class Engine { communicationConfig: self.config.communicationConfig, events: self.events }); + console.log('LISTEN TO COMMANDS'); codeGenerator.listenToCommands(); codeGenerator.buildEmbarkJS(function() { self.events.emit('code-generator-ready'); diff --git a/lib/core/fs.js b/lib/core/fs.js index d196d99c..73f8231a 100644 --- a/lib/core/fs.js +++ b/lib/core/fs.js @@ -19,6 +19,10 @@ function appendFileSync() { return fs.appendFileSync.apply(fs.writeFileSync, arguments); } +function writeFile() { + return fs.writeFile.apply(fs.writeFileSync, arguments); +} + function writeFileSync() { return fs.writeFileSync.apply(fs.writeFileSync, arguments); } @@ -67,18 +71,19 @@ function createWriteStream() { } module.exports = { - mkdirpSync: mkdirpSync, + mkdirpSync, mkdirp, - copySync: copySync, + copySync, readFile, - readFileSync: readFileSync, - appendFileSync: appendFileSync, - writeFileSync: writeFileSync, - readJSONSync: readJSONSync, - writeJSONSync: writeJSONSync, - existsSync: existsSync, - removeSync: removeSync, - embarkPath: embarkPath, - dappPath: dappPath, + readFileSync, + appendFileSync, + writeFile, + writeFileSync, + readJSONSync, + writeJSONSync, + existsSync, + removeSync, + embarkPath, + dappPath, createWriteStream }; diff --git a/lib/pipeline/pipeline.js b/lib/pipeline/pipeline.js index 0bc0a532..e697ee60 100644 --- a/lib/pipeline/pipeline.js +++ b/lib/pipeline/pipeline.js @@ -17,9 +17,10 @@ class Pipeline { this.buildDir = options.buildDir; this.contractsFiles = options.contractsFiles; this.assetFiles = options.assetFiles; - this.events = options.events; this.pipelinePlugins = options.pipelinePlugins; this.pluginImports = options.pluginImports; + this.web3Location = options.web3Location; + this.providerCode = options.providerCode; this.interceptLogs(); } @@ -45,7 +46,10 @@ class Pipeline { this.buildContracts(contractsJSON); - self.buildWeb3JS(function() { + self.buildWeb3JS(function(err) { + if (err) { + return callback(err); + } let importsList = {}; @@ -254,6 +258,10 @@ class Pipeline { } webpack(webpackOptions).run((_err, _stats) => { + if (_err) { + console.log('ERROS'); + console.log(_err); + } if (!detectErrors) { return callback(); } @@ -297,42 +305,31 @@ class Pipeline { 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"; + function getImports(next) { + self.web3Location = self.web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes + code += "\nimport Web3 from '" + self.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(); + code += self.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.mkdirpSync(fs.dappPath(".embark")); - fs.writeFileSync(fs.dappPath(".embark", 'web3_instance.js'), code); - next(); + fs.writeFile(fs.dappPath(".embark", 'web3_instance.js'), code, next); } - ], function(_err, _result) { - cb(); - }); + ], cb); } }