Skip to content

feat: new usage rendering mechanism - #161

Merged
kazupon merged 4 commits into
mainfrom
feat/render-usage-on-plugin
Jun 6, 2025
Merged

kazupon merged 4 commits into
mainfrom
feat/render-usage-on-plugin

Conversation

@kazupon

@kazupon kazupon commented Jun 6, 2025

Copy link
Copy Markdown
Owner

Description

Linked Issues

Additional context

Summary by CodeRabbit

  • New Features

    • Introduced a flexible system for decorating and customizing CLI output, allowing plugins to modify headers, usage messages, and validation error displays.
    • Added new methods for plugins to register global options and decorate output renderers.
    • Added a default renderer plugin that centralizes header, usage, and validation error rendering logic.
  • Bug Fixes

    • Improved pattern matching for test files in configuration to ensure proper file inclusion at all directory levels.
  • Tests

    • Added comprehensive tests for the new decorator system and plugin context enhancements.
    • Provided utilities for creating mock command contexts in tests.
  • Documentation

    • Expanded type definitions and documentation for new decorator-related types.

@coderabbitai

coderabbitai Bot commented Jun 6, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This change introduces a new RendererDecorators class to manage and compose renderer decorators for CLI output in the gunshi package. The CLI and plugin context are refactored to utilize this decorator system, enabling flexible extension of header, usage, and validation error rendering. Supporting types, tests, and utilities are added or updated accordingly. ESLint configuration is also refined for test file matching, and a new default renderer plugin is added to the plugin list.

Changes

File(s) Change Summary
eslint.config.ts Updated ignore patterns for test files in ESLint config to match files at any depth within src directories.
packages/gunshi/src/cli.ts Refactored to use RendererDecorators for rendering headers, usage, and validation errors; updated function signatures to accept decorators.
packages/gunshi/src/decorators.ts Introduced RendererDecorators class for managing and composing renderer decorators for CLI output.
packages/gunshi/src/decorators.test.ts Added comprehensive tests for RendererDecorators, including decorator chaining, error handling, and default fallbacks.
packages/gunshi/src/plugin.ts Modified PluginContext to accept a RendererDecorators instance and added methods for registering decorators and global options.
packages/gunshi/src/plugin.test.ts Updated tests to use RendererDecorators and added tests for renderer decoration functionality in PluginContext.
packages/gunshi/src/types.ts Added new exported type aliases: RendererDecorator and ValidationErrorsDecorator for decorator function signatures.
packages/gunshi/test/utils.ts Added createMockCommandContext utility for generating mock CommandContext objects for testing.
packages/gunshi/src/plugins/index.ts Added new defaultRenderer plugin to the exported plugin list.
packages/gunshi/src/plugins/renderer.ts Added new default renderer plugin registering header, usage, and validation errors decorators that invoke specialized render functions.

Sequence Diagram(s)

sequenceDiagram
    participant CLI
    participant RendererDecorators
    participant PluginContext
    participant Renderer

    CLI->>RendererDecorators: Instantiate
    CLI->>PluginContext: Instantiate with RendererDecorators
    PluginContext->>RendererDecorators: Add renderer decorators (optional)
    CLI->>RendererDecorators: Get header/usage/errors renderer
    RendererDecorators->>Renderer: Compose decorators around base renderer
    CLI->>Renderer: Render output using composed renderer
Loading

Possibly related PRs

  • feat: builtin options with plugin system #155: Introduces the initial plugin system and global options management, related to the evolving plugin infrastructure but does not overlap with renderer decoration or RendererDecorators.

Poem

In the warren of code, where the rabbits convene,
New decorators hop in, their fur glossy and clean.
They wrap up the renderers, layer by layer—
With headers and usage, errors handled with care.
Now tests are all passing, the garden looks bright,
This bunny approves: the CLI feels right!
🐇✨


📜 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 c70a21f and 7d21227.

📒 Files selected for processing (2)
  • packages/gunshi/src/cli.ts (6 hunks)
  • packages/gunshi/src/decorators.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/gunshi/src/decorators.ts
  • packages/gunshi/src/cli.ts
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.

@kazupon kazupon added the feature Includes new features label Jun 6, 2025
@pkg-pr-new

pkg-pr-new Bot commented Jun 6, 2025

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/gunshi@161

commit: 7d21227

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/gunshi/test/utils.ts (1)

141-174: Comprehensive mock implementation with minor type safety concerns.

The mock provides good coverage of the CommandContext interface. However, consider improving the translate function implementation for better type safety.

Consider this more type-safe approach for the translate function:

-    // eslint-disable-next-line unicorn/prefer-native-coercion-functions, @typescript-eslint/no-explicit-any
-    translate: ((key: any) => String(key)) as CommandContext['translate']
+    translate: (key: string, values?: Record<string, unknown>) => key

