Skip to content

feat: support createCommandContext exporting - #334

Merged
kazupon merged 2 commits into
mainfrom
feat/export-create-command-context
Oct 7, 2025
Merged

kazupon merged 2 commits into
mainfrom
feat/export-create-command-context

Conversation

@kazupon

@kazupon kazupon commented Oct 7, 2025

Copy link
Copy Markdown
Owner

Description

Linked Issues

resolve #309

Additional context

Summary by CodeRabbit

  • New Features

    • Exposed createCommandContext and CommandContextParams across core and plugin packages for direct use.
    • Simplified context creation with sensible defaults when fields are omitted.
  • Refactor

    • Standardized createCommandContext usage to a single options object in the completion plugin and removed the redundant local helper.
    • Tests updated to rely on defaults and simpler context inputs.
  • Documentation

    • Enhanced JSDoc for command call modes and logging behavior.

@kazupon kazupon added the feature Includes new features label Oct 7, 2025
@coderabbitai

coderabbitai Bot commented Oct 7, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Made CommandContextParams optional with defaults and re-exported createCommandContext/CommandContextParams across gunshi barrels; updated call sites (tests and plugin-completion) to pass a single options object and rely on defaults; removed a local createCommandContext helper in plugin-completion utils.

Changes

Cohort / File(s) Summary
Core context API
packages/gunshi/src/context.ts
Exported CommandContextParams type; made its fields optional; createCommandContext parameter destructuring now applies defaults (args={}, explicit={}, values={}, positionals=[], rest=[], argv=[], tokens=[], command={}, cliOptions={}, validationError=undefined) and preserves callMode/omitted.
Barrel re-exports
packages/gunshi/src/index.ts, packages/gunshi/src/definition.ts, packages/gunshi/src/plugin.ts
Re-exported createCommandContext and CommandContextParams (type) from context.ts and updated JSDoc/comments.
Plugin-completion integration
packages/plugin-completion/src/index.ts, packages/plugin-completion/src/utils.ts
Switched usage to createCommandContext({ args, command, callMode, extensions }); imported CommandContextExtension from @gunshi/plugin; removed local createCommandContext helper from utils.ts and related imports.
Tests — gunshi & definition
packages/gunshi/src/context.test.ts, packages/gunshi/src/plugin/core.test.ts, packages/definition/src/index.test.ts
Simplified test context inputs to omit now-defaulted fields (rest/tokens/extensions/positionals/argv/etc.); adjusted expectations and replaced some run! non-null assertions with run.
Tests — plugins & renderer & shared
packages/plugin-renderer/src/header.test.ts, packages/plugin-renderer/src/usage.test.ts, packages/plugin-renderer/src/validation.test.ts, packages/shared/src/localization.test.ts
Simplified createCommandContext calls in test scaffolding to pass fewer fields (often only args and/or command); removed non-null assertions and other now-unnecessary explicit properties.
Docs / types JSDoc
packages/gunshi/src/types.ts
JSDoc clarifications for CommandCallMode and CommandContext.log behavior; no type signature changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Caller
  participant Ctx as createCommandContext
  participant Cmd as Command (optional)
  participant Ext as Extensions (optional)

  Caller->>Ctx: createCommandContext({ args?, command?, callMode?, extensions?, ... })
  Note right of Ctx #DDEBF7: Defaults applied for missing fields<br/>args={}, explicit={}, values={}, positionals=[], rest=[], argv=[], tokens=[]
  Ctx->>Cmd: attach command reference (if provided)
  Ctx->>Ext: register extensions map (if provided)
  Ctx-->>Caller: return fully-initialized CommandContext
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

I nibble keys beneath moonlit domes,
Defaults tucked in like cozy homes.
One object to pass, no noise, no fray —
Extensions snug, the context paves the way.
Hop, merge, and flourish — carrot cake hooray! 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Linked Issues Check ⚠️ Warning Although this PR resolves issue #309 by exporting createCommandContext, it does not implement the requested test utilities such as createMockCommandContext or createMockPluginContext, nor does it publish a new @gunshi/test-utils package as described in the issue, so it fails to meet the linked issue’s objectives. To satisfy issue #309, add the specified mock utilities (e.g. createMockCommandContext, createMockPluginContext) and package them under a new @gunshi/test-utils module.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly describes the main change of exporting the createCommandContext function, matching the primary modification in the pull request and conveying the intent clearly and concisely.
Out of Scope Changes Check ✅ Passed All code and test updates strictly support the centralization and export of createCommandContext and related types, and there are no modifications unrelated to exporting or adapting this function.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/export-create-command-context

📜 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 6f1faef and 5805c80.

📒 Files selected for processing (1)
  • packages/gunshi/src/context.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/gunshi/src/context.ts

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Oct 7, 2025

Copy link
Copy Markdown

Open in StackBlitz

