@asyncapi/generator 2.5.0 → 2.7.0

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.
Files changed (50) hide show
  1. package/CHANGELOG.md +146 -0
  2. package/Dockerfile +7 -8
  3. package/cli.js +7 -0
  4. package/docs/api.md +12 -3
  5. package/docs/asyncapi-document.md +2 -1
  6. package/docs/configuration-file.md +112 -33
  7. package/docs/file-templates.md +2 -0
  8. package/docs/generator-template-java.md +560 -0
  9. package/docs/generator-template.md +62 -29
  10. package/docs/hooks.md +3 -3
  11. package/docs/installation-guide.md +5 -51
  12. package/docs/migration-cli.md +70 -0
  13. package/docs/migration-nunjucks-react.md +144 -0
  14. package/docs/nunjucks-render-engine.md +6 -2
  15. package/docs/template.md +2 -2
  16. package/docs/usage.md +14 -32
  17. package/docs/versioning.md +3 -1
  18. package/lib/conditionalGeneration.js +162 -0
  19. package/lib/filtersRegistry.js +5 -1
  20. package/lib/generator.js +96 -43
  21. package/lib/hooksRegistry.js +8 -1
  22. package/lib/logMessages.js +6 -1
  23. package/lib/parser.js +10 -0
  24. package/lib/renderer/nunjucks.js +2 -2
  25. package/lib/templateConfigValidator.js +30 -1
  26. package/package.json +4 -6
  27. package/test/generator.test.js +1 -1
  28. package/test/hooksRegistry.test.js +173 -0
  29. package/test/integration.test.js +51 -4
  30. package/test/parser.test.js +100 -2
  31. package/test/templateConfigValidator.test.js +18 -1
  32. package/test/test-project/README.md +5 -1
  33. package/test/test-project/docker-compose.yml +7 -15
  34. package/test/test-project/package.json +0 -1
  35. package/test/test-project/test-project.test.js +6 -2
  36. package/test/test-project/test-registry.test.js +7 -2
  37. package/test/test-project/test.sh +6 -1
  38. package/test/test-project/verdaccio/config.yaml +6 -0
  39. package/test/test-templates/nunjucks-template/package-lock.json +110 -183
  40. package/test/test-templates/react-template/.ageneratorrc +33 -0
  41. package/test/test-templates/react-template/package.json +5 -20
  42. package/test/test-templates/react-template/template/conditionalFile.txt +0 -0
  43. package/test/test-templates/react-template/template/conditionalFolder/conditionalFile.txt +0 -0
  44. package/test/test-templates/react-template/template/conditionalFolder2/input.txt +0 -0
  45. package/test/test-templates/react-template/template/models.js +6 -0
  46. package/test/utils.test.js +53 -0
  47. package/test/test-project/test-entrypoint.sh +0 -12
  48. package/test/test-templates/react-template/__transpiled/test-file.md.js +0 -24
  49. package/test/test-templates/react-template/__transpiled/test-file.md.js.map +0 -1
  50. package/test/test-templates/react-template/package-lock.json +0 -4135
package/CHANGELOG.md CHANGED
@@ -1,5 +1,151 @@
1
1
  # @asyncapi/generator
2
2
 
