Skip to content

Commit 2d16234

Browse files
authored
feat: add hooks library to generator (#1304)
1 parent 261b66e commit 2d16234

File tree

14 files changed

+581
-27
lines changed

14 files changed

+581
-27
lines changed

.changeset/hooks-in.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@asyncapi/generator": minor
3+
---
4+
5+
- Package `@asyncapi/generator-hooks` is now part of `generator` repo and won't be released separately. Theource code is stored under `apps/hooks` but the `package/library` name stays as it was originally for backward compatibility,
6+
- By default, the `@asyncapi/generator-hooks` package, known as **package** contains many different hooks used in templates and is available in the generator. You no longer have to configure it in your `package.json` in `dependencies`. The package, `@asyncapi/generator-hooks` will no longer be published to NPM separately and is deprecated. You can still have your own hooks, store them in a separate package, and configure them with your template.
7+
- Remember that the fact that the hooks package is now included by default, doesn't mean all hooks from it are enabled by default. You still have to enable a given hook in the configuration file explicitly because some hooks can execute automatically without passing a specific parameter. Also, a hook's supported parameters need to be defined in your template's config.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
**/node_modules/
3-
3+
.DS_Store
44
# Local env files
55
.env
66
.env.local

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ This is a Monorepo managed using [Turborepo](https://turbo.build/) and contains
44

55
1. [Generator](apps/generator): This is a tool that you can use to generate whatever you want basing on the AsyncAPI specification file as an input.
66

7-
2. [Nunjucks-filters](apps/nunjucks-filters): This library contains generator filters that can be reused across multiple templates, helping to avoid redundant work. These filters are designed specifically for Nunjucks templates and are included by default with the generator, so there's no need to add them to dependencies seprately.
7+
1. [Hooks](apps/hooks): This library contains generator filters that can be reused across multiple templates, helping to avoid redundant work. Hooks are designed to let template developers hook into the template generation process. For example, one can create a hook code that will be automatically invoked right after the template generation process has ended.
88

9+
1. [Nunjucks-filters](apps/nunjucks-filters): This library contains generator filters that can be reused across multiple templates, helping to avoid redundant work. These filters are designed specifically for Nunjucks templates and are included by default with the generator, so there's no need to add them to dependencies separately.
910

10-
![npm](https://img.shields.io/npm/v/@asyncapi/generator?style=for-the-badge) ![npm](https://img.shields.io/npm/dt/@asyncapi/generator?style=for-the-badge)
1111

12-
> warning: This package doesn't support AsyncAPI 1.x anymore. We recommend to upgrade to the latest AsyncAPI version using the [AsyncAPI converter](https://github.com/asyncapi/converter-js) (You can refer to [installation guide](/apps/generator//docs//installation-guide.md)). If you need to convert documents on the fly, you may use the [Node.js](https://github.com/asyncapi/converter-js) or [Go](https://github.com/asyncapi/converter-go) converters.
12+
![npm](https://img.shields.io/npm/v/@asyncapi/generator?style=for-the-badge) ![npm](https://img.shields.io/npm/dt/@asyncapi/generator?style=for-the-badge)
1313

1414
<!-- toc is generated with GitHub Actions do not remove toc markers -->
1515

1616
<!-- toc -->
1717

1818
- [Overview](#overview)
1919
- [List of official generator templates](#list-of-official-generator-templates)
20+
- [Filters](#filters)
21+
- [Hooks](#hooks)
2022
- [Contributing](#contributing)
2123
- [Contributors ✨](#contributors-%E2%9C%A8)
2224

@@ -53,18 +55,24 @@ There is a large number of templates that are ready to use and are officially su
5355

5456
You can find above templates and the ones provided by the community in **[this list](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate)**
5557

56-
# Generator Filters
58+
## Filters
5759

58-
This library contains generator filters that can be reused across multiple templates, helping to avoid redundant work. These filters are designed specifically for Nunjucks templates and are included by default with the generator, so there's no need to add them to dependencies seprately.
60+
`apps/nunjucks-filters` library contains generator filters that can be reused across multiple templates, helping to avoid redundant work. These filters are designed specifically for Nunjucks templates and are included by default with the generator, so there's no need to add them to dependencies separately.
5961

6062
This library consists of:
6163

6264
- Custom filters. Check out [API docs](apps/nunjucks-filters/docs/api.md) for complete list
6365
- Lodash-powered filters. For the list of all available filters check [official docs](https://lodash.com/docs/)
6466

67+
## Hooks
68+
69+
The `apps/hooks` library contains generator filters that can be reused across multiple templates, helping to avoid redundant work. [Hooks](https://www.asyncapi.com/docs/tools/generator/hooks) are functions called by the generator at specific moments in the generation process. Hooks can be anonymous functions, but you can also assign them function names. These hooks can have arguments provided to them, or they may be expected to return a value.
70+
71+
These hooks are included in the generator without adding any specific dependency to the library. You still have to enable the given hook in the configuration explicitly because some hooks can execute automatically without passing a specific parameter. [Learn more about configuration and what hooks are available out of the box](https://www.asyncapi.com/docs/tools/generator/hooks#official-library).
72+
6573
## Contributing
6674

67-
For developement setup you can follow the detailed guide in [Developement guide](Development.md)
75+
For the development setup, you can follow the detailed guide in [Developement guide](Development.md)
6876

6977
Read [CONTRIBUTING](CONTRIBUTING.md) guide.
7078

apps/generator/docs/configuration-file.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The `generator` property from `package.json` file must contain a JSON object tha
2020
|`nonRenderableFiles`| [String] | A list of file paths or [globs](https://en.wikipedia.org/wiki/Glob_(programming)) that must be copied "as-is" to the target directory, i.e., without performing any rendering process. This is useful when you want to copy binary files.
2121
|`generator`| [String] | A string representing the generator version-range the template is compatible with. This value must follow the [semver](https://nodejs.dev/learn/semantic-versioning-using-npm) syntax. E.g., `>=1.0.0`, `>=1.0.0 <=2.0.0`, `~1.0.0`, `^1.0.0`, `1.0.0`, etc. [Read more about semver](https://docs.npmjs.com/about-semantic-versioning).
2222
|`filters`| [String] | A list of modules containing functions that can be used as Nunjucks filters. In case of external modules, remember they need to be added as a dependency in `package.json` of your template.
23-
|`hooks`| Object[String, String] or Object[String, Array[String]] | A list of modules containing hooks, except for the ones you keep locally in your template in default location. For each module you must specify the exact name of the hook that should be used in the template. For a single hook you can specify it as a string, for more you must pass an array of strings. In case of external modules, remember they need to be added as a dependency in `package.json` of your template.
23+
|`hooks`| Object[String, String] or Object[String, Array[String]] | A list of modules containing hooks, except for the ones you keep locally in your template in the default location. For each module you must specify the exact name of the hook that should be used in the template. For a single hook, you can specify it as a string; for more hooks, you must pass an array of strings. In the case of external modules, remember they need to be added as a dependency in `package.json` of your template. There is also [an official hooks library](hooks#official-library) always included in the generator. As this is a library of multiple hooks, you still need to explicitly specify in the configuration which one you want to use. Use `@asyncapi/generator-hooks` as the library name.
2424

2525
### Example
2626

@@ -64,7 +64,8 @@ The `generator` property from `package.json` file must contain a JSON object tha
6464
"my-package-with-filters"
6565
],
6666
"hooks": {
67-
"@asyncapi/generator-hooks": "hookFunctionName"
67+
"@asyncapi/generator-hooks": "hookFunctionName",
68+
"my-custom-hooks-package": ["myHook", "andAnotherOne"]
6869
}
6970
}
7071
```

apps/generator/docs/hooks.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ weight: 130
44
---
55

66
Hooks are functions called by the generator on a specific moment in the generation process. Hooks can be anonymous functions but you can also add function names. These hooks can have arguments provided to them or being expected to return a value.
7+
8+
## Types
9+
710
The following types of hooks are currently supported:
811

912
|Hook type|Description| Return type | Arguments
@@ -12,13 +15,15 @@ The following types of hooks are currently supported:
1215
| `generate:after` | Called at the very end of the generation. | void : Nothing is expected to be returned. | [The generator instance](/api)
1316
| `setFileTemplateName ` | Called right before saving a new file generated by [file template](./file-templates.md). | string : a new filename for the generator to use for the file template. | [The generator instance](/api) and object in the form of `{ "originalFilename" : string }`
1417

18+
## Location
19+
1520
The generator parses:
1621
- All the files in the `.hooks` directory inside the template.
17-
- All modules listed in the template configuration and triggers only hooks that names were added to the config. You can use the official AsyncAPI [hooks library](https://github.com/asyncapi/generator-hooks). To learn how to add hooks to configuration [read more about the configuration file](https://www.asyncapi.com/docs/tools/generator/configuration-file).
22+
- All modules listed in the template configuration and triggers only hooks whose names were added to the config. You can use an [official hooks library](#official-library) that is bundled together with the generator. To learn how to add hooks to configuration [read more about the configuration file](configuration-file).
1823

19-
### Examples
24+
## Examples
2025

21-
> Some of the examples have names of hook functions provided and some not. Keep in mind that hook functions kept in template in default location do not require a name. Name is required only if you keep hooks in non default location or in a separate library, because such hooks need to be explicitly configured in the configuration file. For more details on hooks configuration [read more about the configuration file](https://www.asyncapi.com/docs/tools/generator/configuration-file).
26+
> Some of the examples have names of hook functions provided and some not. Keep in mind that hook functions kept in template in default location do not require a name. Name is required only if you keep hooks in non default location or in a separate library, because such hooks need to be explicitly configured in the configuration file. For more details on hooks configuration [read more about the configuration file](configuration-file).
2227
2328
Most basic modules with hooks look like this:
2429
```js
@@ -79,3 +84,35 @@ module.exports = {
7984
};
8085
};
8186
```
87+
88+
## Official library
89+
90+
It is a library of reusable hooks that you can use in your templates. You only have to add its name to the configuration: `@asyncapi/generator-hooks` and specify which hook you want to enable.
91+
92+
This library consists of the following hooks:
93+
|Hook name|Hook type|Description|
94+
|---|---|---|
95+
| `createAsyncapiFile` | `generate:after` | It creates an AsyncAPI file with the content of the spec file passed to the generator. By default, it creates the file in the root of the generation output directory. This hook also supports custom parameters that the user can pass to template generation. The parameter called `asyncapiFileDir` allows the user to specify the location where the spec file should be created. To make your template users use this parameter, you need to add it to the configuration of your template like other parameters |
96+
97+
1. In your template configuration in `package.json` specify you want to use this library and what hook exactly:
98+
```json
99+
{
100+
"generator": {
101+
"hooks": {
102+
"@asyncapi/generator-hooks": "createAsyncapiFile"
103+
}
104+
}
105+
}
106+
```
107+
1. Some hooks support custom parameters that template's user can use to specify different behaviour of the hook. To enable these, you need to also add them to the list of your template's parameters:
108+
```json
109+
{
110+
"generator": {
111+
"parameters": {
112+
"asyncapiFileDir": {
113+
"description": "This template by default also outputs the AsyncAPI document that was passed as input. You can specify with this parameter what should be the location of this AsyncAPI document, relative to specified template output."
114+
}
115+
}
116+
}
117+
}
118+
```

apps/generator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@asyncapi/generator-react-sdk": "^1.1.2",
5353
"@asyncapi/multi-parser": "^2.1.1",
5454
"@asyncapi/nunjucks-filters": "*",
55+
"@asyncapi/generator-hooks": "*",
5556
"@asyncapi/parser": "^3.0.14",
5657
"@npmcli/arborist": "5.6.3",
5758
"@npmcli/config": "^8.0.2",

0 commit comments

Comments
 (0)