Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support filters from external library #298

Merged
merged 10 commits into from
Apr 29, 2020
Merged

feat: support filters from external library #298

merged 10 commits into from
Apr 29, 2020

Conversation

derberg
Copy link
Member

@derberg derberg commented Apr 17, 2020

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
Copy link
Member

@fmvilas fmvilas left a comment

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!

docs/authoring.md Outdated Show resolved Hide resolved
docs/authoring.md Outdated Show resolved Hide resolved
lib/filtersRegistry.js Outdated Show resolved Hide resolved
@derberg
Copy link
Member Author

derberg commented Apr 19, 2020

@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
Copy link
Member Author

derberg commented Apr 20, 2020

@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
Copy link
Member

@fmvilas fmvilas left a comment

Choose a reason for hiding this comment

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

Looks great! Minor stuff 👍

lib/filtersRegistry.js Outdated Show resolved Hide resolved
lib/filtersRegistry.js Outdated Show resolved Hide resolved
@sonarqubecloud
Copy link

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
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