Skip to content

Commit

Permalink
feat: add hooks before (#315)
Browse files Browse the repository at this point in the history
* feat: add hooks before

* Rephrasing of description.

* Apply suggestions from code review

fix typos

Co-authored-by: Fran Méndez <[email protected]>

Co-authored-by: Semen Tenishchev <[email protected]>
Co-authored-by: Fran Méndez <[email protected]>
  • Loading branch information
3 people authored Apr 30, 2020
1 parent 97696e9 commit 4e14ab1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
31 changes: 30 additions & 1 deletion docs/authoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ In case you have more than one template and want to reuse filters, you can put t

## Hooks

Hooks are functions called by the generator on a specific moment in the generation process. For now there is one hook supported called `generate:after` that is called at the very end of the generation. The generator will parse all the files in the `.hooks` directory.
Hooks are functions called by the generator on a specific moment in the generation process.
The following types of hooks are currently supported:

|Hook name|Description|
|---|---|
| `generate:before` | Called after registration of all filters and before generator starts processing of the template.|
| `generate:after` | Called at the very end of the generation. |

The generator will parse all the files in the `.hooks` directory.
#### Examples
Below you have an example Hook that after generation creates an AsyncAPI file.

```js
Expand All @@ -100,6 +108,27 @@ module.exports = register => {
});
};
```
And here an example Hook that before generation switches `publish` and `subscribe` operations for each channel.

```js
module.exports = register => {
register('generate:before', generator => {
const asyncapi = generator.asyncapi;
for (let [key, value] of Object.entries(asyncapi.channels())) {
let publish = value._json.publish;
value._json.publish = value._json.subscribe;
value._json.subscribe = publish;
if (!value._json.subscribe) {
delete value._json.subscribe;
}
if (!value._json.publish) {
delete value._json.publish;
}
}
});
};
```


## Partials

Expand Down
1 change: 1 addition & 0 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class Generator {
await this.loadTemplateConfig();
this.registerHooks();
await registerFilters(this.nunjucks, this.templateConfig, this.templateDir, FILTERS_DIRNAME);
await this.launchHook('generate:before');

if (this.entrypoint) {
const entrypointPath = path.resolve(this.templateContentDir, this.entrypoint);
Expand Down

0 comments on commit 4e14ab1

Please sign in to comment.