diff --git a/lib/core/modules/coderunner/codeRunner.js b/lib/core/modules/coderunner/codeRunner.js index 4924c5f4..728918b6 100644 --- a/lib/core/modules/coderunner/codeRunner.js +++ b/lib/core/modules/coderunner/codeRunner.js @@ -1,6 +1,5 @@ -// still needs to be run on a separate file due to the global context -var RunCode = require('./runCode.js'); - +const RunCode = require('./runCode.js'); +const EmbarkJS = require('embarkjs'); class CodeRunner { constructor(options) { this.plugins = options.plugins; @@ -26,6 +25,13 @@ class CodeRunner { self.events.request("runcode:eval", command.code); } }); + } else { + this.runCode.registerVar('EmbarkJS', EmbarkJS); + this.events.on('code-generator-ready', () => { + this.events.request('code-generator:embarkjs:initialization-code', (code) => { + this.runCode.doEval(code); + }); + }) } this.events.on("runcode:register", (varName, code) => { diff --git a/lib/core/modules/coderunner/runCode.js b/lib/core/modules/coderunner/runCode.js index 73078091..0ce3fd1f 100644 --- a/lib/core/modules/coderunner/runCode.js +++ b/lib/core/modules/coderunner/runCode.js @@ -6,7 +6,11 @@ class RunCode { } doEval(code) { - return vm.runInNewContext(code, this.context); + try { + return vm.runInNewContext(code, this.context); + } catch(e) { + console.log(e) + } } registerVar(varName, code) { diff --git a/lib/modules/code_generator/index.js b/lib/modules/code_generator/index.js index 8c13819b..3dfe8d61 100644 --- a/lib/modules/code_generator/index.js +++ b/lib/modules/code_generator/index.js @@ -99,6 +99,10 @@ class CodeGenerator { self.buildPlaceholderPage(cb); }); + self.events.setCommandHandler('code-generator:embarkjs:initialization-code', (cb) => { + console.log("command") + cb(self.getEmbarkJsInitializationCode()); + }); } generateContext() { @@ -117,7 +121,7 @@ class CodeGenerator { result += Templates.main_context(); result += Templates.load_manager(); result += Templates.define_when_env_loaded(); - + if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) { return result; } @@ -216,7 +220,6 @@ class CodeGenerator { return result; } - generateStorageInitialization(useEmbarkJS) { if (!useEmbarkJS || this.storageConfig === {}) return ""; @@ -313,6 +316,8 @@ class CodeGenerator { web3Location = web3Location.replace(/\\/g, '/'); // Import paths must always have forward slashes code += "\nimport Web3 from '" + web3Location + "';\n"; code += "\nimport web3 from 'Embark/web3';\n"; + code += "\nimport IpfsApi from 'ipfs-api';\n"; + next(); }, function getJSCode(next) { @@ -325,6 +330,7 @@ class CodeGenerator { code += self.generateCommunicationInitialization(true); code += self.generateStorageInitialization(true); code += self.generateNamesInitialization(true); + next(); }, function writeFile(next) { @@ -337,6 +343,27 @@ class CodeGenerator { }); } + getEmbarkJsInitializationCode() { + let code = ''; + let embarkjsCodes = this.plugins.getPluginsFor('embarkjsCode'); + for (let plugin of embarkjsCodes) { + code += plugin.embarkjs_code.join('\n'); + } + + let initCodes = this.plugins.getPluginsFor('initCode'); + for (let plugin of initCodes) { + let initCodes = plugin.embarkjs_init_code['communication'] || []; + for (let initCode of initCodes) { + let [block, shouldInit] = initCode; + if (shouldInit.call(plugin, this.communicationConfig)) { + code += block; + } + } + } + + return code; + } + buildContractJS(contractName, contractJSON, cb) { let contractCode = ""; contractCode += "import web3 from 'Embark/web3';\n"; diff --git a/lib/modules/ipfs/embarkjs.js b/lib/modules/ipfs/embarkjs.js index 105739d4..3f454133 100644 --- a/lib/modules/ipfs/embarkjs.js +++ b/lib/modules/ipfs/embarkjs.js @@ -1,4 +1,4 @@ -import IpfsApi from 'ipfs-api'; +/*global IpfsApi*/ let __embarkIPFS = {};