Skip to content

feat: extract load commands as plugin extension - #184

Merged
kazupon merged 2 commits into
mainfrom
feat/command-loader-as-plugin
Jun 19, 2025
Merged

kazupon merged 2 commits into
mainfrom
feat/command-loader-as-plugin

Conversation

@kazupon

@kazupon kazupon commented Jun 19, 2025

Copy link
Copy Markdown
Owner

Description

Linked Issues

Additional context

Summary by CodeRabbit

  • New Features

    • Introduced a new loader plugin that dynamically loads and caches commands as an extension in the command context.
  • Refactor

    • Updated command context to handle extensions in a more streamlined way.
    • Changed how commands are loaded, shifting from a direct method to using the loader extension.
    • Made plugin setup functions optional to improve plugin flexibility.
    • Removed setup methods from completion and dry-run plugins for simplification.
  • Bug Fixes

    • Improved test setup to include the loader extension for more accurate subcommand handling.
  • Documentation

    • Updated type definitions to reflect the new command loading mechanism and extension structure.

@coderabbitai

coderabbitai Bot commented Jun 19, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes refactor command loading in the Gunshi project by removing the internal loadCommands method from the command context and introducing it as a plugin extension via a new loader plugin. The context and rendering logic are updated to use this extension, and related type signatures are adjusted accordingly.

Changes

File(s) Change Summary
packages/gunshi/src/context.ts, packages/gunshi/src/types.ts Removed internal loadCommands from command context and its type, refactored extension handling.
packages/gunshi/src/plugins/loader.ts Added new loader plugin providing loadCommands as a context extension.
packages/gunshi/src/plugins/index.ts Registered the new loader plugin in the plugins array.
packages/gunshi/src/renderer/usage.ts Updated rendering logic to use loader extension for command loading, adjusted generics.
packages/gunshi/src/renderer.test.ts Modified test to include loader plugin extension in context for subcommands test.
packages/gunshi/src/plugin.ts Made plugin setup function optional, updated plugin factory accordingly.
packages/gunshi/src/plugins/completion.ts Removed setup method from completion plugin.
packages/gunshi/src/plugins/dryrun.ts Removed setup method from dryrun plugin.

Sequence Diagram(s)

sequenceDiagram
    participant Renderer
    participant Context
    participant LoaderPlugin

    Renderer->>Context: Access extensions.loader.loadCommands()
    Context->>LoaderPlugin: Call loadCommands()
    LoaderPlugin->>Context: Retrieve subCommands from env
    LoaderPlugin-->>Renderer: Return loaded commands
Loading

Possibly related PRs

  • feat: core feature plugnaization #177: Introduces a core plugin system that collects and applies plugin extensions into the command context, which is directly built upon by this PR's refactoring of command loading into a plugin extension.

Poem

A loader hops in, so spry and new,
Commands now plugins, not hidden from view.
Extensions are tidy, the context is neat—
No more old methods beneath our feet!
With a twitch of the nose and a plugin or two,
Gunshi grows smarter—hip hip, bunny-hoo! 🐇✨


📜 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 4b614b2 and c95c83c.

