Skip to content

feat: support filters from external library - #298

Merged
derberg merged 10 commits into
asyncapi:masterfrom
derberg:use-filters-lib
Apr 29, 2020
Merged

feat: support filters from external library#298
derberg merged 10 commits into
asyncapi:masterfrom
derberg:use-filters-lib

Conversation

@derberg

@derberg derberg commented Apr 17, 2020

Copy link
Copy Markdown
Member

Description

  • support filters from an external library. New template config parameter is added, filters that accepts an array of libraries that you can additionally register as filters for your template.
  • generator now reads any function and then registers it as a filter so it is easier to write functions for templates and test them
  • extracted whole logic related to filters registration to a separate file as generator.js is super overwhelming so using the opportunity that I worked on filters I moved them out

How to test:
./cli.js test/docs/streetlights.yml https://github.com/derberg/html-template/#use-filters-lib -o output --force-write

Migration Guide

Your templates will not work with the latest generator. In your template, in filter directory you need to refactor your functions to follow new structure or remove filters that are already extracted to https://github.com/asyncapi/generator-filters

  "filters": [
    "@asyncapi/generator-filters"
  ]

and have this package in package.json dependencies

 "@asyncapi/generator-filters": "^1.0.0"
  • To refactor your existing filters just make sure your filters are normal JS functions and you export them as part of the module. Have a look on one of our official templates to see how new filters look like https://github.com/asyncapi/html-template/blob/master/filters/all.js. You do not have to change the logic, they just need to be refactored as they no longer need Nunjucks environment as input, which also means they are much easier to test now

Related issue(s)
Fixes #212

@derberg
derberg marked this pull request as ready for review April 17, 2020 16:44

@fmvilas fmvilas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some suggestions. Great stuff!

Comment thread docs/authoring.md Outdated
Comment thread docs/authoring.md Outdated
Comment thread lib/filtersRegistry.js Outdated
@derberg

derberg commented Apr 19, 2020

Copy link
Copy Markdown
Member Author

@fmvilas your suggestions are in, thanks! I also removed one not needed function that was left from my development. What about the config? nothing agains? should I move other config values there with this PR or?

@derberg

derberg commented Apr 20, 2020

Copy link
Copy Markdown
Member Author

@fmvilas forget my last question, I'm removing node-config as its purpose if for apps rather than libraries and it doesn't work nice in a scenario when the generator is used as a library, only complicates live. I will bring back global variable FILTERS_DIRNAME and pass it to the filters function like others arguments.

fmvilas
fmvilas previously approved these changes Apr 27, 2020

@fmvilas fmvilas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Minor stuff 👍

Comment thread lib/filtersRegistry.js Outdated
Comment thread lib/filtersRegistry.js Outdated
@sonarqubecloud

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@derberg
derberg merged commit 4d0a951 into asyncapi:master Apr 29, 2020
@derberg
derberg deleted the use-filters-lib branch April 29, 2020 15:06
@asyncapi-bot

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.41.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid code repetition in templates filters

3 participants