diff --git a/src/lib/constants.json b/src/lib/constants.json index 3599f9587..febebb808 100644 --- a/src/lib/constants.json +++ b/src/lib/constants.json @@ -83,5 +83,9 @@ "eth", "xyz" ] + }, + "dappConfig": { + "dir": "config", + "blockchain": "blockchain.json" } } diff --git a/src/lib/modules/code_generator/index.js b/src/lib/modules/code_generator/index.js index fa01fd6da..54c77403b 100644 --- a/src/lib/modules/code_generator/index.js +++ b/src/lib/modules/code_generator/index.js @@ -20,6 +20,9 @@ const Templates = { class CodeGenerator { constructor(embark, options) { this.blockchainConfig = embark.config.blockchainConfig || {}; + this.embarkConfig = embark.config.embarkConfig; + this.dappConfigs = {}; + this.logger = embark.logger; this.rpcHost = this.blockchainConfig.rpcHost || ''; this.rpcPort = this.blockchainConfig.rpcPort || ''; this.contractsConfig = embark.config.contractsConfig || {}; @@ -42,6 +45,10 @@ class CodeGenerator { listenToCommands() { let self = this; + this.events.on('config:load:contracts', (contractConfig) => { + this.generateConfigs(contractConfig); + }); + this.events.setCommandHandler('provider-code', function(cb) { let providerCode = self.generateProvider(false); @@ -186,6 +193,23 @@ class CodeGenerator { return result; } + generateConfigs(contractConfig) { + this.dappConfigs.blockchain = {dappConnection: contractConfig.dappConnection}; + async.waterfall([ + (next) => { + fs.mkdirp(utils.joinPath(this.embarkConfig.buildDir, constants.dappConfig.dir), next); + }, + (_dir, next) => { + fs.writeFile(utils.joinPath(this.embarkConfig.buildDir, constants.dappConfig.dir, constants.dappConfig.blockchain), + JSON.stringify(this.dappConfigs.blockchain, null, 2), next); + } + ], (err) => { + if (err) { + this.logger.error(err.message || err); + } + }); + } + generateContractCode(contract, gasLimit) { let abi = JSON.stringify(contract.abiDefinition); diff --git a/src/test/code_generator.js b/src/test/code_generator.js index 7fc1b6eac..38ea75b03 100644 --- a/src/test/code_generator.js +++ b/src/test/code_generator.js @@ -27,14 +27,15 @@ describe('embark.CodeGenerator', function() { deployedAddress: "0x124", code: '123456' } - ] + ]; const TestEvents = { request: (cmd, cb) => { cb(currentSolcVersion); }, setCommandHandler: () => { - } + }, + on: () => {} }; let generator = new CodeGenerator({config: {blockchainConfig: {}}, events: TestEvents}, {});