-
-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add hooks library to generator #1304
Changes from all commits
e7df724
554bdc6
7a79a0b
aa35277
49042ac
a8a6571
35ba5fb
42493f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@asyncapi/generator": minor | ||
--- | ||
|
||
- 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, | ||
- 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. | ||
- 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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
|
||
**/node_modules/ | ||
|
||
.DS_Store | ||
# Local env files | ||
.env | ||
.env.local | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,21 @@ This is a Monorepo managed using [Turborepo](https://turbo.build/) and contains | |
|
||
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. | ||
|
||
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. | ||
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. | ||
|
||
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. | ||
|
||
![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) | ||
|
||
> 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. v1 of asyncapi was ages before, better remove it as it only confuse instead of helping |
||
![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) | ||
|
||
<!-- toc is generated with GitHub Actions do not remove toc markers --> | ||
|
||
<!-- toc --> | ||
|
||
- [Overview](#overview) | ||
- [List of official generator templates](#list-of-official-generator-templates) | ||
- [Filters](#filters) | ||
- [Hooks](#hooks) | ||
- [Contributing](#contributing) | ||
- [Contributors ✨](#contributors-%E2%9C%A8) | ||
|
||
|
@@ -53,18 +55,24 @@ There is a large number of templates that are ready to use and are officially su | |
|
||
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)** | ||
|
||
# Generator Filters | ||
## 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. | ||
`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. | ||
|
||
This library consists of: | ||
|
||
- Custom filters. Check out [API docs](apps/nunjucks-filters/docs/api.md) for complete list | ||
- Lodash-powered filters. For the list of all available filters check [official docs](https://lodash.com/docs/) | ||
|
||
## Hooks | ||
|
||
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. | ||
|
||
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). | ||
|
||
## Contributing | ||
|
||
For developement setup you can follow the detailed guide in [Developement guide](Development.md) | ||
For the development setup, you can follow the detailed guide in [Developement guide](Development.md) | ||
|
||
Read [CONTRIBUTING](CONTRIBUTING.md) guide. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ weight: 130 | |
--- | ||
|
||
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. | ||
|
||
## Types | ||
|
||
The following types of hooks are currently supported: | ||
|
||
|Hook type|Description| Return type | Arguments | ||
|
@@ -12,13 +15,15 @@ The following types of hooks are currently supported: | |
| `generate:after` | Called at the very end of the generation. | void : Nothing is expected to be returned. | [The generator instance](/api) | ||
| `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 }` | ||
|
||
## Location | ||
|
||
The generator parses: | ||
- All the files in the `.hooks` directory inside the template. | ||
- 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). | ||
- 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). | ||
|
||
### Examples | ||
## Examples | ||
|
||
> 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). | ||
> 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). | ||
|
||
Most basic modules with hooks look like this: | ||
```js | ||
|
@@ -79,3 +84,35 @@ module.exports = { | |
}; | ||
}; | ||
``` | ||
|
||
## Official library | ||
|
||
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. | ||
|
||
This library consists of the following hooks: | ||
|Hook name|Hook type|Description| | ||
|---|---|---| | ||
| `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 | | ||
|
||
1. In your template configuration in `package.json` specify you want to use this library and what hook exactly: | ||
```json | ||
{ | ||
"generator": { | ||
"hooks": { | ||
"@asyncapi/generator-hooks": "createAsyncapiFile" | ||
} | ||
} | ||
} | ||
``` | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it shows up like an error but it is all good: https://github.com/asyncapi/generator/blob/aa35277c7384a2b33767cd8706da615bdcbd1292/apps/generator/docs/hooks.md#official-library |
||
```json | ||
{ | ||
"generator": { | ||
"parameters": { | ||
"asyncapiFileDir": { | ||
"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." | ||
} | ||
} | ||
} | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding changeset as we need to release a new 2.5 version
have a look if all is clear