Skip to content

Commit

Permalink
feat: add support for template-generator versioning requirements (#309)
Browse files Browse the repository at this point in the history
* feat: add support for template-generator versioning requirements

* Add generator param to Authoring docs
  • Loading branch information
fmvilas authored Apr 28, 2020
1 parent b94f024 commit 8e09602
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
4 changes: 3 additions & 1 deletion docs/authoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ The `.tp-config.json` file contains a JSON object that may have the following in
|`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: { ... } }`.
|`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.
|`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.
|`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.

### Example

Expand Down Expand Up @@ -188,7 +189,8 @@ The `.tp-config.json` file contains a JSON object that may have the following in
"nonRenderableFiles": [
"src/api/middlewares/*.*",
"lib/lib/config.js"
]
],
"generator": "<2.0.0"
}
```

Expand Down
14 changes: 9 additions & 5 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const Ajv = require('ajv');
const filenamify = require('filenamify');
const git = require('simple-git/promise');
const npmi = require('npmi');
const semver = require('semver');
const packageJson = require('../package.json');
const {
convertMapToObject,
isFileSystemPath,
Expand Down Expand Up @@ -669,9 +671,13 @@ class Generator {
* @return {Promise}
*/
async validateTemplateConfig(asyncapiDocument) {
const { parameters, supportedProtocols, conditionalFiles } = this.templateConfig;
const { parameters, supportedProtocols, conditionalFiles, generator } = this.templateConfig;
let server;

if (typeof generator === 'string' && !semver.satisfies(packageJson.version, generator)) {
throw new Error(`This template is not compatible with the current version of the generator (${packageJson.version}). This template is compatible with the following version range: ${generator}.`);
}

const requiredParams = [];
Object.keys(parameters || {}).forEach(key => {
if (parameters[key].required === true) requiredParams.push(key);
Expand Down Expand Up @@ -700,10 +706,8 @@ class Generator {
if (!server) throw new Error(`Couldn't find server with name ${this.templateParams.server}.`);
}

if (server && Array.isArray(supportedProtocols)) {
if (!supportedProtocols.includes(server.protocol())) {
throw new Error(`Server "${this.templateParams.server}" uses the ${server.protocol()} protocol but this template only supports the following ones: ${supportedProtocols}.`);
}
if (server && Array.isArray(supportedProtocols) && !supportedProtocols.includes(server.protocol())) {
throw new Error(`Server "${this.templateParams.server}" uses the ${server.protocol()} protocol but this template only supports the following ones: ${supportedProtocols}.`);
}
}
}
Expand Down
29 changes: 26 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"minimatch": "^3.0.4",
"npmi": "^4.0.0",
"nunjucks": "^3.2.0",
"semver": "^7.3.2",
"simple-git": "^1.131.0"
},
"devDependencies": {
Expand Down

0 comments on commit 8e09602

Please sign in to comment.