From 1a5ddea9a81ffab0198d4379e04434a624dac0cd Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 3 Jul 2015 07:59:35 -0400 Subject: [PATCH] restructure lib initialization --- lib/config/config.js | 4 ++-- lib/config/contracts.js | 14 +++++++++----- lib/index.js | 20 +++++++++++++------- test/config.contracts.js | 12 +++++------- test/deploy.js | 3 ++- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/config/config.js b/lib/config/config.js index 3dfc3d64..48968faf 100644 --- a/lib/config/config.js +++ b/lib/config/config.js @@ -3,10 +3,10 @@ var readYaml = require('read-yaml'); BlockchainConfig = require('./blockchain.js'); ContractsConfig = require('./contracts.js'); -Config = { +config = { Blockchain: BlockchainConfig, Contracts: ContractsConfig } -module.exports = Config +module.exports = config diff --git a/lib/config/contracts.js b/lib/config/contracts.js index 1102d9a9..4416ae78 100644 --- a/lib/config/contracts.js +++ b/lib/config/contracts.js @@ -4,21 +4,25 @@ var fs = require('fs'); var Blockchain = require('./blockchain.js'); var toposort = require('toposort'); -ContractsConfig = function(files, blockchainConfig, web3) { +ContractsConfig = function(blockchainConfig, web3) { + this.blockchainConfig = blockchainConfig; + this.web3 = web3; + this.contractFiles = []; +} + +ContractsConfig.prototype.init = function(files) { this.all_contracts = []; this.contractDB = {}; this.contractFiles = files; - this.web3 = web3; this.contractDependencies = {}; - this.blockchainConfig = blockchainConfig; try { - this.web3.setProvider(new this.web3.providers.HttpProvider("http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort)); + this.web3.setProvider(new this.web3.providers.HttpProvider("http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort)); primaryAddress = this.web3.eth.coinbase; this.web3.eth.defaultAccount = primaryAddress; } catch (_error) { e = _error; - throw new Error("can't connect to " + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort + " check if an ethereum node is running"); + throw new Error("can't connect to " + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + " check if an ethereum node is running"); } console.log("address is : " + primaryAddress); diff --git a/lib/index.js b/lib/index.js index 0981bb33..c28bbc9a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,12 +12,18 @@ var syncMe = require('sync-me'); var methodmissing = require('methodmissing'); var jasmine = require('jasmine'); -embark = {} -embark.Tests = require('./test.js'); -embark.Blockchain = require('./blockchain.js'); -embark.Deploy = require('./deploy.js'); -embark.Release = require('./ipfs.js'); -embark.Config = require('./config/config.js'); +var Tests = require('./test.js'); +var Blockchain = require('./blockchain.js'); +var Deploy = require('./deploy.js'); +var Release = require('./ipfs.js'); +var Config = require('./config/config.js'); -module.exports = embark; +Embark = { + init: function() { + this.blockchainConfig = (new Config.BlockchainConfig()); + this.contractsConfig = (new Config.ContractsConfig(this.blockchainConfig, web3)); + } +} + +module.exports = Embark; diff --git a/test/config.contracts.js b/test/config.contracts.js index 33795e99..78f6381e 100644 --- a/test/config.contracts.js +++ b/test/config.contracts.js @@ -6,14 +6,10 @@ require('mocha-sinon'); describe('embark.config.contracts', function() { var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development"); - //sinon.stub(web3, "setProvider"); - //sinon.stub(web3.eth.compile, "solidity", function() { - // return "compiled_code"; - //}); describe('#loadConfigFile', function() { it('should read and load yml file', function() { - var contractsConfig = new Config.Contracts([], blockchainConfig, web3); + var contractsConfig = new Config.Contracts(blockchainConfig, web3); contractsConfig.loadConfigFile('test/support/contracts.yml'); assert.equal(contractsConfig.contractConfig.hasOwnProperty('development'), true) @@ -47,8 +43,9 @@ describe('embark.config.contracts', function() { 'test/support/contracts/simple_storage.sol', 'test/support/contracts/another_storage.sol' ] - contractsConfig = new Config.Contracts(files, blockchainConfig, web3); + contractsConfig = new Config.Contracts(blockchainConfig, web3); contractsConfig.loadConfigFile('test/support/contracts.yml'); + contractsConfig.init(files); contractsConfig.compileContracts(); }); @@ -65,8 +62,9 @@ describe('embark.config.contracts', function() { 'test/support/contracts/another_storage.sol', 'test/support/contracts/wallets.sol' ] - contractsConfig = new Config.Contracts(files, blockchainConfig, web3); + contractsConfig = new Config.Contracts(blockchainConfig, web3); contractsConfig.loadConfigFile('test/support/arguments.yml'); + contractsConfig.init(files); contractsConfig.compileContracts('development'); }); diff --git a/test/deploy.js b/test/deploy.js index 461bfc45..e8f9bd16 100644 --- a/test/deploy.js +++ b/test/deploy.js @@ -11,8 +11,9 @@ describe('embark.deploy', function() { 'test/support/contracts/wallets.sol' ]; var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development"); - var contractsConfig = new Config.Contracts(files, blockchainConfig, web3); + var contractsConfig = new Config.Contracts(blockchainConfig, web3); contractsConfig.loadConfigFile('test/support/arguments.yml'); + contractsConfig.init(files); var deploy = new Deploy('development', files, blockchainConfig, contractsConfig); describe('#deploy_contracts', function() {