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: add the .ageneratorrc file for setting the generator Config. #1485

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ItshMoh
Copy link
Contributor

@ItshMoh ItshMoh commented Apr 9, 2025

Description
In this PR i have added the feature for .ageneratorrc file which can be defined in the template directory and here we can write the generator config. By this file we can replace the generator key from the package.json. I have changed the loadTemplateConfig function to allow the .ageneratorrc file. It is just optional to add .ageneratorrc file you can also do the config settings in package.json also.
Related issue(s)
#1449

Summary by CodeRabbit

  • New Features

    • Introduced new configuration files for WebSocket client templates, allowing flexible setup and parameterization.
  • Bug Fixes

    • Enhanced error messages to clarify configuration sources and improved the configuration loading process.
  • Chores

    • Updated in-code documentation for clarity regarding filter registration.
    • Added a new dependency for YAML parsing.
    • Removed legacy configuration sections from client setup files.

Copy link

changeset-bot bot commented Apr 9, 2025

⚠️ No Changeset found

Latest commit: e13b9b1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

coderabbitai bot commented Apr 9, 2025

Walkthrough

The pull request updates documentation and configuration handling in the generator module. In filtersRegistry.js, a comment is revised to clarify that Nunjucks filters need not be listed in either package.json or .ageneratorrc. In generator.js, the error message now references .ageneratorrc, and the loadTemplateConfig method is restructured to prioritize .ageneratorrc over package.json, with enhanced error logging. Additionally, a new dependency (js-yaml) is added to the main package.json, and new .ageneratorrc files are provided for JavaScript and Python WebSocket client templates while removing the embedded generator configuration sections from their respective package.json files.

Changes

File(s) Change Summary
apps/generator/lib/filtersRegistry.js, apps/generator/lib/generator.js Updated comments in filtersRegistry.js and refined error handling and configuration loading in generator.js to prioritize .ageneratorrc over package.json.
package.json Added new dependency "js-yaml": "^4.1.0" in the dependencies section.
packages/templates/clients/websocket/{javascript,python}/.ageneratorrc,
packages/templates/clients/websocket/{javascript,python}/package.json
Introduced new .ageneratorrc config files for both JavaScript and Python templates and removed the old "generator" sections from the corresponding package.json files.

Sequence Diagram(s)

sequenceDiagram
    participant G as Generator
    participant FS as File System
    participant YP as YAML Parser
    G->>FS: Check if .ageneratorrc exists
    alt .ageneratorrc found
        FS-->>G: Return .ageneratorrc content
        G->>YP: Parse YAML content
        YP-->>G: Parsed configuration
        G-->>G: Set templateConfig
    else .ageneratorrc missing
        G->>FS: Check for package.json
        FS-->>G: Return package.json content
        G-->>G: Extract "generator" config or use {}
    end
    Note right of G: Log error if any exception occurs
Loading

Suggested labels

ready-to-merge

Suggested reviewers

  • magicmatatjahu
  • jonaslagoni
  • asyncapi-bot-eve
  • derberg

Poem

In the code garden, I hop with delight,
Watching comments and configs take flight.
.ageneratorrc now leads the way,
While errors are logged without dismay.
With each merge, I twirl in pure delight!
🐇🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 37eeb86 and 9dde88d.

📒 Files selected for processing (1)
  • apps/generator/test/generator.test.js (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Test generator as dependency with Node 18
  • GitHub Check: Test generator as dependency with Node 20
  • GitHub Check: Test NodeJS PR - windows-latest
🔇 Additional comments (1)
apps/generator/test/generator.test.js (1)

54-54: Updated error message correctly references the new .ageneratorrc file.

The error message now references .ageneratorrc instead of package.json, which aligns with the new feature that allows users to define generator configurations in a dedicated .ageneratorrc file within the template directory. This change properly reflects the updated behavior of the Generator class to prioritize configurations from the .ageneratorrc file.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
apps/generator/lib/generator.js (1)

971-992: Good implementation of the new .ageneratorrc configuration.

The method now properly prioritizes .ageneratorrc over package.json, with appropriate fallback behavior and error handling. This change effectively implements the feature to allow users to define configurations outside of package.json.

A few suggestions for improvement:

  1. Consider adding a constant for the .ageneratorrc filename at the top of the file with the other constants (similar to CONFIG_FILENAME)
  2. Consider using the existing logging system (log object) instead of console.error for consistency with the rest of the codebase
- console.error('Error loading template config:', e);
+ log.error('Error loading template config:', e);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eacd057 and 37eeb86.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (7)
  • apps/generator/lib/filtersRegistry.js (1 hunks)
  • apps/generator/lib/generator.js (2 hunks)
  • package.json (1 hunks)
  • packages/templates/clients/websocket/javascript/.ageneratorrc (1 hunks)
  • packages/templates/clients/websocket/javascript/package.json (0 hunks)
  • packages/templates/clients/websocket/python/.ageneratorrc (1 hunks)
  • packages/templates/clients/websocket/python/package.json (0 hunks)
💤 Files with no reviewable changes (2)
  • packages/templates/clients/websocket/javascript/package.json
  • packages/templates/clients/websocket/python/package.json
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Test generator as dependency with Node 20
  • GitHub Check: Test generator as dependency with Node 18
🔇 Additional comments (6)
package.json (1)

47-49: Addition of js-yaml dependency looks good.

The added dependency js-yaml (version ^4.1.0) is necessary to support parsing the newly introduced .ageneratorrc YAML configuration files. This aligns perfectly with the PR objectives to allow users to define generator configurations in a .ageneratorrc file as an alternative to using the package.json file.

apps/generator/lib/filtersRegistry.js (1)

19-19: Documentation update correctly reflects new configuration options.

The comment has been updated to include both package.json and .ageneratorrc as places where Nunjucks filters don't need to be listed. This documentation change ensures clarity for developers working with the system and is consistent with the new feature implementation.

packages/templates/clients/websocket/javascript/.ageneratorrc (1)

1-11: Well-structured configuration file for JavaScript WebSocket client.

This newly added .ageneratorrc file correctly implements the generator configuration for the JavaScript WebSocket client with appropriate renderer, API version, and generator version constraints. The parameters section is well-defined with clear descriptions and proper required/default value handling.

According to the AI summary, this configuration was previously embedded in the package.json file and has now been properly extracted to this dedicated configuration file, which aligns with the PR objectives.

packages/templates/clients/websocket/python/.ageneratorrc (1)

1-11: Well-structured configuration file for Python WebSocket client.

This newly added .ageneratorrc file correctly implements the generator configuration for the Python WebSocket client. The configuration properly sets the renderer to React, API version to v3, and defines appropriate generator version constraints. The parameters section correctly defines the required server parameter and optional clientFileName parameter with an appropriate Python-specific default value (client.py).

According to the AI summary, this configuration was previously embedded in the package.json file and has now been properly extracted to this dedicated configuration file, which aligns with the PR objectives.

apps/generator/lib/generator.js (2)

138-138: Error message updated to reference the new configuration file.

The error message now correctly references .ageneratorrc instead of package.json, aligning with the new configuration approach.


976-978: Verify js-yaml dependency has been added.

The code now depends on the js-yaml library to parse the YAML configuration file. Let's verify this dependency has been properly added to the project.

#!/bin/bash
# Check if js-yaml is listed in package.json dependencies
grep -A 10 '"dependencies"' package.json | grep 'js-yaml'

# Check if js-yaml is actually installed in node_modules
find node_modules -name "js-yaml" -type d | head -1

@ItshMoh
Copy link
Contributor Author

ItshMoh commented Apr 9, 2025

hey @derberg here I have allowed the both .ageneratorrc file and package.json for setting the config. I am thinking of also including it in .changeset . What's your thoughts?
Here any other changes you expect

@derberg
Copy link
Member

derberg commented Apr 9, 2025

/u

Copy link

sonarqubecloud bot commented Apr 9, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
D Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

const yamlConfig = require('js-yaml').load(yaml);
this.templateConfig = yamlConfig || {};
} catch (accessError) {
// File doesn't exist, fallback to package.json
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO, it would be better to explicitly check for the presence of the file and doing conditional code, instead of relying on throwing errors and catching them.

Reasons:
1- You have enclosed multiple operations in the try block, not only the access function. If one of the other operations produce an error such as readFile (unrelated to file not existing) or the require().load(), it could be wrongly interpreted as file not existing. This is also for the second try/catch below.

2- There are multiple nested try/catch, and I believe this reduces readability and makes it harder to maintain. It would be much easier if it was a linear flow with conditionals instead.

Proposal: replace .access with a method that checks for the presence of file and returns a boolean.

Copy link
Contributor

@nightknighto nightknighto Apr 10, 2025

Choose a reason for hiding this comment

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

I looked up the methods and it seems that the promisified exist method is deprecated.
I guess if we don't want to use existsSync, then this is a good option: https://stackoverflow.com/a/65446343/16761632

Copy link
Contributor

@nightknighto nightknighto Apr 10, 2025

Choose a reason for hiding this comment

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

On another note, while looking through the docs, I found out an explicit note to not use .access to check if the file exists before reading, instead reading directly and handling the error if it doesn't exist: https://nodejs.org/api/fs.html#fsaccesspath-mode-callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants