fix getting web3 location and provider code

This commit is contained in:
Jonathan Rainville 2018-05-07 15:48:01 -04:00
parent 148a74f3d6
commit d2d29be334
3 changed files with 106 additions and 62 deletions

View File

@ -10,6 +10,8 @@ const Watch = require('../pipeline/watch.js');
const LibraryManager = require('../versions/library_manager.js'); const LibraryManager = require('../versions/library_manager.js');
const utils = require('../utils/utils'); const utils = require('../utils/utils');
const constants = require('../constants'); const constants = require('../constants');
const async = require('async');
const fs = require('../core/fs.js');
class Engine { class Engine {
constructor(options) { constructor(options) {
@ -136,34 +138,73 @@ class Engine {
self.events.emit("status", "Building Assets"); self.events.emit("status", "Building Assets");
const pipelineProcess = require('child_process').fork(utils.joinPath(__dirname, '../pipeline/pipeline.js')); const pipelineProcess = require('child_process').fork(utils.joinPath(__dirname, '../pipeline/pipeline.js'));
pipelineProcess.send({action: constants.pipeline.init, options: { async.waterfall([
buildDir: self.config.buildDir, function getWeb3Version(next) {
contractsFiles: self.config.contractsFiles, console.log('Waiting for web3 version');
assetFiles: self.config.assetFiles, self.events.request("version:get:web3", function(web3Version) {
events: self.events, console.log('Web3 version', web3Version);
pipelinePlugins: self.plugins.getPluginsFor('pipeline'), next(null, web3Version);
pluginImports: self.plugins.getPluginsProperty('imports', 'imports') });
}}); },
function getWeb3Location(web3Version, next) {
pipelineProcess.on('message', function (msg) { if (web3Version === "1.0.0-beta") {
if (msg.result === constants.pipeline.built) { return next(null, utils.joinPath(fs.embarkPath("js/web3-1.0.min.js")));
if (self.watch) { } else {
self.watch.restart(); // Necessary because changing a file while it is writing can stop it from being watched 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) { // Setup Pipeline
self.logger.debug(self.normalizeInput(msg.message)); 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 () { pipelineProcess.on('message', function (msg) {
self.events.request('code', function (abi, contractsJSON) { if (msg.result === constants.pipeline.built) {
self.currentAbi = abi; if (self.watch) {
self.contractsJSON = contractsJSON; self.watch.restart(); // Necessary because changing a file while it is writing can stop it from being watched
pipelineProcess.send({action: constants.pipeline.build, abi, contractsJSON, path: null}); }
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, communicationConfig: self.config.communicationConfig,
events: self.events events: self.events
}); });
console.log('LISTEN TO COMMANDS');
codeGenerator.listenToCommands(); codeGenerator.listenToCommands();
codeGenerator.buildEmbarkJS(function() { codeGenerator.buildEmbarkJS(function() {
self.events.emit('code-generator-ready'); self.events.emit('code-generator-ready');

View File

@ -19,6 +19,10 @@ function appendFileSync() {
return fs.appendFileSync.apply(fs.writeFileSync, arguments); return fs.appendFileSync.apply(fs.writeFileSync, arguments);
} }
function writeFile() {
return fs.writeFile.apply(fs.writeFileSync, arguments);
}
function writeFileSync() { function writeFileSync() {
return fs.writeFileSync.apply(fs.writeFileSync, arguments); return fs.writeFileSync.apply(fs.writeFileSync, arguments);
} }
@ -67,18 +71,19 @@ function createWriteStream() {
} }
module.exports = { module.exports = {
mkdirpSync: mkdirpSync, mkdirpSync,
mkdirp, mkdirp,
copySync: copySync, copySync,
readFile, readFile,
readFileSync: readFileSync, readFileSync,
appendFileSync: appendFileSync, appendFileSync,
writeFileSync: writeFileSync, writeFile,
readJSONSync: readJSONSync, writeFileSync,
writeJSONSync: writeJSONSync, readJSONSync,
existsSync: existsSync, writeJSONSync,
removeSync: removeSync, existsSync,
embarkPath: embarkPath, removeSync,
dappPath: dappPath, embarkPath,
dappPath,
createWriteStream createWriteStream
}; };

View File

@ -17,9 +17,10 @@ class Pipeline {
this.buildDir = options.buildDir; this.buildDir = options.buildDir;
this.contractsFiles = options.contractsFiles; this.contractsFiles = options.contractsFiles;
this.assetFiles = options.assetFiles; this.assetFiles = options.assetFiles;
this.events = options.events;
this.pipelinePlugins = options.pipelinePlugins; this.pipelinePlugins = options.pipelinePlugins;
this.pluginImports = options.pluginImports; this.pluginImports = options.pluginImports;
this.web3Location = options.web3Location;
this.providerCode = options.providerCode;
this.interceptLogs(); this.interceptLogs();
} }
@ -45,7 +46,10 @@ class Pipeline {
this.buildContracts(contractsJSON); this.buildContracts(contractsJSON);
self.buildWeb3JS(function() { self.buildWeb3JS(function(err) {
if (err) {
return callback(err);
}
let importsList = {}; let importsList = {};
@ -254,6 +258,10 @@ class Pipeline {
} }
webpack(webpackOptions).run((_err, _stats) => { webpack(webpackOptions).run((_err, _stats) => {
if (_err) {
console.log('ERROS');
console.log(_err);
}
if (!detectErrors) { if (!detectErrors) {
return callback(); return callback();
} }
@ -297,42 +305,31 @@ class Pipeline {
let code = ""; let code = "";
async.waterfall([ async.waterfall([
function getWeb3Location(next) { function getImports(next) {
self.events.request("version:get:web3", function(web3Version) { self.web3Location = self.web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes
if (web3Version === "1.0.0-beta") { code += "\nimport Web3 from '" + self.web3Location + "';\n";
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 if (typeof web3 !== 'undefined') {";
code += "\n } else {"; code += "\n } else {";
code += "\n var web3 = new Web3();\n"; code += "\n var web3 = new Web3();\n";
code += "\n }"; code += "\n }";
self.events.request('provider-code', function(providerCode) { code += self.providerCode;
code += providerCode; code += "\nglobal.__embarkContext = __mainContext.__loadManagerInstance;\n";
code += "\nglobal.__embarkContext = __mainContext.__loadManagerInstance;\n"; code += "\nwindow.web3 = web3;\n";
code += "\nwindow.web3 = web3;\n"; code += "\nexport default web3;\n";
code += "\nexport default web3;\n";
next(); next();
},
function makeDirectory(next) {
fs.mkdirp(fs.dappPath(".embark"), (err, _result) => {
next(err);
}); });
}, },
function writeFile(next) { function writeFile(next) {
fs.mkdirpSync(fs.dappPath(".embark")); fs.writeFile(fs.dappPath(".embark", 'web3_instance.js'), code, next);
fs.writeFileSync(fs.dappPath(".embark", 'web3_instance.js'), code);
next();
} }
], function(_err, _result) { ], cb);
cb();
});
} }
} }