Skip to content

feat!: entry command lazy loading - #118

Merged
kazupon merged 6 commits into
mainfrom
feat/entry-lazy-loading1
May 11, 2025
Merged

feat!: entry command lazy loading#118
kazupon merged 6 commits into
mainfrom
feat/entry-lazy-loading1

Conversation

@kazupon

@kazupon kazupon commented May 10, 2025

Copy link
Copy Markdown
Owner

Description

Linked Issues

Additional context

Summary by CodeRabbit

  • Documentation

    • Clarified documentation for parameter types in command generation and corrected property names in command context descriptions.
  • New Features

    • Added a type guard function to accurately identify lazy commands.
  • Refactor

    • Renamed CommandOptions to CliOptions throughout the codebase and updated related type names and references for consistency.
    • Improved command resolution and execution logic for better modularity and type safety.
    • Updated function and parameter names for clarity and alignment with new types.
  • Tests

    • Updated test cases to use the new cliOptions property name and adjusted type usage accordingly.

@coderabbitai

coderabbitai Bot commented May 10, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This update refactors the CLI framework to consistently use CliOptions instead of CommandOptions, renaming related types and properties throughout the codebase, tests, and documentation. It also expands support for lazy commands, modularizes command resolution and execution, and updates type signatures and helper functions for improved clarity and type safety.

Changes

File(s) Change Summary
docs/guide/advanced/documentation-generation.md, docs/guide/essentials/declarative-configuration.md Updated documentation to clarify parameter types and correct property names (entry and cliOptions).
src/cli.ts, src/types.ts, src/constants.ts, src/generator.ts, src/context.ts Refactored to use CliOptions instead of CommandOptions, updated function and interface signatures, improved command resolution logic, added helpers, and modularized execution flow.
src/utils.ts Added isLazyCommand type guard; updated lazy command resolution logic to use this guard.
src/cli.test.ts, src/context.test.ts, src/renderer.test.ts Updated tests to use CliOptions and cliOptions naming, adjusted type imports, and modified lazy command test definitions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant CommandResolver
    participant CommandExecutor

    User->>CLI: invoke cli(argv, entry, options)
    CLI->>CLI: resolveCliOptions(options, entry)
    CLI->>CommandResolver: resolveCommand(sub, entry, options)
    CommandResolver-->>CLI: { commandName, command, callMode }
    CLI->>CommandExecutor: executeCommand(command, ctx, commandName)
    CommandExecutor->>command: run(ctx)
    CommandExecutor-->>CLI: result
    CLI-->>User: CLI execution complete
Loading

Possibly related PRs

  • kazupon/gunshi#116: Introduced initial support for LazyCommand in the CLI, which is further expanded and refined in this PR.
  • kazupon/gunshi#109: Refactored CLI option and argument typing, similar in scope to this PR's renaming and type changes.

Suggested labels

breaking, feature

Poem

In the warren of code, a change hops through,
With CliOptions shining, fresh and new.
Lazy commands leap with nimble delight,
Type guards and helpers keep logic tight.
The docs are refreshed, the tests all agree—
A rabbit’s proud of this CLI spree! 🐇✨


📜 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 40fb668 and be4b27a.

📒 Files selected for processing (1)
  • src/generator.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/generator.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Cloudflare Pages
✨ 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.

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.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented May 10, 2025

Copy link
Copy Markdown

Deploying gunshi with  Cloudflare Pages  Cloudflare Pages

Latest commit: be4b27a
Status: ✅  Deploy successful!
Preview URL: https://fdcfb0e4.gunshi.pages.dev
Branch Preview URL: https://feat-entry-lazy-loading1.gunshi.pages.dev

View logs

@pkg-pr-new

pkg-pr-new Bot commented May 10, 2025

Copy link
Copy Markdown

Open in StackBlitz

@gunshi/bone

npm i https://pkg.pr.new/@gunshi/bone@118

@gunshi/definition

npm i https://pkg.pr.new/@gunshi/definition@118

gunshi

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

@gunshi/plugin

npm i https://pkg.pr.new/@gunshi/plugin@118

@gunshi/plugin-completion

npm i https://pkg.pr.new/@gunshi/plugin-completion@118

@gunshi/plugin-dryrun

npm i https://pkg.pr.new/@gunshi/plugin-dryrun@118

@gunshi/plugin-global

npm i https://pkg.pr.new/@gunshi/plugin-global@118

@gunshi/plugin-i18n