This removes the need for ESLint disables and provides clearer typing.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f68cd16 and aa445bb.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (8)
  • eslint.config.ts (1 hunks)
  • packages/gunshi/src/cli.ts (6 hunks)
  • packages/gunshi/src/decorators.test.ts (1 hunks)
  • packages/gunshi/src/decorators.ts (1 hunks)
  • packages/gunshi/src/plugin.test.ts (2 hunks)
  • packages/gunshi/src/plugin.ts (3 hunks)
  • packages/gunshi/src/types.ts (1 hunks)
  • packages/gunshi/test/utils.ts (2 hunks)
🔇 Additional comments (22)
eslint.config.ts (1)

36-37: LGTM! Improved glob pattern precision.

The updated patterns correctly match test files at any depth within src directories. The original patterns would miss nested test files due to incorrect glob syntax.

packages/gunshi/test/utils.ts (1)

13-17: LGTM! Proper import expansion.

The addition of CommandContext import supports the new mock function implementation.

packages/gunshi/src/plugin.test.ts (3)

6-11: Well-structured mock setup.

The mocking isolates the default renderers effectively, allowing focused testing of the decoration behavior.


15-40: Consistent integration with new decorator system.

All existing tests are properly updated to use the RendererDecorators constructor parameter, maintaining test functionality while adapting to the new architecture.


43-96: Comprehensive coverage of decorator functionality.

The new test suites effectively verify that each decorator type (header, usage, validation errors) works correctly. The tests properly:

  • Use async/await for decoration testing
  • Verify decorated output contains expected modifications
  • Pass appropriate parameters (including error for validation decorators)
  • Use consistent mock contexts
packages/gunshi/src/decorators.test.ts (4)

1-11: Excellent test setup with proper mocking.

The mock setup isolates the default renderers effectively, enabling focused testing of the decorator chain behavior.


12-42: Thorough verification of default behavior.

These tests properly verify that the system works correctly when no decorators are applied, providing a solid baseline for decorator functionality testing.


44-78: Comprehensive decorator application testing.

The tests effectively verify both single and multiple decorator scenarios, including the correct application order. The assertion on line 77 clearly demonstrates that decorators are applied in the expected sequence.


80-159: Excellent coverage of advanced scenarios.

This section covers critical aspects including:

  • Async decorator handling with proper timing
  • Context parameter passing verification
  • Error parameter handling for validation decorators
  • Exception propagation from decorators
  • Edge case testing with empty decorator chains

The test implementations are robust and provide confidence in the decorator system's reliability.

packages/gunshi/src/plugin.ts (3)

34-36: LGTM! Constructor properly initializes decorator dependency.

The constructor correctly accepts and stores the RendererDecorators instance, establishing proper dependency injection for the new rendering system.


51-59: Good validation logic for global options.

The validation correctly prevents empty option names and duplicate registrations, which helps maintain data integrity.


65-83: Clean delegation pattern for decorator methods.

The three decorator methods properly delegate to the internal RendererDecorators instance, maintaining good separation of concerns and encapsulation.

packages/gunshi/src/types.ts (2)

484-494: Well-designed generic decorator type.

The RendererDecorator<T> type correctly captures the decorator pattern with proper typing for the base renderer function and return type. The generic parameter allows reuse across different renderer types.


496-508: Appropriate specialized type for validation error decorators.

The ValidationErrorsDecorator type correctly extends the decorator pattern to include the AggregateError parameter, maintaining type safety for validation error rendering.

packages/gunshi/src/cli.ts (4)

38-40: Clean integration of decorator system.

The RendererDecorators instance is properly created and passed to the PluginContext, establishing the foundation for the new rendering mechanism.


156-169: Proper renderer resolution with fallback logic.

The function correctly prioritizes environment overrides while falling back to decorated renderers from the decorators instance, maintaining backward compatibility.


175-189: Consistent header rendering pattern.

The header rendering follows the same pattern as usage rendering, ensuring consistency in the decorator system integration.


191-201: Validation error rendering maintains same pattern.

The validation error rendering correctly follows the established pattern of environment override priority with decorator fallback.

packages/gunshi/src/decorators.ts (4)

14-29: Well-encapsulated decorator storage.

The private arrays and public add methods provide clean encapsulation for managing decorators of different types. The naming is clear and consistent.


31-37: Clean delegation to shared builder method.

The getter methods properly delegate to the shared #buildRenderer method, promoting code reuse and consistency.


39-50: Correct composition logic for validation error decorators.

The inline composition correctly handles the different signature of validation error decorators, maintaining the proper decorator chain where each decorator wraps the previous renderer.


52-66: Solid decorator composition implementation.

The #buildRenderer method correctly composes decorators in the right order, where each decorator receives the previous renderer and context. The early return for empty decorators is an efficient optimization.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 6, 2025

Copy link
Copy Markdown

Deploying gunshi with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7d21227
Status: ✅  Deploy successful!
Preview URL: https://60025a3b.gunshi.pages.dev
Branch Preview URL: https://feat-render-usage-on-plugin.gunshi.pages.dev

View logs

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

Labels

feature Includes new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant