From 1aeb6fd83b9d565c82d108cfd5564508028aaa6a Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Tue, 9 Apr 2019 16:39:48 +0200 Subject: [PATCH] feat(@embark/embark-specialconfigs): introduce new beforeDeploy hooks This commit enables new `beforeDeploy` deployment hooks. With this change it's possible to add `beforeDeploy` to either `contracts: {}` within the contracts configuration, or an individual contract. For example: ``` contracts: { SimpleStorage: { beforeDeploy: async (context) => { } } } ``` Runs the hook before `SimpleStorage` will be deployed. Whereas ``` contracts: { ... beforeDeploy: async () => { } } will be executed before *all* Smart Contracts will be deployed. --- packages/embark-specialconfigs/src/index.js | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/embark-specialconfigs/src/index.js b/packages/embark-specialconfigs/src/index.js index 803c85ad2..c1c9325c3 100644 --- a/packages/embark-specialconfigs/src/index.js +++ b/packages/embark-specialconfigs/src/index.js @@ -13,7 +13,9 @@ class SpecialConfigs { this.embark = embark; this.config = embark.config; + this.registerBeforeAllDeployAction(); this.registerAfterDeployAction(); + this.registerBeforeDeployAction(); this.registerOnDeployAction(); this.registerDeployIfAction(); } @@ -91,6 +93,21 @@ class SpecialConfigs { return replaceWithAddresses(cmd); } + registerBeforeAllDeployAction() { + this.embark.registerActionForEvent('deploy:beforeAll', async (cb) => { + const beforeDeployFn = this.config.contractsConfig.beforeDeploy; + if (!beforeDeployFn || typeof beforeDeployFn !== 'function') { + return cb(); + } + try { + await beforeDeployFn(); + cb(); + } catch (err) { + cb(new Error(`Error running beforeDeploy hook: ${err.message}`)); + } + }); + } + registerAfterDeployAction() { this.embark.registerActionForEvent("contracts:deploy:afterAll", async (cb) => { if (typeof this.config.contractsConfig.afterDeploy === 'function') { @@ -138,6 +155,23 @@ class SpecialConfigs { }, callback); } + registerBeforeDeployAction() { + this.embark.registerActionForEvent('deploy:contract:beforeDeploy', async (params, cb) => { + const contract = params.contract; + const beforeDeployFn = params.contract.beforeDeploy; + if (!beforeDeployFn || typeof beforeDeployFn !== 'function') { + return cb(); + } + try { + const dependencies = await this.getOnDeployLifecycleHookDependencies(contract); + await beforeDeployFn(dependencies); + cb(); + } catch (e) { + cb(new Error(`Error running beforeDeploy hook for ${contract.className}: ${e.message || e}`)); + } + }); + } + registerOnDeployAction() { this.embark.registerActionForEvent("deploy:contract:deployed", async (params, cb) => { let contract = params.contract;