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.
This commit is contained in:
Pascal Precht 2019-04-09 16:39:48 +02:00 committed by Iuri Matias
parent f0aed55319
commit 1aeb6fd83b
1 changed files with 34 additions and 0 deletions

View File

@ -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;