📒 Files selected for processing (4)
  • packages/gunshi/src/plugin.ts (4 hunks)
  • packages/gunshi/src/plugins/completion.ts (1 hunks)
  • packages/gunshi/src/plugins/dryrun.ts (1 hunks)
  • packages/gunshi/src/plugins/loader.ts (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • packages/gunshi/src/plugins/dryrun.ts
  • packages/gunshi/src/plugins/completion.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/gunshi/src/plugins/loader.ts
🔇 Additional comments (4)
packages/gunshi/src/plugin.ts (4)

178-178: LGTM: Making setup optional supports flexible plugin architecture.

This change correctly makes the setup property optional in the PluginOptions interface, allowing plugins to be defined without setup functions when they only provide extensions.


224-233: LGTM: Consistent optional setup across plugin overloads.

The setup property is correctly made optional in both plugin function overloads, maintaining type consistency with the interface change.


241-247: LGTM: Third overload maintains consistency.

The generic plugin overload correctly implements the optional setup parameter, maintaining consistency with the other overloads and the interface definition.


252-256: LGTM: Proper conditional setup execution prevents runtime errors.

The implementation correctly checks for the existence of setup before calling it, preventing potential runtime errors when plugins are defined without setup functions. This defensive programming approach is appropriate for the optional setup functionality.

✨ 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 19, 2025
@pkg-pr-new

pkg-pr-new Bot commented Jun 19, 2025

Copy link
Copy Markdown

Open in StackBlitz

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

commit: c95c83c

@kazupon kazupon changed the title feat: extract load commands as plugin extensions feat: extract load commands as plugin extension Jun 19, 2025
@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

Deploying gunshi with  Cloudflare Pages  Cloudflare Pages

Latest commit: c95c83c
Status: ✅  Deploy successful!
Preview URL: https://9739dbfc.gunshi.pages.dev
Branch Preview URL: https://feat-command-loader-as-plugi.gunshi.pages.dev

View logs

@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/src/plugins/loader.ts (1)

51-53: Address the TODO comment for setup logic

The setup method contains a TODO comment indicating incomplete implementation. Consider what setup logic might be needed for the loader plugin.

Do you want me to help implement the setup logic or create an issue to track this task?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 543af7e and 4b614b2.

📒 Files selected for processing (6)
  • packages/gunshi/src/context.ts (2 hunks)
  • packages/gunshi/src/plugins/index.ts (1 hunks)
  • packages/gunshi/src/plugins/loader.ts (1 hunks)
  • packages/gunshi/src/renderer.test.ts (2 hunks)
  • packages/gunshi/src/renderer/usage.ts (3 hunks)
  • packages/gunshi/src/types.ts (0 hunks)
💤 Files with no reviewable changes (1)
  • packages/gunshi/src/types.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (13)
packages/gunshi/src/renderer.test.ts (2)

3-3: LGTM: Loader plugin import added correctly

The import statement properly brings in the loader plugin needed for the test context.


495-497: ```shell
#!/bin/bash

Search for subCommands usage in all test files across the repo

rg -Hn "subCommands" -g "*.test.ts"

Search for loadCommands usage in all test files across the repo

rg -Hn "loadCommands" -g "*.test.ts"


</details>
<details>
<summary>packages/gunshi/src/plugins/index.ts (2)</summary>

`9-9`: **LGTM: Loader plugin import added**

The import statement correctly adds the loader plugin to the available plugins.

---

`14-14`: **LGTM: Loader plugin properly integrated into plugin array**

The loader plugin is correctly positioned in the plugins array between globals and defaultRenderer, which is appropriate for the command loading functionality.

</details>
<details>
<summary>packages/gunshi/src/plugins/loader.ts (2)</summary>

`16-22`: **LGTM: Well-defined interface for command loading**

The `LoaderCommandContext` interface clearly defines the contract for command loading functionality with proper generic support.

---

`27-43`: **LGTM: Solid implementation with caching**

The extension implementation is well-structured with proper caching to avoid redundant command loading. The use of `Promise.all` for concurrent command resolution is efficient.

</details>
<details>
<summary>packages/gunshi/src/context.ts (3)</summary>

`34-34`: **LGTM: Import cleanup aligns with refactoring**

The removal of `resolveLazyCommand` import is consistent with moving command loading functionality to the loader plugin.

---

`242-252`: **LGTM: Improved extension handling**

The refactored extension handling is more robust, using `defineProperty` to properly manage the extensions object on the core context. This provides better encapsulation and type safety.

---

`253-253`: **LGTM: Simplified context creation**

The simplified approach of directly freezing and returning the core object is cleaner than the previous conditional extension merging.

</details>
<details>
<summary>packages/gunshi/src/renderer/usage.ts (4)</summary>

`16-16`: **LGTM: Import added for loader extension interface**

The import of `LoaderCommandContext` is necessary for the updated generic constraints.

---

`143-145`: **LGTM: Generic constraint ensures loader extension availability**

The updated generic constraint properly requires the presence of the loader extension, ensuring type safety for the command loading functionality.

---

`147-147`: **LGTM: Safe access to loader extension with fallback**

The use of optional chaining with fallback to empty array provides robust error handling in case the loader extension is not available.

---

`213-217`: **LGTM: Consistent pattern for loader extension access**

The `hasCommands` function follows the same pattern as `renderCommandsSection`, properly constraining generics and safely accessing the loader extension.

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

@kazupon
kazupon merged commit f30a6ff into main Jun 19, 2025
@kazupon
kazupon deleted the feat/command-loader-as-plugin branch June 19, 2025 04:23
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