npm i https://pkg.pr.new/@gunshi/plugin-i18n@118

@gunshi/plugin-renderer

npm i https://pkg.pr.new/@gunshi/plugin-renderer@118

@gunshi/resources

npm i https://pkg.pr.new/@gunshi/resources@118

@gunshi/shared

npm i https://pkg.pr.new/@gunshi/shared@118

commit: 0516c8e

@kazupon kazupon changed the title feat: entry command lazy loading feat!: entry command lazy loading May 10, 2025
@kazupon
kazupon marked this pull request as ready for review May 10, 2025 17:03

@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: 1

🧹 Nitpick comments (3)
src/cli.ts (3)

94-102: getCommandArgs can return an object with a live reference – consider defensive cloning

cmd.args is returned directly when present.
If downstream code mutates the returned object (e.g., ctx.args.foo = false) it will mutate the original definition attached to the command, which might be reused on subsequent invocations and cause subtle bugs in long-living CLI processes.

-    return cmd.args || create<A>()
+    return cmd.args ? { ...cmd.args } as A : create<A>()

Copy-on-read keeps command definitions immutable and thread-safe.


230-236: Mutation of sub-command definition breaks immutability

Inside resolveCommand, the sub-command object is mutated in-place:

cmd.name = sub

If the same cmd instance is reused elsewhere, this implicit state change can leak outside the CLI flow. Prefer returning a shallow-cloned object with the name populated, or set the default lazily when the command is first defined.


248-258: Error message may be blank when command name is omitted

When the entry command has no explicit name, name is passed as an empty string.
executeCommand then throws:

throw new Error(`'run' not found on Command \`${name}\``)

which results in 'run' not found on Command – note the missing identifier.
Consider using a fallback such as '(anonymous)' for better debuggability:

-  throw new Error(`'run' not found on Command \`${name}\``)
+  throw new Error(`'run' not found on Command \`${name || "(anonymous)"}\``)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a0ab25d and 7f66765.

📒 Files selected for processing (11)
  • docs/guide/advanced/documentation-generation.md (1 hunks)
  • docs/guide/essentials/declarative-configuration.md (1 hunks)
  • src/cli.test.ts (4 hunks)
  • src/cli.ts (5 hunks)
  • src/constants.ts (2 hunks)
  • src/context.test.ts (7 hunks)
  • src/context.ts (6 hunks)
  • src/generator.ts (2 hunks)
  • src/renderer.test.ts (13 hunks)
  • src/types.ts (2 hunks)
  • src/utils.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (4)
src/constants.ts (1)
src/types.ts (1)
  • CliOptions (158-222)
src/context.ts (4)
src/types.ts (3)
  • CliOptions (158-222)
  • CommandEnvironment (81-153)
  • Command (322-351)
src/utils.ts (2)
  • create (102-104)
  • log (106-108)
src/constants.ts (2)
  • COMMAND_OPTIONS_DEFAULT (48-63)
  • NOOP (20-20)
src/translation.ts (1)
  • createTranslationAdapter (10-14)
src/utils.ts (1)
src/types.ts (3)
  • LazyCommand (457-466)
  • Commandable (471-471)
  • Command (322-351)
src/cli.test.ts (2)
src/definition.ts (1)
  • lazy (37-49)
src/types.ts (2)
  • Command (322-351)
  • CliOptions (158-222)
🪛 LanguageTool
docs/guide/essentials/declarative-configuration.md

[uncategorized] ~226-~226: Loose punctuation mark.
Context: ...definition object itself. - cliOptions: The resolved CLI options passed to `cli...

(UNLIKELY_OPENING_PUNCTUATION)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (19)
docs/guide/advanced/documentation-generation.md (1)

56-56: Update description to include lazy command functions
The parameter doc for entry now correctly states it can accept lazy command functions, aligning with the new LazyCommand support.

docs/guide/essentials/declarative-configuration.md (1)

226-226: Align property name in documentation
The context property name is updated to cliOptions, matching the refactoring of the CommandOptions interface to CliOptions.

🧰 Tools
🪛 LanguageTool

[uncategorized] ~226-~226: Loose punctuation mark.
Context: ...definition object itself. - cliOptions: The resolved CLI options passed to `cli...

(UNLIKELY_OPENING_PUNCTUATION)

src/constants.ts (1)

7-7: Rename type import and default options constant
The import was changed from CommandOptions to CliOptions, and COMMAND_OPTIONS_DEFAULT now uses CliOptions<Args> for consistency with the renamed interface.