@gunshi/bone

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

@gunshi/definition

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

gunshi

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

@gunshi/plugin

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

@gunshi/plugin-completion

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

@gunshi/plugin-dryrun

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

@gunshi/plugin-global

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

@gunshi/plugin-i18n

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

@gunshi/plugin-renderer

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

@gunshi/resources

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

@gunshi/shared

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

commit: 5805c80

@kazupon
kazupon marked this pull request as ready for review October 7, 2025 15:26

@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 (2)
packages/plugin-completion/src/index.ts (1)

188-200: Align factory signature and consider narrowing extensions typing.

  • Match factory’s (ctx, cmd) params to satisfy strict function types.
  • Optionally, narrow extensions to the concrete key/type to improve createCommandContext inference.

Apply this minimal change:

-  const extensions: Record<string, CommandContextExtension> = Object.create(null)
+  const extensions: Record<string, CommandContextExtension> = Object.create(null)
   if (i18n) {
     extensions[i18nPluginId] = {
       key: Symbol(i18nPluginId),
-      factory: () => i18n
+      factory: (_ctx, _cmd) => i18n
     }
   }

Optional, for tighter types (if desired):

const extensions = Object.create(null) as {
  [K in typeof i18nPluginId]?: CommandContextExtension<I18nExtension>
}

As per coding guidelines

packages/gunshi/src/context.ts (1)

54-59: Keep generics consistent with the function signature.

Align the interface bounds/defaults with createCommandContext for consistency and easier maintenance.

