@asyncapi/generator
Advanced tools
Comparing version 0.45.1 to 0.46.0
@@ -56,3 +56,3 @@ #!/usr/bin/env node | ||
}) | ||
.option('-d, --disable-hook <hookName>', 'disable a specific hook', disableHooksParser) | ||
.option('-d, --disable-hook <hookType>', 'disable a specific hook type', disableHooksParser) | ||
.option('--debug', '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') | ||
@@ -59,0 +59,0 @@ .option('-i, --install', 'installs the template and its dependencies (defaults to false)') |
@@ -41,3 +41,3 @@ <a name="Generator"></a> | ||
| [options.noOverwriteGlobs] | <code>Array.<String></code> | | List of globs to skip when regenerating the template. | | ||
| [options.disabledHooks] | <code>Array.<String></code> | | List of hooks to disable. | | ||
| [options.disabledHooks] | <code>Array.<String></code> | | List of hook types to disable. | | ||
| [options.output] | <code>String</code> | <code>'fs'</code> | Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set. | | ||
@@ -44,0 +44,0 @@ | [options.forceWrite] | <code>Boolean</code> | <code>false</code> | 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. | |
@@ -12,3 +12,3 @@ # Authoring templates | ||
1. Templates may contain [Nunjucks filters or helper functions](https://mozilla.github.io/nunjucks/templating.html#builtin-filters). [Read more about filters](#filters). | ||
1. Templates may contain `hooks` that are functions invoked after the generation. They must be stored in the `.hooks` directory under the template directory. [Read more about hooks](#hooks). | ||
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). | ||
1. Templates may contain `partials` (reusable chunks). They must be stored in the `.partials` directory under the template directory. [Read more about partials](#partials). | ||
@@ -82,6 +82,6 @@ 1. Templates may have a configuration file. It must be stored in the template directory and its name must be `.tp-config.json`. [Read more about the configuration file](#configuration-file). | ||
Hooks are functions called by the generator on a specific moment in the generation process. These hooks can have arguments provided to them or being expected to return a value. | ||
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. | ||
The following types of hooks are currently supported: | ||
|Hook name|Description| Return type | Arguments | ||
|Hook type|Description| Return type | Arguments | ||
|---|---|---|---| | ||
@@ -92,6 +92,17 @@ | `generate:before` | Called after registration of all filters and before generator starts processing of the template. | void : Nothing is expected to be returned. | [The generator instance](https://github.com/asyncapi/generator/blob/master/docs/api.md) | ||
The generator will parse all the files in the `.hooks` directory. | ||
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](#configuration-file). | ||
#### Examples | ||
#### Examples | ||
> Some of examples have name 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 module with hooks looke like this: | ||
```js | ||
module.exports = { | ||
'generate:after': generator => console.log('This runs after generation is complete') | ||
} | ||
``` | ||
Below you have an example Hook that after generation creates an AsyncAPI file. | ||
@@ -103,4 +114,4 @@ | ||
module.exports = register => { | ||
register('generate:after', generator => { | ||
module.exports = { | ||
'generate:after': generator => { | ||
const asyncapi = generator.originalAsyncAPI; | ||
@@ -117,3 +128,3 @@ let extension; | ||
fs.writeFileSync(path.resolve(generator.targetDir, `asyncapi.${extension}`), asyncapi); | ||
}); | ||
} | ||
}; | ||
@@ -124,4 +135,4 @@ ``` | ||
```js | ||
module.exports = register => { | ||
register('generate:before', generator => { | ||
module.exports = { | ||
'generate:before': function switchOperations(generator) { | ||
const asyncapi = generator.asyncapi; | ||
@@ -139,3 +150,3 @@ for (let [key, value] of Object.entries(asyncapi.channels())) { | ||
} | ||
}); | ||
}; | ||
}; | ||
@@ -146,7 +157,7 @@ ``` | ||
```js | ||
module.exports = register => { | ||
register('setFileTemplateName ', (generator, hookArguments) => { | ||
const currentFilename = hookArguments.originalFilename ; | ||
return currentFilename.replace('-', '_') | ||
}); | ||
module.exports = { | ||
'setFileTemplateName': (generator, hookArguments) => { | ||
const currentFilename = hookArguments.originalFilename ; | ||
return currentFilename.replace('-', '_') | ||
}; | ||
}; | ||
@@ -189,3 +200,4 @@ ``` | ||
|`generator`| [String] | A string representing the Generator version-range the template is compatible with. This value must follow the [semver](https://docs.npmjs.com/misc/semver) syntax. E.g., `>=1.0.0`, `>=1.0.0 <=2.0.0`, `~1.0.0`, `^1.0.0`, `1.0.0`, etc. | ||
|`filters`| [String] | A list of modules containing functions that can be uses as Nunjucks filters. In case of external modules, remember they need to be added as a dependency in `package.json` of your template. | ||
|`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. | ||
|`hooks`| Object[String, String] or Object[String, Array[String]] | A list of modules containing hooks, except of the ones you keep locally in your template in default location. For each module you must specify exact name of the hook that should be used in the template. For 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. | ||
@@ -224,3 +236,6 @@ ### Example | ||
"@asyncapi/generator-filters" | ||
] | ||
], | ||
"hooks": { | ||
"@asyncapi/generator-hooks": "hookFunctionName" | ||
} | ||
} | ||
@@ -227,0 +242,0 @@ ``` |
@@ -69,3 +69,3 @@ const path = require('path'); | ||
confFilters.forEach(async el => { | ||
const promises = confFilters.map(async el => { | ||
const modulePath = await isLocalTemplate(templateDir) ? path.resolve(templateDir, DEFAULT_MODULES_DIR, el) : el; | ||
@@ -76,2 +76,4 @@ const mod = require(modulePath); | ||
}); | ||
await Promise.all(promises); | ||
} | ||
@@ -78,0 +80,0 @@ |
const path = require('path'); | ||
const fs = require('fs'); | ||
const xfs = require('fs.extra'); | ||
const walkSync = require('klaw-sync'); | ||
const minimatch = require('minimatch'); | ||
@@ -31,2 +30,3 @@ const parser = require('@asyncapi/parser'); | ||
const { registerFilters } = require('./filtersRegistry'); | ||
const { registerHooks } = require('./hooksRegistry'); | ||
@@ -82,3 +82,3 @@ const ajv = new Ajv({ allErrors: true }); | ||
* @param {String[]} [options.noOverwriteGlobs] List of globs to skip when regenerating the template. | ||
* @param {String[]} [options.disabledHooks] List of hooks to disable. | ||
* @param {String[]} [options.disabledHooks] List of hook types to disable. | ||
* @param {String} [options.output='fs'] Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set. | ||
@@ -114,2 +114,3 @@ * @param {Boolean} [options.forceWrite=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. | ||
this.templateConfig = {}; | ||
this.hooks = {}; | ||
@@ -171,3 +172,3 @@ // Load template configuration | ||
await this.loadTemplateConfig(); | ||
this.registerHooks(); | ||
await registerHooks(this.hooks, this.templateConfig, this.templateDir, HOOKS_DIRNAME); | ||
await registerFilters(this.nunjucks, this.templateConfig, this.templateDir, FILTERS_DIRNAME); | ||
@@ -701,25 +702,2 @@ await this.validateTemplateConfig(asyncapiDocument); | ||
/** | ||
* Loads the template hooks. | ||
* @private | ||
*/ | ||
registerHooks() { | ||
try { | ||
this.hooks = {}; | ||
const hooksPath = path.resolve(this.templateDir, HOOKS_DIRNAME); | ||
if (!fs.existsSync(hooksPath)) return this.hooks; | ||
const files = walkSync(hooksPath, { nodir: true }); | ||
files.forEach(file => { | ||
require(file.path)((when, hook) => { | ||
this.hooks[when] = this.hooks[when] || []; | ||
this.hooks[when].push(hook); | ||
}); | ||
}); | ||
} catch (e) { | ||
e.message = `There was a problem registering the hooks: ${e.message}`; | ||
throw e; | ||
} | ||
} | ||
/** | ||
* Launches all the hooks registered at a given hook point/name. | ||
@@ -726,0 +704,0 @@ * |
{ | ||
"name": "@asyncapi/generator", | ||
"version": "0.45.1", | ||
"version": "0.46.0", | ||
"description": "The AsyncAPI generator. It can generate documentation, code, anything!", | ||
@@ -56,3 +56,2 @@ "main": "./lib/generator.js", | ||
"js-yaml": "^3.13.1", | ||
"klaw-sync": "^6.0.0", | ||
"markdown-it": "^8.4.1", | ||
@@ -59,0 +58,0 @@ "minimatch": "^3.0.4", |
@@ -48,3 +48,3 @@ <h1 align="center">AsyncAPI Generator</h1> | ||
-V, --version output the version number | ||
-d, --disable-hook <hookName> disable a specific hook | ||
-d, --disable-hook <hookType> disable a specific hook type | ||
--debug 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 | ||
@@ -51,0 +51,0 @@ -i, --install installs the template and its dependencies (defaults to false) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
111263
15
30
1219
16
- Removedklaw-sync@^6.0.0
- Removedgraceful-fs@4.2.11(transitive)
- Removedklaw-sync@6.0.0(transitive)