Also applies to: 48-48

src/renderer.test.ts (3)

102-102: Consistent rename in renderHeader tests
All calls to createCommandContext in the renderHeader suite have been updated from commandOptions to cliOptions, matching the core API change.

Also applies to: 125-125, 145-145, 163-163


215-215: Consistent rename in renderUsage tests
The cliOptions property is used uniformly across all renderUsage test cases, ensuring the tests reflect the updated context API.

Also applies to: 243-243, 287-287, 323-323, 371-371, 419-419, 469-469, 493-493


516-516: Consistent rename in renderValidationErrors test
The single test for renderValidationErrors now passes cliOptions instead of the old commandOptions.

src/context.test.ts (1)

66-66: Rename parameter in createCommandContext calls
All instances of the commandOptions parameter in this test suite have been renamed to cliOptions, consistent with the new interface and function signature.

Also applies to: 157-157, 204-204, 265-265, 340-340, 413-413, 469-469

src/types.ts (2)

84-107: Good documentation updates

The JSDoc comments have been correctly updated to reference CliOptions instead of CommandOptions, maintaining consistent API documentation.


155-158: Appropriate interface renaming from CommandOptions to CliOptions

The renaming from CommandOptions to CliOptions provides a clearer description of the interface's purpose. This is a good naming improvement that better reflects that these are options for the CLI function rather than for a specific command.

src/utils.ts (2)

22-24: Good addition of type guard for LazyCommand

Adding a dedicated type guard function isLazyCommand improves type safety and makes the code more maintainable. The check for 'commandName' in cmd && !!cmd.commandName ensures that only properly configured lazy commands are recognized.


32-32: Updated to use the new type guard function

Good refactoring to use the new isLazyCommand type guard instead of a generic function check. This makes the code more precise and type-safe.

src/cli.test.ts (4)

9-9: Updated import to use CliOptions

The import statement has been correctly updated to use CliOptions instead of CommandOptions.


276-283: Improved lazy command implementation

The test now properly uses the lazy helper function to create a lazy command, which is more consistent with the improved lazy command support in the codebase.


284-284: Updated command registration key

The registration key for the lazy command has been correctly updated to use command2.commandName instead of command2.name, reflecting the structure of lazy commands.


563-563: Updated type assertions

The type assertions for the options objects have been correctly updated to use CliOptions instead of CommandOptions.

Also applies to: 612-612

src/context.ts (3)

33-33: Updated import to use CliOptions

The import statement has been correctly updated to use CliOptions instead of CommandOptions.


87-87: Updated parameter type in CommandContextParams

The parameter type in the CommandContextParams interface has been updated to use cliOptions instead of commandOptions, maintaining consistency with the interface rename.


106-107: Comprehensive parameter and variable renaming

All occurrences of commandOptions have been consistently renamed to cliOptions throughout the file. This maintains the API consistency with the interface renaming.

Also applies to: 123-123, 125-125, 128-128, 189-189, 213-213

src/cli.ts (1)

104-106:

Details

❓ Verification inconclusive

Source-order of COMMON_ARGS overrides user options

Object.assign(create<A>(), args, COMMON_ARGS) copies COMMON_ARGS last, so built-in flags (--help, --version …) overwrite any identically-named user-defined positional or option already present in args. If the intention is to let framework defaults augment but never override the command’s declaration, swap the order:

-return Object.assign(create<A>(), args, COMMON_ARGS)
+return Object.assign(create<A>(), COMMON_ARGS, args)

Please double-check the desired precedence rules.


Re-evaluate argument precedence in resolveArguments
The current merge order

Object.assign(create<A>(), args, COMMON_ARGS)

applies COMMON_ARGS last, so built-in flags (e.g. --help, --version) will override identically named user options. If the intent is for framework defaults to only augment—not override—user inputs, swap the order:

-return Object.assign(create<A>(), args, COMMON_ARGS)
+return Object.assign(create<A>(), COMMON_ARGS, args)

Please confirm which precedence rule you’d like to enforce.

  • File: src/cli.ts, Lines 104–106

Comment thread src/generator.ts Outdated
kazupon and others added 2 commits May 11, 2025 10:41
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@kazupon
kazupon merged commit 3359934 into main May 11, 2025
@kazupon
kazupon deleted the feat/entry-lazy-loading1 branch May 11, 2025 01:45
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.

1 participant