-export interface CommandContextParams<
-  G extends GunshiParams | { args: Args } | { extensions: ExtendContext },
-  V extends ArgValues<ExtractArgs<G>>,
-  C extends Command<G> | LazyCommand<G> = Command<G>,
-  E extends Record<string, CommandContextExtension> = Record<string, CommandContextExtension>
-> {
+export interface CommandContextParams<
+  G extends GunshiParamsConstraint = DefaultGunshiParams,
+  V extends ArgValues<ExtractArgs<G>> = ArgValues<ExtractArgs<G>>,
+  C extends Command<G> | LazyCommand<G> = Command<G>,
+  E extends Record<string, CommandContextExtension> = {}
+> {

As per coding guidelines

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 82391dd and 6f1faef.

📒 Files selected for processing (14)
  • packages/definition/src/index.test.ts (0 hunks)
  • packages/gunshi/src/context.test.ts (3 hunks)
  • packages/gunshi/src/context.ts (2 hunks)
  • packages/gunshi/src/definition.ts (2 hunks)
  • packages/gunshi/src/index.ts (2 hunks)
  • packages/gunshi/src/plugin.ts (1 hunks)
  • packages/gunshi/src/plugin/core.test.ts (3 hunks)
  • packages/gunshi/src/types.ts (2 hunks)
  • packages/plugin-completion/src/index.ts (3 hunks)
  • packages/plugin-completion/src/utils.ts (0 hunks)
  • packages/plugin-renderer/src/header.test.ts (0 hunks)
  • packages/plugin-renderer/src/usage.test.ts (9 hunks)
  • packages/plugin-renderer/src/validation.test.ts (0 hunks)
  • packages/shared/src/localization.test.ts (1 hunks)
💤 Files with no reviewable changes (4)
  • packages/plugin-renderer/src/header.test.ts
  • packages/plugin-completion/src/utils.ts
  • packages/definition/src/index.test.ts
  • packages/plugin-renderer/src/validation.test.ts
🧰 Additional context used
📓 Path-based instructions (2)
packages/gunshi/src/**/*.ts

📄 CodeRabbit inference engine (CLAUDE.md)

packages/gunshi/src/**/*.ts: All source code is in TypeScript with strict mode enabled
Type safety is a core feature - maintain strict TypeScript types throughout

Files:

  • packages/gunshi/src/plugin.ts
  • packages/gunshi/src/index.ts
  • packages/gunshi/src/definition.ts
  • packages/gunshi/src/context.ts
  • packages/gunshi/src/context.test.ts
  • packages/gunshi/src/plugin/core.test.ts
  • packages/gunshi/src/types.ts
**/*.ts

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.ts: Use ES modules throughout the codebase
Follow existing code style (enforced by ESLint and Prettier)

Files:

  • packages/gunshi/src/plugin.ts
  • packages/plugin-renderer/src/usage.test.ts
  • packages/gunshi/src/index.ts
  • packages/gunshi/src/definition.ts
  • packages/shared/src/localization.test.ts
  • packages/gunshi/src/context.ts
  • packages/gunshi/src/context.test.ts
  • packages/gunshi/src/plugin/core.test.ts
  • packages/gunshi/src/types.ts
  • packages/plugin-completion/src/index.ts
🧠 Learnings (1)
📚 Learning: 2025-07-21T07:12:47.997Z
Learnt from: CR
PR: kazupon/gunshi#0
File: CLAUDE.md:0-0
Timestamp: 2025-07-21T07:12:47.997Z
Learning: Applies to packages/gunshi/test/**/*.test.ts : Add tests for new features in the corresponding test file

Applied to files:

  • packages/gunshi/src/plugin/core.test.ts
🧬 Code graph analysis (2)
packages/gunshi/src/context.ts (1)
packages/gunshi/src/types.ts (10)
  • ExtractArgs (93-101)
  • ExtractArgExplicitlyProvided (110-110)
  • ArgToken (9-9)
  • CommandCallMode (352-352)
  • CliOptions (250-344)
  • GunshiParamsConstraint (76-84)
  • DefaultGunshiParams (67-67)
  • Command (528-579)
  • LazyCommand (589-603)
  • CommandContextExtension (471-486)
packages/plugin-completion/src/index.ts (2)
packages/gunshi/src/types.ts (2)
  • CommandContextExtension (471-486)
  • Args (9-9)
packages/gunshi/src/context.ts (1)
  • createCommandContext (122-230)
🔇 Additional comments (15)
packages/gunshi/src/plugin.ts (1)

27-27: LGTM! Export additions align with PR objectives.

The addition of createCommandContext and CommandContextParams exports to the plugin barrel makes these utilities accessible for plugin authors and test scenarios, supporting the PR's goal of providing test utilities.

Also applies to: 30-30

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

348-350: LGTM! Improved documentation clarity.

The JSDoc enhancement clearly describes the two command execution modes (entry and subCommand), improving developer understanding.


432-433: LGTM! Clarifies conditional logging behavior.

The documentation now explicitly states that the message is not output when CommandEnvironment.usageSilent is true, which is helpful for understanding the function's behavior.

packages/plugin-renderer/src/usage.test.ts (1)

154-167: LGTM! Simplified test setup relies on defaults.

The test cases now use simplified createCommandContext calls, omitting fields like explicit, values, positionals, rest, argv, tokens, and omitted where they aren't critical to the test. This aligns with the new optional CommandContextParams with defaults.

Note: Line 257-276 retains all fields, which is appropriate for that specific test case.

Also applies to: 181-193, 221-234, 308-321, 355-368, 403-419, 425-439, 479-492, 529-542, 548-561

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

7-11: LGTM! Documentation reflects new exports.

The module-level documentation now includes createCommandContext and clarifies that it's mainly for testing purposes, which is helpful for users of this entry point.


32-32: LGTM! Consistent public API exports.

The addition of createCommandContext and CommandContextParams to the main barrel export is consistent with the other barrel files and supports the PR's objective of providing test utilities.

Also applies to: 36-36

packages/shared/src/localization.test.ts (1)

35-39: LGTM! Test setup simplified appropriately.

The test now constructs the command context with only the essential fields (args, callMode, command), relying on defaults for other properties. This is cleaner and aligns with the updated API.

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

138-143: LGTM! Simplified test relies on defaults.

The test now omits rest, tokens, and extensions fields, demonstrating that createCommandContext correctly provides defaults for these optional parameters.


281-284: LGTM! Appropriate field selection for test.

The test keeps extensions because the test validates multiple plugin extensions, while correctly omitting rest and tokens to rely on defaults.


327-334: LGTM! Minimal context for execution order test.

The test constructs context with only the fields needed to verify extension factory execution order, appropriately relying on defaults for other properties.

packages/gunshi/src/definition.ts (1)

52-52: LGTM! Consistent barrel exports.

The definition barrel now exports createCommandContext and CommandContextParams, maintaining consistency with the other entry points (index.ts, plugin.ts) and supporting the PR's test utilities objective.

Also applies to: 67-67

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

440-444: LGTM! Streamlined test with essential fields.

The test now constructs context with only values, command, and extensions, relying on defaults for other properties. The removal of the non-null assertion (run!run) is safer and cleaner.

Also applies to: 446-446


464-472: LGTM! Appropriate field selection for regular command test.

The test keeps args, explicit, and values fields that are relevant for testing the regular command behavior, while correctly omitting other fields. The non-null assertion removal improves safety.

Also applies to: 474-474


509-514: LGTM! Minimal context for extension access test.

The test constructs context with only command and extensions, appropriately demonstrating that the extension can access context properties with minimal setup. Non-null assertion removal is a good practice.

Also applies to: 515-515

packages/plugin-completion/src/index.ts (1)

60-60: Good move to consume public createCommandContext.

Reduces duplication and aligns plugin with exported API surface.

Comment thread packages/gunshi/src/context.ts
@kazupon
kazupon merged commit acd171a into main Oct 7, 2025
9 checks passed
@kazupon
kazupon deleted the feat/export-create-command-context branch October 7, 2025 15:40
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.

support gunshi test utils

1 participant