@asyncapi/generator 1.9.13 → 1.9.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CODEOWNERS +3 -0
- package/README.md +9 -216
- package/docs/README.md +12 -9
- package/docs/api.md +122 -100
- package/docs/asyncapi-document.md +87 -0
- package/docs/configuration-file.md +3 -3
- package/docs/file-templates.md +1 -1
- package/docs/hooks.md +1 -1
- package/docs/index.md +48 -1
- package/docs/installation-guide.md +81 -0
- package/docs/jsdoc2md-handlebars/custom-sig-name.hbs +12 -0
- package/docs/jsdoc2md-handlebars/defaultvalue.hbs +1 -0
- package/docs/jsdoc2md-handlebars/header.hbs +12 -0
- package/docs/jsdoc2md-handlebars/link.hbs +25 -0
- package/docs/jsdoc2md-handlebars/main.hbs +8 -0
- package/docs/jsdoc2md-handlebars/params-table.hbs +9 -0
- package/docs/nunjucks-render-engine.md +2 -2
- package/docs/parser.md +92 -2
- package/docs/react-render-engine.md +3 -3
- package/docs/special-file-names.md +1 -1
- package/docs/template-context.md +41 -0
- package/docs/template-development.md +153 -0
- package/docs/template.md +69 -2
- package/docs/typescript-support.md +2 -2
- package/docs/usage.md +151 -0
- package/docs/versioning.md +31 -0
- package/lib/generator.js +2 -2
- package/package.json +4 -4
- package/.releaserc +0 -10
- package/docs/asyncapi-context.md +0 -6
- package/docs/asyncapi-file.md +0 -6
- package/docs/authoring-templates.md +0 -17
package/docs/usage.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Usage"
|
|
3
|
+
weight: 30
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
There are two ways to use the generator:
|
|
7
|
+
- [Generator CLI](#generator-cli)
|
|
8
|
+
- [Generator library](#using-as-a-modulepackage)
|
|
9
|
+
|
|
10
|
+
## Generator CLI
|
|
11
|
+
```bash
|
|
12
|
+
Usage: asyncapi generate fromTemplate <asyncapi> <template> [<options>]
|
|
13
|
+
|
|
14
|
+
- <asyncapi>: Local path or URL pointing to AsyncAPI document for example https://bit.ly/asyncapi
|
|
15
|
+
- <template>: Name of the generator template like for example @asyncapi/html-template or https://github.com/asyncapi/html-template
|
|
16
|
+
|
|
17
|
+
- <options>:
|
|
18
|
+
-V, --version output the generator version
|
|
19
|
+
-d, --disable-hook [hooks...] disable a specific hook type or hooks from a given hook type
|
|
20
|
+
--debug enable more specific errors in the console
|
|
21
|
+
-i, --install install the template and its dependencies (defaults to false)
|
|
22
|
+
-n, --no-overwrite <glob> glob or path of the file(s) to skip when regenerating
|
|
23
|
+
-o, --output <outputDir> directory to put the generated files (defaults to current directory)
|
|
24
|
+
-p, --param <name=value> additional parameters to pass to templates
|
|
25
|
+
--force-write force writing of the generated files to a given directory even if it is a Git repository with unstaged files or not empty dir (defaults to false)
|
|
26
|
+
--watch-template watches the template directory and the AsyncAPI document, and re-generates the files when changes occur. Ignores the output directory. This flag should be used only for template development.
|
|
27
|
+
--map-base-url <url:folder> maps all schema references from base URL to local folder
|
|
28
|
+
-h, --help display help for command
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
All templates are installable npm packages. Therefore, the value of `template` can be anything supported by `npm install`. Here's a summary of the possibilities:
|
|
32
|
+
```
|
|
33
|
+
npm install [<@scope>/]<name>
|
|
34
|
+
npm install [<@scope>/]<name>@<tag>
|
|
35
|
+
npm install [<@scope>/]<name>@<version>
|
|
36
|
+
npm install [<@scope>/]<name>@<version range>
|
|
37
|
+
npm install <git-host>:<git-user>/<repo-name>
|
|
38
|
+
npm install <git repo url>
|
|
39
|
+
npm install <tarball file>
|
|
40
|
+
npm install <tarball url>
|
|
41
|
+
npm install <folder>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Global templates installed with `yarn` or `npm`
|
|
45
|
+
|
|
46
|
+
You can preinstall templates globally before installing the generator CLI. The generator first tries to locate the template in local dependencies; if absent it checks where the global generator packages are installed.
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm install -g @asyncapi/html-template@0.16.0
|
|
50
|
+
asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template
|
|
51
|
+
# The generator uses html-template version 0.16.0 and not the latest version.
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### CLI usage examples
|
|
55
|
+
|
|
56
|
+
**The shortest possible syntax:**
|
|
57
|
+
```bash
|
|
58
|
+
asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**Generating from a URL:**
|
|
62
|
+
```bash
|
|
63
|
+
asyncapi generate fromTemplate https://bit.ly/asyncapi @asyncapi/html-template
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**Specify where to put the result:**
|
|
67
|
+
```bash
|
|
68
|
+
asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template -o ./docs
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Passing parameters to templates:**
|
|
72
|
+
```bash
|
|
73
|
+
asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template -o ./docs -p title='Hello from param'
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
In the template you can use it like this: ` {{ params.title }}`
|
|
77
|
+
|
|
78
|
+
**Disabling the hooks:**
|
|
79
|
+
```bash
|
|
80
|
+
asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template -o ./docs -d generate:before generate:after=foo,bar
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The generator skips all hooks of the `generate:before` type and `foo`, `bar` hooks of the `generate:after` type.
|
|
84
|
+
|
|
85
|
+
**Installing the template from a folder:**
|
|
86
|
+
```bash
|
|
87
|
+
asyncapi generate fromTemplate asyncapi.yaml ~/my-template
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
It creates a symbolic link to the target directory (`~/my-template` in this case).
|
|
91
|
+
|
|
92
|
+
**Installing the template from a git URL:**
|
|
93
|
+
```bash
|
|
94
|
+
asyncapi generate fromTemplate asyncapi.yaml https://github.com/asyncapi/html-template.git
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**Map schema references from baseUrl to local folder:**
|
|
98
|
+
```bash
|
|
99
|
+
asyncapi generate fromTemplate test/docs/apiwithref.json @asyncapi/html-template -o ./build/ --force-write --map-base-url https://schema.example.com/crm/:./test/docs/
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The parameter `--map-base-url` maps external schema references to local folders.
|
|
103
|
+
|
|
104
|
+
### CLI usage with Docker
|
|
105
|
+
|
|
106
|
+
When using our docker image that we regularly update, you don't need to install Node.js or Npm, even though the generator is written with it since the Docker image has the generator installed.
|
|
107
|
+
|
|
108
|
+
Install [Docker](https://docs.docker.com/get-docker/) first, then use docker to pull and run the image using the following command:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
docker run --rm -it \
|
|
112
|
+
-v [ASYNCAPI SPEC FILE LOCATION]:/app/asyncapi.yml \
|
|
113
|
+
-v [GENERATED FILES LOCATION]:/app/output \
|
|
114
|
+
asyncapi/generator [COMMAND HERE]
|
|
115
|
+
|
|
116
|
+
# Example that you can run inside the generator directory after cloning this repository. First, you specify the mount in the location of your AsyncAPI specification file and then you mount it in the directory where the generation result should be saved.
|
|
117
|
+
docker run --rm -it \
|
|
118
|
+
-v ${PWD}/test/docs/dummy.yml:/app/asyncapi.yml \
|
|
119
|
+
-v ${PWD}/output:/app/output \
|
|
120
|
+
asyncapi/generator -o /app/output /app/asyncapi.yml @asyncapi/html-template --force-write
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### CLI usage with `npx` instead of `npm`
|
|
124
|
+
|
|
125
|
+
[npx](https://www.npmjs.com/package/npx) is very useful when you want to run the generator in a CI/CD environment. In such a scenario, do not install the generator globally because most environments that provide Node.js and Npm, also provide npx out of the box.
|
|
126
|
+
|
|
127
|
+
Use the following npx command on your terminal:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
npx -p @asyncapi/cli asyncapi generate fromTemplate ./asyncapi.yaml @asyncapi/html-template
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Using as a module/package
|
|
134
|
+
Once you install the generator in your project, you can use it to generate whatever you want. The following code snippet is an example of HTML generation using the official `@asyncapi/html-template` template and fetching the spec document from the server using:
|
|
135
|
+
```
|
|
136
|
+
https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0/examples/2.0.0/streetlights.yml
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
const path = require('path');
|
|
141
|
+
const generator = new Generator('@asyncapi/html-template', path.resolve(__dirname, 'example'));
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
await generator.generateFromURL('https://raw.githubusercontent.com/asyncapi/asyncapi/2.0.0/examples/2.0.0/streetlights.yml');
|
|
145
|
+
console.log('Done!');
|
|
146
|
+
} catch (e) {
|
|
147
|
+
console.error(e);
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
See the [API documentation](api.md) for more examples and full API reference information.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Generator version vs template version"
|
|
3
|
+
weight: 70
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
The generator tool generates whatever you want, as long as it can be defined in a template based on the [AsyncAPI document](asyncapi-document). On the other hand, a **template** is a file or group of files that specify the kind of output you expect from using the generator's features. For example, you may want to use the [NodeJS template](https://github.com/asyncapi/nodejs-template) to generate boilerplate code for your message-based APIs.
|
|
7
|
+
|
|
8
|
+
Templates are dependent on the generators' features. For example, the template you want to use may be compatible with the latest generator version but incompatible with the previous versions. Check the configuration file or ReadME of the template to see the version of the generator it supports. The generator has an `isTemplateCompatible` function that checks if the template is compatible with the version of the generator you want to use. If the template isn't compatible, you will see a terminal error output similar to the following:
|
|
9
|
+
```
|
|
10
|
+
Something went wrong:
|
|
11
|
+
Error: This template is not compatible with the current version of the generator (${generatorVersion}). This template is compatible with the following version range: ${generator}.`)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
> Use the following command to check the version of the AsyncAPI CLI you have installed; `asyncapi --version`
|
|
15
|
+
|
|
16
|
+
It is better to lock a specific version of the template and the generator if you plan to use the AsyncAPI CLI and a particular template in production. The differences between using the version of the AsyncAPI CLI you have installed and locking a certain version on production are demonstrated in the following code snippets.
|
|
17
|
+
|
|
18
|
+
Generate HTML with the latest AsyncAPI CLI using the html-template.
|
|
19
|
+
```
|
|
20
|
+
npm install -g @asyncapi/cli
|
|
21
|
+
asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template -o ./docs
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Generate HTML using a particular version of the AsyncAPI CLI using the html-template.
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
npm install -g @asyncapi/cli@0.20.0
|
|
28
|
+
asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template@0.7.0 -o ./docs
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
> Before using newer versions of the template, always look at the [changelog](https://github.com/asyncapi/html-template/releases) first. If the generator's features are not important to you, just make sure to use a version compatible with your template.
|
package/lib/generator.js
CHANGED
|
@@ -334,11 +334,11 @@ class Generator {
|
|
|
334
334
|
* Returns the content of a given template file.
|
|
335
335
|
*
|
|
336
336
|
* @example
|
|
337
|
-
* const Generator = require('asyncapi
|
|
337
|
+
* const Generator = require('@asyncapi/generator');
|
|
338
338
|
* const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html');
|
|
339
339
|
*
|
|
340
340
|
* @example <caption>Using a custom `templatesDir`</caption>
|
|
341
|
-
* const Generator = require('asyncapi
|
|
341
|
+
* const Generator = require('@asyncapi/generator');
|
|
342
342
|
* const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html', '~/my-templates');
|
|
343
343
|
*
|
|
344
344
|
* @static
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asyncapi/generator",
|
|
3
|
-
"version": "1.9.
|
|
3
|
+
"version": "1.9.15",
|
|
4
4
|
"description": "The AsyncAPI generator. It can generate documentation, code, anything!",
|
|
5
5
|
"main": "./lib/generator.js",
|
|
6
6
|
"bin": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"test:integration": "npm run test:cleanup && jest --testPathPattern=integration --modulePathIgnorePatterns='./__mocks__'",
|
|
19
19
|
"test:cli": "node cli.js ./test/docs/dummy.yml @asyncapi/html-template -o test/output --force-write --debug && test -e test/output/index.html",
|
|
20
20
|
"test:cleanup": "rimraf \"test/temp\"",
|
|
21
|
-
"docs": "jsdoc2md lib/generator.js > docs/api.md",
|
|
21
|
+
"docs": "jsdoc2md --partial docs/jsdoc2md-handlebars/custom-sig-name.hbs docs/jsdoc2md-handlebars/main.hbs docs/jsdoc2md-handlebars/docs.hbs docs/jsdoc2md-handlebars/header.hbs docs/jsdoc2md-handlebars/defaultvalue.hbs docs/jsdoc2md-handlebars/link.hbs docs/jsdoc2md-handlebars/params-table.hbs --files lib/generator.js > docs/api.md",
|
|
22
22
|
"docker:build": "docker build -t asyncapi/generator:latest .",
|
|
23
23
|
"lint": "eslint --max-warnings 0 --config .eslintrc .",
|
|
24
24
|
"lint:tpl:validator": "eslint --fix --config .eslintrc .github/templates-list-validator",
|
|
@@ -73,8 +73,8 @@
|
|
|
73
73
|
"semver": "^7.3.2",
|
|
74
74
|
"simple-git": "^3.3.0",
|
|
75
75
|
"source-map-support": "^0.5.19",
|
|
76
|
-
"ts-node": "^9.1
|
|
77
|
-
"typescript": "^4.
|
|
76
|
+
"ts-node": "^10.9.1",
|
|
77
|
+
"typescript": "^4.9.3"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
80
|
"eslint": "^6.8.0",
|
package/.releaserc
DELETED
package/docs/asyncapi-context.md
DELETED
package/docs/asyncapi-file.md
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "Authoring Templates"
|
|
3
|
-
weight: 50
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
The AsyncAPI generator has been built with extensibility in mind. The package uses a set of default templates to let you generate documentation and code. However, you can create and use your own templates. In this section, you learn how to create your own one.
|
|
7
|
-
|
|
8
|
-
To work on a template, you need an AsyncAPI specification file that you can use for testing. For this purpose, you can use [this](../test/docs/dummy.yml) dummy file as it's purpose is to cover as many features of AsyncAPI as possible. Do not copy this file to your template but use it directly from this repo like this: `ag https://raw.githubusercontent.com/asyncapi/generator/master/test/docs/dummy.yml ./your-template`
|
|
9
|
-
|
|
10
|
-
> In case you find some features missing or other possible improvements in the dummy file, suggest changes. The goal is to build a file that all templates can use and check their specification features coverage.
|
|
11
|
-
|
|
12
|
-
1. A template is a directory in your file system.
|
|
13
|
-
1. The template can have own dependencies. Just create `package.json` for the template. The generator makes sure to trigger the installation of dependencies.
|
|
14
|
-
1. Templates can be configured. Configuration must be stored in the `package.json` file under custom `generator` property. [Read more about the configuration file](#configuration-file).
|
|
15
|
-
1. The template engine can be either [React](#react) (recommended) or [Nunjucks](#nunjucks) (default). This can be controlled with the `renderer` property in the [template configuration](#template-configuration).
|
|
16
|
-
1. Templates may contain `hooks` that are functions invoked during specific moment of the generation. In the template, they must be stored in the `hooks` directory under the template directory. They can also be stored in other modules and external libraries and configured inside the template [Read more about hooks](#hooks).
|
|
17
|
-
1. There are parameters with special meaning. [Read more about special parameters](#special-parameters).
|