3
+ ## 2.7.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 802ad41: ### What's New
8
+
9
+ - Introduced a new `conditionalGeneration` configuration for templates.
10
+ - Supports conditional generation of both files and folders.
11
+ - Enables conditions based on values from the AsyncAPI document (`subject`) and custom template `parameters`.
12
+ - The **`parameter`** allows users to pass custom flags or values at generation time (via the CLI or programmatically) to control what parts of the template get rendered.
13
+ - **Addition of new config file `.ageneratorrc`**: Previously, generator configuration had to be defined in the `package.json` file. Now, you can define the configuration in a separate `.ageneratorrc` file. The configuration defined in the `.ageneratorrc` file will override any configuration defined in `package.json`. The generator will first check for the `.ageneratorrc` file in the template's root directory, and if not found, it will look for the generator config in `package.json`.
14
+
15
+ The `.ageneratorrc` file should be in YAML format.
16
+
17
+ Earlier, the `package.json` for the Python WebSocket client looked like this:
18
+
19
+ ```json
20
+ {
21
+ "name": "@asyncapi/template-python-websocket-client",
22
+ "version": "0.0.1",
23
+ "description": "This is a template generating Python websocket client",
24
+ // ...other configuration...
25
+ "generator": {
26
+ "renderer": "react",
27
+ "apiVersion": "v3",
28
+ "generator": ">=1.3.0 <3.0.0",
29
+ "parameters": {
30
+ "server": {
31
+ "description": "The name of the server described in AsyncAPI document",
32
+ "required": true
33
+ },
34
+ "clientFileName": {
35
+ "description": "The name of the generated client file",
36
+ "required": false,
37
+ "default": "client.py"
38
+ },
39
+ "appendClientSuffix": {
40
+ "description": "Add 'Client' suffix at the end of the class name. This option has no effect if 'customClientName' is specified.",
41
+ "required": false,
42
+ "default": false
43
+ },
44
+ "customClientName": {
45
+ "description": "The custom name for the generated client class",
46
+ "required": false
47
+ }
48
+ }
49
+ }
50
+ }
51
+ ```
52
+
53
+ Now after the introduction of the `.ageneratorrc` file, the `package.json` can be simplified by removing the generator configuration:
54
+
55
+ ```json
56
+ {
57
+ "name": "@asyncapi/template-python-websocket-client",
58
+ "version": "0.0.1",
59
+ "description": "This is a template generating Python websocket client",
60
+ "scripts": {
61
+ "test": "npm run test:cleanup && jest --coverage",
62
+ "test:update": "npm run test -- -u",
63
+ "test:cleanup": "rimraf \"test/temp\"",
64
+ "lint": "eslint --max-warnings 0 --config ../../../../../.eslintrc --ignore-path ../../../../../.eslintignore .",
65
+ "lint:fix": "eslint --fix --max-warnings 0 --config ../../../../../.eslintrc --ignore-path ../../../../../.eslintignore ."
66
+ },
67
+ "author": "Lukasz Gornicki <lpgornicki@gmail.com>",
68
+ "license": "Apache-2.0",
69
+ "dependencies": {
70
+ "@asyncapi/generator-react-sdk": "^1.1.2",
71
+ "@asyncapi/generator-helpers": "1.0.0"
72
+ },
73
+ "devDependencies": {
74
+ "@asyncapi/parser": "^3.0.14",
75
+ "@babel/cli": "^7.25.9",
76
+ "@babel/core": "^7.26.0",
77
+ "@babel/preset-env": "^7.26.0",
78
+ "@babel/preset-react": "^7.25.9",
79
+ "jest-esm-transformer": "^1.0.0",
80
+ "@asyncapi/generator": "*",
81
+ "eslint": "^6.8.0",
82
+ "eslint-plugin-jest": "^23.8.2",
83
+ "eslint-plugin-react": "^7.34.1",
84
+ "eslint-plugin-sonarjs": "^0.5.0",
85
+ "rimraf": "^3.0.2"
86
+ },
87
+ "jest": {
88
+ "moduleFileExtensions": ["js", "json", "jsx"],
89
+ "transform": {
90
+ "^.+\\.jsx?$": "babel-jest"
91
+ },
92
+ "moduleNameMapper": {
93
+ "^nimma/legacy$": "<rootDir>/../../../../../node_modules/nimma/dist/legacy/cjs/index.js",
94
+ "^nimma/(.*)": "<rootDir>/../../../../../node_modules/nimma/dist/cjs/$1"
95
+ }
96
+ },
97
+ "babel": {
98
+ "presets": [
99
+ "@babel/preset-env",
100
+ [
101
+ "@babel/preset-react",
102
+ {
103
+ "runtime": "automatic"
104
+ }
105
+ ]
106
+ ]
107
+ }
108
+ }
109
+ ```
110
+
111
+ And the equivalent `.ageneratorrc` file would be:
112
+
113
+ ```yaml
114
+ renderer: react
115
+ apiVersion: v3
116
+ generator: ">=1.3.0 <3.0.0"
117
+ parameters:
118
+ server:
119
+ description: The name of the server described in AsyncAPI document
120
+ required: true
121
+ clientFileName:
122
+ description: The name of the generated client file
123
+ required: false
124
+ default: client.py
125
+ appendClientSuffix:
126
+ description: Add 'Client' suffix at the end of the class name. This option has no effect if 'customClientName' is specified.
127
+ required: false
128
+ default: false
129
+ customClientName:
130
+ description: The custom name for the generated client class
131
+ required: false
132
+ ```
133
+
134
+ ### Deprecation notice
135
+
136
+ - The existing `conditionalFile` configuration is now deprecated and will be removed in a future release.
137
+ - To migrate, replace `conditionalFile` with `conditionalGeneration` in your template configuration.
138
+
139
+ ## 2.6.0
140
+
141
+ ### Minor Changes
142
+
143
+ - fd5dfd7: - **Deprecation of `ag` CLI**: The `ag` CLI is deprecated in favour of the `AsyncAPI CLI` that is a single entry point for all the AsyncAPI tools. No new features will be added to `ag` and it will be completely removed. The official documentation of AsyncAPI Generator has not mentioned `ag` for over a year, instead only using `AsyncAPI CLI` and `asyncapi generate fromTemplate` commands. Refer to the [migration guide](https://www.asyncapi.com/docs/tools/generator/migration-cli) that will help you understand how to migrate your `ag` commands to the new `AsyncAPI CLI` command.
144
+
145
+ - **Deprecation of Nunjucks render engine:** The [Nunjucks render engine](https://www.asyncapi.com/docs/tools/generator/nunjucks-render-engine) is deprecated and will be removed in October 2025. It is recommended to switch to the [React render engine](https://www.asyncapi.com/docs/tools/generator/react-render-engine) instead. If you are using Nunjucks in production, read the [migration guide](https://www.asyncapi.com/docs/tools/generator/migration-nunjucks-react) that will help you understand how to migrate to the new engine. The removal of the Nunjucks render engine results also in removal of [Nunjucks-filters](apps/nunjucks-filters) library.
146
+
147
+ Removal of both deprecated parts of the generator is planned for October 2025, which gives you 9 months to migrate.
148
+
3
149
  ## 2.5.0
4
150
 
5
151
  ### Minor Changes
package/Dockerfile CHANGED
@@ -1,21 +1,20 @@
1
- ARG ASYNCAPI_GENERATOR_VERSION=1.10.9
2
-
3
1
  FROM node:18-alpine
4
2
 
3
+ ARG ASYNCAPI_GENERATOR_VERSION=1.10.9
4
+
5
5
  WORKDIR /app
6
6
 
7
7
  # Since 0.14.0 release of html-template chromium is needed for pdf generation
8
- ENV PUPPETEER_EXECUTABLE_PATH /usr/bin/chromium-browser
9
- ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
8
+ ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
9
+ ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
10
10
 
11
11
  # Since 0.30.0 release Git is supported and required as a dependency
12
12
  # Since 0.14.0 release of html-template chromium is needed for pdf generation.
13
13
  # More custom packages for specific template should not be added to this dockerfile. Instead, we should come up with some extensibility solution.
14
+ # Installing latest released npm package
14
15
  RUN apk --update add git chromium && \
15
- rm -rf /var/lib/apt/lists/* && \
16
- rm /var/cache/apk/*
16
+ rm /var/cache/apk/* && \
17
+ npm install --ignore-scripts -g "@asyncapi/generator@${ASYNCAPI_GENERATOR_VERSION}"
17
18
 
18
- # Installing latest released npm package
19
- RUN npm install -g @asyncapi/generator@$ASYNCAPI_GENERATOR_VERSION
20
19
 
21
20
  ENTRYPOINT [ "ag" ]
package/cli.js CHANGED
@@ -89,10 +89,17 @@ program
89
89
  .option('-o, --output <outputDir>', 'directory where to put the generated files (defaults to current directory)', parseOutput, process.cwd())
90
90
  .option('-p, --param <name=value>', 'additional param to pass to templates', paramParser)
91
91
  .option('--force-write', 'force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir (defaults to false)')
92
+ .option('--disable-warning', 'disable "ag" deprecation warning (defaults to false)')
92
93
  .option('--watch-template', 'watches the template directory and the AsyncAPI document, and re-generate the files when changes occur. Ignores the output directory. This flag should be used only for template development.')
93
94
  .option('--map-base-url <url:folder>','maps all schema references from base url to local folder',mapBaseUrlParser)
94
95
  .parse(process.argv);
95
96
 
97
+ if (!program.disableWarning) {
98
+ console.warn(yellow(
99
+ 'Warning: The "ag" CLI is deprecated and will be removed in a future release. Please use the AsyncAPI CLI instead. See release notes for details: https://github.com/asyncapi/generator/releases/tag/%40asyncapi%2Fgenerator%402.6.0. You can hide this working using --disable-warning flag.')
100
+ );
101
+ }
102
+
96
103
  if (!asyncapiDocPath) {
97
104
  console.error(red('> Path or URL to AsyncAPI file not provided.'));
98
105
  program.help(); // This exits the process
package/docs/api.md CHANGED
@@ -13,6 +13,7 @@ Reference API documentation for AsyncAPI Generator library.
13
13
  * [Generator](#Generator)
14
14
  * [new Generator(templateName, targetDir, options)](#new_Generator_new)
15
15
  * _instance_
16
+ * [.compile](#Generator+compile) : `Boolean`
16
17
  * [.registry](#Generator+registry) : `Object`
17
18
  * [.templateName](#Generator+templateName) : `String`
18
19
  * [.targetDir](#Generator+targetDir) : `String`
@@ -38,9 +39,9 @@ Reference API documentation for AsyncAPI Generator library.
38
39
  * [.executeAfterHook()](#Generator+executeAfterHook) ⇒ `Promise.<void>`
39
40
  * [.parseInput()](#Generator+parseInput)
40
41
  * [.configureTemplate()](#Generator+configureTemplate)
41
- * ~~[.generateFromString(asyncapiString, [parseOptions])](#Generator+generateFromString) ⇒ `Promise`~~
42
- * [.generateFromURL(asyncapiURL)](#Generator+generateFromURL) ⇒ `Promise`
43
- * [.generateFromFile(asyncapiFile)](#Generator+generateFromFile) ⇒ `Promise`
42
+ * ~~[.generateFromString(asyncapiString, [parseOptions])](#Generator+generateFromString) ⇒ `Promise.<(TemplateRenderResult|undefined)>`~~
43
+ * [.generateFromURL(asyncapiURL)](#Generator+generateFromURL) ⇒ `Promise.<(TemplateRenderResult|undefined)>`
44
+ * [.generateFromFile(asyncapiFile)](#Generator+generateFromFile) ⇒ `Promise.<(TemplateRenderResult|undefined)>`
44
45
  * [.installTemplate([force])](#Generator+installTemplate)
45
46
  * _static_
46
47
  * [.getTemplateFile(templateName, filePath, [templatesDir])](#Generator.getTemplateFile) ⇒ `Promise`
@@ -64,6 +65,7 @@ Instantiates a new Generator object.
64
65
  - [.forceWrite] `Boolean` ` = false` - Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir. Default is set to false.
65
66
  - [.install] `Boolean` ` = false` - Install the template and its dependencies, even when the template has already been installed.
66
67
  - [.debug] `Boolean` ` = false` - Enable more specific errors in the console. At the moment it only shows specific errors about filters. Keep in mind that as a result errors about template are less descriptive.
68
+ - [.compile] `Boolean` ` = true` - Whether to compile the template or use the cached transpiled version provided by template in '__transpiled' folder
67
69
  - [.mapBaseUrlToFolder] `Object.<String, String>` - Optional parameter to map schema references from a base url to a local base folder e.g. url=https://schema.example.com/crm/ folder=./test/docs/ .
68
70
  - [.registry] `Object` - Optional parameter with private registry configuration
69
71
  - [.url] `String` - Parameter to pass npm registry url
@@ -85,6 +87,13 @@ const generator = new Generator('@asyncapi/html-template', path.resolve(__dirnam
85
87
  });
86
88
  ```
87
89
 
90
+ <a name="Generator+compile"></a>
91
+
92
+ * generator.compile : `Boolean`** :
93
+ Whether to compile the template or use the cached transpiled version provided by template in '__transpiled' folder.
94
+
95
+ **Kind**: instance property of [`Generator`](#Generator)
96
+
88
97
  <a name="Generator+registry"></a>
89
98
 
90
99
  * generator.registry : `Object`** :
@@ -68,9 +68,10 @@ function createAsyncapiFile(generator) {
68
68
 
69
69
  const outputFileName = `asyncapi.${extension}`;
70
70
 
71
- const asyncapiOutputLocation = path.resolve('./'', outputFileName);
71
+ const asyncapiOutputLocation = path.resolve('./', outputFileName);
72
72
 
73
73
  fs.writeFileSync(asyncapiOutputLocation, asyncapi);
74
+ }
74
75
  ```
75
76
 
76
77
  ### Method 2: `asyncapi` and template
@@ -3,30 +3,76 @@ title: "Configuration file"
3
3
  weight: 90
4
4
  ---
5
5
 
6
- The `generator` property from `package.json` file must contain a JSON object that may have the following information:
6
+ You can configure your AsyncAPI Generator template using either a dedicated `.ageneratorrc` YAML file or through the `generator` property in your `package.json` file. Previously, generator configuration had to be defined in the `package.json` file. Now, you can define the configuration in a separate `.ageneratorrc` file. The configuration defined in the `.ageneratorrc` file will override any configuration defined in `package.json`. The generator will first check for the `.ageneratorrc` file in the template's root directory, and if not found, it will look for the generator config in `package.json`.
7
7
 
8
- |Name|Type|Description|
9
- |---|---|---|
10
- |`renderer`| String | Its value can be either `react` or `nunjucks` (default).
11
- |`apiVersion`| String | Determines which **major** version of the [Parser-API](https://github.com/asyncapi/parser-api) the template uses. For example, `v2` for `v2.x.x`. If not specified, the Generator assumes the template is not compatible with the Parser-API so it will use the [Parser-JS v1 API](https://github.com/asyncapi/parser-js/tree/v1.18.1#api-documentation). For templates that need to support AsyncAPI specification v3 make sure to use `v3` [Parser-API](https://github.com/asyncapi/parser-api). If the template uses a version of the Parser-API that is not supported by the Generator, the Generator will throw an error.
12
- |`supportedProtocols`| [String] | A list with all the protocols this template supports.
13
- |`parameters`| Object[String, Object] | An object with all the parameters that can be passed when generating the template. When using the command line, it's done by indicating `--param name=value` or `-p name=value`.
14
- |`parameters[param].description`| String | A user-friendly description about the parameter.
15
- |`parameters[param].default`| Any | Default value of the parameter if not specified. Shouldn't be used for mandatory `required=true` parameters.
16
- |`parameters[param].required`| Boolean | Whether the parameter is required or not.
17
- |`conditionalFiles`| Object[String, Object] | An object containing all the file paths that should be conditionally rendered. Each key represents a file path and each value must be an object with the keys `subject` and `validation`. The file path should be relative to the `template` directory inside the template.
18
- |`conditionalFiles[filePath].subject`| String | The `subject` is a [JMESPath](http://jmespath.org/) query to grab the value you want to apply the condition to. It queries an object with the whole AsyncAPI document and, when specified, the given server. The object looks like this: `{ asyncapi: { ... }, server: { ... } }`. If the template supports `server` parameter, you can access server details like for example protocol this way: `server.protocol`. During validation with `conditionalFiles` only the server that template user selected is available in the specification file. For more information about `server` parameter [read about special parameters](#special-parameters).
19
- |`conditionalFiles[filePath].validation`| Object | The `validation` is a JSON Schema Draft 07 object. This JSON Schema definition will be applied to the JSON value resulting from the `subject` query. If validation doesn't have errors, the condition is met, and therefore the given file will be rendered. Otherwise, the file is ignored. Check [JSON Schema Validation](https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6) document for a list of all possible validation keywords.
20
- |`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.
21
- |`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).
22
- |`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 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.
24
8
 
25
- ### Example
9
+ ## Configuration Methods
10
+
11
+ ### Option 1: Using `.ageneratorrc` file (Recommended)
12
+
13
+ Create a `.ageneratorrc` file in the root of your template with YAML syntax. This approach keeps your template configuration separate from package metadata:
14
+
15
+ ```yaml
16
+ renderer: react
17
+ apiVersion: v3
18
+ supportedProtocols:
19
+ - amqp
20
+ - mqtt
21
+
22
+ parameters:
23
+ server:
24
+ description: The server you want to use in the code.
25
+ required: true
26
+ dummyParameter:
27
+ description: Example of parameter with default value.
28
+ default: just a string
29
+ required: false
30
+
31
+ conditionalFiles:
32
+ path/to/file/that/is/relative/to/template/dir/test-amqp.js:
33
+ subject: server.protocol
34
+ validation:
35
+ const: amqp
36
+ path/to/file/that/is/relative/to/template/dir/support.html:
37
+ subject: info.contact
38
+ validation:
39
+ required:
40
+ - url
41
+
42
+ conditionalGeneration:
43
+ conditionalOnFile.js:
44
+ parameter: singleFile
45
+ validation:
46
+ not:
47
+ const: true
48
+ conditionOnFolderInfo:
49
+ subject: info.contact.name
50
+ validation:
51
+ const: API Support
52
+
53
+ nonRenderableFiles:
54
+ - src/api/middlewares/*.*
55
+ - lib/lib/config.js
56
+
57
+ generator: "<2.0.0"
58
+
59
+ filters:
60
+ - my-package-with-filters
61
+
62
+ hooks:
63
+ '@asyncapi/generator-hooks': hookFunctionName
64
+ my-custom-hooks-package:
65
+ - myHook
66
+ - andAnotherOne
67
+
68
+ ```
69
+
70
+ ### Option 2: Using `package.json`
71
+
72
+ Alternatively, you can include your configuration in the `generator` property of your `package.json` file:
26
73
 
27
74
  ```json
28
- "generator":
29
- {
75
+ "generator": {
30
76
  "renderer": "react",
31
77
  "apiVersion": "v3",
32
78
  "supportedProtocols": ["amqp", "mqtt"],
@@ -41,26 +87,32 @@ The `generator` property from `package.json` file must contain a JSON object tha
41
87
  "required": false
42
88
  }
43
89
  },
44
- "conditionalFiles": {
45
- "path/to/file/that/is/relative/to/template/dir/test-amqp.js": {
46
- "subject": "server.protocol",
47
- "validation": {
48
- "const": "amqp"
90
+ "conditionalGeneration": {
91
+ "conditionalOnFile.js": {
92
+ "parameter": "singleFile",
93
+ "validation": {
94
+ "not": { "const": "true" }
95
+ }
96
+ },
97
+ "conditionOnFolder": {
98
+ "parameter": "singleFolder",
99
+ "validation": {
100
+ "not": { "const": "true" }
49
101
  }
50
102
  },
51
- "path/to/file/that/is/relative/to/template/dir/support.html": {
52
- "subject": "info.contact",
53
- "validation": {
54
- "required": ["url"]
103
+ "conditionOnFolderInfo": {
104
+ "subject": "info.contact.name",
105
+ "validation": {
106
+ "const": "API Support"
55
107
  }
56
- }
108
+ },
57
109
  },
58
110
  "nonRenderableFiles": [
59
111
  "src/api/middlewares/*.*",
60
112
  "lib/lib/config.js"
61
113
  ],
62
114
  "generator": "<2.0.0",
63
- "filters":[
115
+ "filters": [
64
116
  "my-package-with-filters"
65
117
  ],
66
118
  "hooks": {
@@ -70,12 +122,39 @@ The `generator` property from `package.json` file must contain a JSON object tha
70
122
  }
71
123
  ```
72
124
 
125
+ > **Note:** If both `.ageneratorrc` file and the `generator` property in `package.json` exist, the configuration from `.ageneratorrc` will override the `package.json` configuration.
126
+
127
+ The `generator` property from `package.json` file and must contain a JSON object and the `ageneratorrc` file must contain a YAML object that may have the following information:
128
+
129
+
130
+ |Name|Type|Description|
131
+ |---|---|---|
132
+ |`renderer`| String | Its value can be either `react` or `nunjucks` (default).
133
+ |`apiVersion`| String | Determines which **major** version of the [Parser-API](https://github.com/asyncapi/parser-api) the template uses. For example, `v2` for `v2.x.x`. If not specified, the Generator assumes the template is not compatible with the Parser-API so it will use the [Parser-JS v1 API](https://github.com/asyncapi/parser-js/tree/v1.18.1#api-documentation). For templates that need to support AsyncAPI specification v3 make sure to use `v3` [Parser-API](https://github.com/asyncapi/parser-api). If the template uses a version of the Parser-API that is not supported by the Generator, the Generator will throw an error.
134
+ |`supportedProtocols`| [String] | A list with all the protocols this template supports.
135
+ |`parameters`| Object[String, Object] | An object with all the parameters that can be passed when generating the template. When using the command line, it's done by indicating `--param name=value` or `-p name=value`.
136
+ |`parameters[param].description`| String | A user-friendly description about the parameter.
137
+ |`parameters[param].default`| Any | Default value of the parameter if not specified. Shouldn't be used for mandatory `required=true` parameters.
138
+ |`parameters[param].required`| Boolean | Whether the parameter is required or not.
139
+ | `conditionalFiles` | Object[String, Object] | An object containing all the file paths that should be conditionally rendered. Each key represents a file path and each value must be an object with the keys `subject` and `validation`. The file path should be relative to the `template` directory inside the template. **Note: It is deprecated and will be removed with future releases. Use `conditionalGeneration` instead.** |
140
+ | `conditionalFiles[filePath].subject` | String | The `subject` is a [JMESPath](http://jmespath.org/) query to grab the value you want to apply the condition to. It queries an object with the whole AsyncAPI document and, when specified, the given server. The object looks like this: `{ asyncapi: { ... }, server: { ... } }`. If the template supports the `server` parameter, you can access server details, for example, `server.protocol`. During validation with `conditionalFiles`, only the server that the template user selected is available in the specification file. For more information about `server` parameter [read about special parameters](#special-parameters). **Note: It is deprecated and will be removed with future releases. Use `conditionalGeneration` instead.** |
141
+ | `conditionalFiles[filePath].validation` | Object | The `validation` is a JSON Schema Draft 07 object. This JSON Schema definition will be applied to the JSON value resulting from the `subject` query. If validation doesn't have errors, the condition is met, and therefore the given file will be rendered. Otherwise, the file is ignored. Check [JSON Schema Validation](https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6) for a list of all possible validation keywords. **Note: It is deprecated and will be removed with future releases. Use `conditionalGeneration` instead.** |
142
+ |`conditionalGeneration` | Object[String, { subject?: String, parameter?: String, validation: Object }] | An object containing all the file paths or directory names that should be conditionally rendered. Each key represents a file path or directory name and each value must be an object with the keys `subject`, `parameter` and `validation`. You can use either subject or parameter according to the use case. The path should be relative to the `template` directory inside the template. **Note: conditionalGeneration and conditionalFile are mutually exclusive, which means both cannot be configured at the same time in the template**. |
143
+ |`conditionalGeneration[filePath/directoryName].subject`| String | The `subject` is a [JMESPath](http://jmespath.org/) query to grab the value you want to apply the condition to. It queries an object with the whole AsyncAPI document and, when specified, the given server. The object looks like this: `{ asyncapi: { ... }, server: { ... } }`. If the template supports the `server` parameter, you can access server details like, for example, protocol this way: `server.protocol`. During validation with `conditionalGeneration`, only the server that the template user selected is available in the specification file. For more information about the `server` parameter [read about special parameters](#special-parameters). |
144
+ |`conditionalGeneration[filePath/directoryName].parameter`| String | The `parameter` is the name of a custom template parameter passed through `templateParams` that controls whether a specific file or folder should be included in the generated output. You must define a `validation` rule using a JSON Schema fragment to apply the condition. For example, if you define `"parameter": "includeDocs"` with `"validation": { "const": true }`, the corresponding folder (e.g., `docs/`) will only be generated when the user passes `{ includeDocs: true }`. If `includeDocs` is `false`, it will be skipped. |
145
+ |`conditionalGeneration[filePath/directoryName].validation`| Object (JSON Schema fragment) | The validation defines the condition under which the file or directory will be generated. It must be a valid JSON Schema fragment that validates the value of the parameter. For example, if you want to include a folder only when includeDocs is true, use "validation": { "const": true }. You can also use more complex validation logic, like "enum": ["yes", "true"] or "type": "string" with a "pattern" constraint. If the parameter fails validation, the file or folder will not be included in the generated output. This allows for powerful and flexible control over template generation based on user input. |
146
+ |`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.
147
+ |`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).
148
+ |`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.
149
+ |`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.
150
+
151
+ ---
152
+
73
153
  ## Special parameters
74
154
 
75
155
  There are some template parameters that have a special meaning:
76
156
 
77
157
  |Name|Description|
78
158
  |---|---|
79
- |`server`| It is used to let the template know which server from the AsyncAPI specification file you want to use. In some cases, this may be required. For instance, when generating code that connects to a specific server. Use this parameter in case your template relies on users' information about what server from the specification file they want to use during generation. You also need this parameter if you want to use `server.protocol` notation within `conditionalFiles` configuration option. Once you decide to specify this parameter for your template, it is recommended you make it a mandatory parameter otherwise a feature like `conditionalFiles` is not going to work if your users do not use this parameter obligatory.
80
-
159
+ |`server`| It is used to let the template know which server from the AsyncAPI specification file you want to use. In some cases, this may be required. For instance, when generating code that connects to a specific server. Use this parameter in case your template relies on users' information about what server from the specification file they want to use during generation. You also need this parameter if you want to use `server.protocol` notation within `conditionalGeneration` configuration option. Once you decide to specify this parameter for your template, it is recommended you make it a mandatory parameter otherwise a feature like `conditionalGeneration` is not going to work if your users do not use this parameter obligatory.
81
160
 
@@ -7,6 +7,8 @@ weight: 140
7
7
 
8
8
  > **Note**: This section applies only to the Nunjucks render engine. For information on using the React render engine, refer to the [Generating files with the React render engine](#generating-files-with-the-react-render-engine) section below.
9
9
 
10
+ > **Note**: Nunjucks renderer engine is deprecated and will be removed in the future. Use the React renderer engine instead. For more details read notes from release [@asyncapi/generator@2.6.0](https://github.com/asyncapi/generator/releases/tag/%40asyncapi%2Fgenerator%402.6.0).
11
+
10
12
  It is possible to generate files for each specific object in your AsyncAPI documentation using the Nunjucks render engine. For example, you can specify a filename like `$$channel$$.js` to generate a file for each channel defined in your AsyncAPI. The following file-template names and extra variables are available:
11
13
 
12
14
  - `$$channel$$`, within the template-file you have access to two variables [`channel`](https://github.com/asyncapi/parser-api/blob/master/docs/api.md#channel) and [`channelName`](https://github.com/asyncapi/parser-api/blob/master/docs/api.md#channels). Where the `channel` contains the current channel being rendered.