feat: support createCommandContext exporting - #334
Conversation
WalkthroughMade 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
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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. Comment |
@gunshi/bone
@gunshi/definition
gunshi
@gunshi/plugin
@gunshi/plugin-completion
@gunshi/plugin-dryrun
@gunshi/plugin-global
@gunshi/plugin-i18n
@gunshi/plugin-renderer
@gunshi/resources
@gunshi/shared
commit: |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
packages/plugin-completion/src/index.ts (1)
188-200: Alignfactorysignature and consider narrowingextensionstyping.
- Match
factory’s(ctx, cmd)params to satisfy strict function types.- Optionally, narrow
extensionsto the concrete key/type to improvecreateCommandContextinference.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
createCommandContextfor 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
📒 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.tspackages/gunshi/src/index.tspackages/gunshi/src/definition.tspackages/gunshi/src/context.tspackages/gunshi/src/context.test.tspackages/gunshi/src/plugin/core.test.tspackages/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.tspackages/plugin-renderer/src/usage.test.tspackages/gunshi/src/index.tspackages/gunshi/src/definition.tspackages/shared/src/localization.test.tspackages/gunshi/src/context.tspackages/gunshi/src/context.test.tspackages/gunshi/src/plugin/core.test.tspackages/gunshi/src/types.tspackages/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
createCommandContextandCommandContextParamsexports 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 (
entryandsubCommand), improving developer understanding.
432-433: LGTM! Clarifies conditional logging behavior.The documentation now explicitly states that the message is not output when
CommandEnvironment.usageSilentis 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
createCommandContextcalls, omitting fields likeexplicit,values,positionals,rest,argv,tokens, andomittedwhere they aren't critical to the test. This aligns with the new optionalCommandContextParamswith 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
createCommandContextand 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
createCommandContextandCommandContextParamsto 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, andextensionsfields, demonstrating thatcreateCommandContextcorrectly provides defaults for these optional parameters.
281-284: LGTM! Appropriate field selection for test.The test keeps
extensionsbecause the test validates multiple plugin extensions, while correctly omittingrestandtokensto 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
createCommandContextandCommandContextParams, 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, andextensions, 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, andvaluesfields 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
commandandextensions, 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 publiccreateCommandContext.Reduces duplication and aligns plugin with exported API surface.
Description
Linked Issues
resolve #309
Additional context
Summary by CodeRabbit
New Features
Refactor
Documentation