diff --git a/lib/modules/deployment/index.js b/lib/modules/deployment/index.js index c32ca242e..8c6445d47 100644 --- a/lib/modules/deployment/index.js +++ b/lib/modules/deployment/index.js @@ -1,8 +1,7 @@ let async = require('async'); const ContractDeployer = require('./contract_deployer.js'); -const utils = require('../../utils/utils.js'); -//require("../utils/debug_util.js")(__filename, async); +const cloneDeep = require('clone-deep'); class DeployManager { constructor(embark, options) { @@ -43,25 +42,37 @@ class DeployManager { deployAll(done) { let self = this; - self.events.request('contracts:list', (err, contracts) => { - if (err) { - return done(err); - } + self.events.request('contracts:dependencies', (err, contractDependencies) => { + self.events.request('contracts:list', (err, contracts) => { + if (err) { + return done(err); + } - self.logger.info(__("deploying contracts")); - self.events.emit("deploy:beforeAll"); + self.logger.info(__("deploying contracts")); + self.events.emit("deploy:beforeAll"); - const contractsPerDependencyCount = utils.groupBy(contracts, 'dependencyCount'); - async.eachSeries(contractsPerDependencyCount, - function (parallelGroups, callback) { - async.each(parallelGroups, (contract, eachCb) => { + const contractDeploys = {}; + contracts.forEach(contract => { + function deploy(result, callback) { + if (typeof result === 'function') { + callback = result; + } contract._gasLimit = self.gasLimit; self.events.request('deploy:contract', contract, (err) => { - eachCb(err); + callback(err); }); - }, callback); - }, - function (err, _results) { + } + + const className = contract.className; + if (!contractDependencies[className] || contractDependencies[className].length === 0) { + contractDeploys[className] = deploy; + return; + } + contractDeploys[className] = cloneDeep(contractDependencies[className]); + contractDeploys[className].push(deploy); + }); + + async.auto(contractDeploys, function(err, _results) { if (err) { self.logger.error(__("error deploying contracts")); self.logger.error(err.message); @@ -73,8 +84,8 @@ class DeployManager { } self.logger.info(__("finished deploying contracts")); done(err); - } - ); + }); + }); }); } @@ -102,11 +113,11 @@ class DeployManager { }, // TODO: shouldn't be necessary - function checkCompileOnly(callback){ - if(self.onlyCompile){ + function checkCompileOnly(callback) { + if (self.onlyCompile) { self.events.emit('contractsDeployed'); return done(); - } + } return callback(); },