Skip to content

refactor(cli): co-locate command registration in each command folder#309

Open
rafa-thayto wants to merge 4 commits into
mainfrom
modular-cli-imports
Open

refactor(cli): co-locate command registration in each command folder#309
rafa-thayto wants to merge 4 commits into
mainfrom
modular-cli-imports

Conversation

@rafa-thayto

Copy link
Copy Markdown
Contributor

What

Splits the command-registration wiring out of the monolithic packages/cli-core/src/cli-program.ts (1109 lines) into per-command registerX(program) functions co-located in each command's folder. createProgram() is now a short, ordered list of registerX(program) calls.

packages/cli-core/src/cli-program.ts | 860 +--------------------

This makes each command's surface (name, flags, examples, action) owned by its own folder — you add or change a command by editing commands/<name>/index.ts, not by editing one giant file. It follows the pattern already established by registerExtras(program).

How it works

  • Each command folder exposes export function registerX(program: Command): void holding its own .command()/.option()/.setExamples()/.action() chain (mirrors the existing registerExtras precedent).
  • New commands/toggles/ owns the shared enable/disable parents, wiring the orgs + billing handlers under both (neither feature folder cleanly owns those parents).
  • Shared option parsers (parseIntegerOption, collectOptionValues) extracted to lib/option-parsers.ts; the users-only USER_LIST_ORDER_BY_CHOICES moved into the users folder.
  • Removed the now-dead apps/users aggregate exports that only existed to feed cli-program.ts.

Behavior

No user-facing change. Registration order is preserved exactly, so clerk --help and every subcommand's help is unchanged. This was verified by capturing the full --help tree (39 command nodes) from main before the change and diffing it against the result after — byte-identical.

Test plan

  • bun run format — clean
  • bun run typecheck — clean
  • bun run lint — 0 warnings, 0 errors
  • bun run build:compile — compiles (336 modules)
  • bun run test — no new failures introduced (pre-existing local-env failures unchanged vs main; full verdict from CI)
  • --help tree byte-identical across all 39 nodes (init, auth, link, unlink, whoami, open, apps, users, env, config, enable/disable, api, doctor, switch-env, completion, skill, update, bird)
  • Multi-agent review (migration-fidelity + security/performance/TypeScript/code-quality) — cleared

Empty changeset included (internal refactor, no user-facing impact).

@changeset-bot

changeset-bot Bot commented May 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a7e05c2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented May 25, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@rafa-thayto, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 28 minutes and 55 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3c3e40aa-a6c1-4fa6-a3aa-6868332faab6

📥 Commits

Reviewing files that changed from the base of the PR and between b8056d9 and a7e05c2.

📒 Files selected for processing (18)
  • packages/cli-core/src/cli-program.ts
  • packages/cli-core/src/commands/api/index.ts
  • packages/cli-core/src/commands/apps/index.ts
  • packages/cli-core/src/commands/auth/index.ts
  • packages/cli-core/src/commands/completion/index.ts
  • packages/cli-core/src/commands/config/index.ts
  • packages/cli-core/src/commands/deploy/index.ts
  • packages/cli-core/src/commands/doctor/index.ts
  • packages/cli-core/src/commands/env/index.ts
  • packages/cli-core/src/commands/init/index.ts
  • packages/cli-core/src/commands/link/index.ts
  • packages/cli-core/src/commands/open/index.ts
  • packages/cli-core/src/commands/switch-env/index.ts
  • packages/cli-core/src/commands/toggles/index.ts
  • packages/cli-core/src/commands/unlink/index.ts
  • packages/cli-core/src/commands/update/index.ts
  • packages/cli-core/src/commands/users/index.ts
  • packages/cli-core/src/commands/whoami/index.ts
📝 Walkthrough

Walkthrough

This PR refactors the CLI program architecture by extracting inline command definitions from cli-program.ts into modular register* functions. Each command (init, auth, link, unlink, whoami, open, apps, users, env, config, toggles, api, doctor, completion, deploy, update, etc.) now has its own registration module, and the main program wires them all via a registrants array. The change also introduces shared option-parsing utilities (collectOptionValues, parseIntegerOption) and adds tests for those helpers.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 15.63% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'refactor(cli): co-locate command registration in each command folder' accurately and specifically describes the main change: extracting command registration logic from a monolithic file into distributed command folder locations.
Description check ✅ Passed The description comprehensively explains what changed (extraction of registration logic), how it works (registerX functions in each folder), behavior preservation (byte-identical help output), and test coverage (format, typecheck, lint, build, test, help tree verification).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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.

@rafa-thayto

Copy link
Copy Markdown
Contributor Author

!snapshot

@rafa-thayto rafa-thayto force-pushed the modular-cli-imports branch from 1a4f549 to 999e72a Compare May 28, 2026 20:35
@coderabbitai

coderabbitai Bot commented May 28, 2026

Copy link
Copy Markdown

Actionable comments posted: 0

@rafa-thayto rafa-thayto force-pushed the modular-cli-imports branch from 999e72a to 1a9db1b Compare May 30, 2026 12:17
Comment thread packages/cli-core/src/cli-program.ts Outdated
Comment thread packages/cli-core/src/lib/option-parsers.ts
@rafa-thayto rafa-thayto force-pushed the modular-cli-imports branch from 1a9db1b to 569e165 Compare June 2, 2026 12:22
@rafa-thayto rafa-thayto requested a review from wyattjoh June 2, 2026 12:22
@rafa-thayto rafa-thayto force-pushed the modular-cli-imports branch 2 times, most recently from 59dee03 to ab4de1f Compare June 12, 2026 12:13

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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)
packages/cli-core/src/commands/whoami/index.ts (1)

68-78: ⚡ Quick win

Add a focused test for registerWhoami command wiring.

This refactor moves registration behavior, so please add/adjust tests that assert whoami is registered with --json and that the action forwards options.json into whoami({ json }).

As per coding guidelines: "**/*: If there are no tests added or modified as part of the PR, please suggest that tests be added to cover the changes."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/cli-core/src/commands/whoami/index.ts` around lines 68 - 78, Add a
unit test for registerWhoami that verifies the command wiring: instantiate a
Command mock/object, call registerWhoami, assert that a "whoami" subcommand
exists with the --json option and examples, and spy/mock the whoami function to
assert that the command's action calls whoami({ json: options.json }) (i.e.,
options.json is forwarded). Target the registerWhoami export and the whoami
function when creating the test; if no tests are added/modified in the PR, add
one per the guideline to cover this behavior.

Source: Coding guidelines

packages/cli-core/src/commands/env/index.ts (1)

4-28: ⚡ Quick win

Add command-registration tests for env pull wiring.

Please add coverage for registerEnv() (command path, options, examples, and .action(pull) binding) so this refactor stays regression-safe.

As per coding guidelines: “If there are no tests added or modified as part of the PR, please suggest that tests be added to cover the changes.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/cli-core/src/commands/env/index.ts` around lines 4 - 28, Add unit
tests that cover registerEnv to prevent regressions: verify the "env" command
and its "pull" subcommand are registered with the correct command path and
examples, assert the options (--app, --instance, --file) are present on the
"pull" command, and confirm the .action handler is bound to the pull function
(reference registerEnv and pull). Use the Command test harness or a mocked
Command to inspect registered commands/examples/options and assert examples
match the array provided in registerEnv so any refactor will fail fast.

Source: Coding guidelines

packages/cli-core/src/commands/deploy/index.ts (1)

638-648: ⚡ Quick win

Add registration-focused tests for the new deploy command wiring.

Line 638 introduces new command registration entrypoints (deploy run default + deploy status --wait) but no test changes are included in this review context. Please add/adjust CLI registration tests to assert subcommand visibility/default behavior and --wait option wiring.

As per coding guidelines: "**/*: If there are no tests added or modified as part of the PR, please suggest that tests be added to cover the changes."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/cli-core/src/commands/deploy/index.ts` around lines 638 - 648, The
new registerDeploy function adds a deploy command with a hidden default
subcommand ("deploy run") wired to the deploy handler and a visible "deploy
status" with a "--wait" option wired to deployStatus, but there are no tests
verifying this wiring; add unit tests that import registerDeploy and assert (1)
the top-level "deploy" command exists, (2) "deploy run" is registered as the
default/hidden subcommand and triggers the deploy handler, and (3) "deploy
status" is registered and exposes the "--wait" option which, when parsed,
results in the correct option value being passed to deployStatus; reference
registerDeploy, deployCmd, deploy, and deployStatus when locating where to hook
the tests.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/cli-core/src/commands/env/index.ts`:
- Around line 17-20: Update the command description to reflect actual behavior:
it auto-detects the target env file unless --file is provided rather than always
writing to .env.local. Change the .description(...) text in
packages/cli-core/src/commands/env/index.ts (the description string used for the
pull command) to mention auto-detection and the --file override, and ensure the
wording aligns with the existing pull implementation that selects a target file
when --file is not passed.

---

Nitpick comments:
In `@packages/cli-core/src/commands/deploy/index.ts`:
- Around line 638-648: The new registerDeploy function adds a deploy command
with a hidden default subcommand ("deploy run") wired to the deploy handler and
a visible "deploy status" with a "--wait" option wired to deployStatus, but
there are no tests verifying this wiring; add unit tests that import
registerDeploy and assert (1) the top-level "deploy" command exists, (2) "deploy
run" is registered as the default/hidden subcommand and triggers the deploy
handler, and (3) "deploy status" is registered and exposes the "--wait" option
which, when parsed, results in the correct option value being passed to
deployStatus; reference registerDeploy, deployCmd, deploy, and deployStatus when
locating where to hook the tests.

In `@packages/cli-core/src/commands/env/index.ts`:
- Around line 4-28: Add unit tests that cover registerEnv to prevent
regressions: verify the "env" command and its "pull" subcommand are registered
with the correct command path and examples, assert the options (--app,
--instance, --file) are present on the "pull" command, and confirm the .action
handler is bound to the pull function (reference registerEnv and pull). Use the
Command test harness or a mocked Command to inspect registered
commands/examples/options and assert examples match the array provided in
registerEnv so any refactor will fail fast.

In `@packages/cli-core/src/commands/whoami/index.ts`:
- Around line 68-78: Add a unit test for registerWhoami that verifies the
command wiring: instantiate a Command mock/object, call registerWhoami, assert
that a "whoami" subcommand exists with the --json option and examples, and
spy/mock the whoami function to assert that the command's action calls whoami({
json: options.json }) (i.e., options.json is forwarded). Target the
registerWhoami export and the whoami function when creating the test; if no
tests are added/modified in the PR, add one per the guideline to cover this
behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a16520dd-50da-49a2-a2f4-5b39e514b9d3

📥 Commits

Reviewing files that changed from the base of the PR and between 999e72a and ab4de1f.

📒 Files selected for processing (22)
  • .changeset/fancy-frogs-open.md
  • packages/cli-core/src/cli-program.ts
  • packages/cli-core/src/commands/api/index.ts
  • packages/cli-core/src/commands/apps/index.ts
  • packages/cli-core/src/commands/auth/index.ts
  • packages/cli-core/src/commands/completion/index.ts
  • packages/cli-core/src/commands/config/index.ts
  • packages/cli-core/src/commands/deploy/index.ts
  • packages/cli-core/src/commands/doctor/index.ts
  • packages/cli-core/src/commands/env/index.ts
  • packages/cli-core/src/commands/init/index.ts
  • packages/cli-core/src/commands/link/index.ts
  • packages/cli-core/src/commands/open/index.ts
  • packages/cli-core/src/commands/switch-env/index.ts
  • packages/cli-core/src/commands/toggles/README.md
  • packages/cli-core/src/commands/toggles/index.ts
  • packages/cli-core/src/commands/unlink/index.ts
  • packages/cli-core/src/commands/update/index.ts
  • packages/cli-core/src/commands/users/index.ts
  • packages/cli-core/src/commands/whoami/index.ts
  • packages/cli-core/src/lib/option-parsers.test.ts
  • packages/cli-core/src/lib/option-parsers.ts
✅ Files skipped from review due to trivial changes (2)
  • .changeset/fancy-frogs-open.md
  • packages/cli-core/src/commands/toggles/README.md
🚧 Files skipped from review as they are similar to previous changes (15)
  • packages/cli-core/src/commands/api/index.ts
  • packages/cli-core/src/commands/switch-env/index.ts
  • packages/cli-core/src/lib/option-parsers.ts
  • packages/cli-core/src/commands/link/index.ts
  • packages/cli-core/src/commands/doctor/index.ts
  • packages/cli-core/src/commands/auth/index.ts
  • packages/cli-core/src/commands/completion/index.ts
  • packages/cli-core/src/commands/unlink/index.ts
  • packages/cli-core/src/commands/toggles/index.ts
  • packages/cli-core/src/commands/open/index.ts
  • packages/cli-core/src/commands/update/index.ts
  • packages/cli-core/src/commands/init/index.ts
  • packages/cli-core/src/commands/users/index.ts
  • packages/cli-core/src/commands/config/index.ts
  • packages/cli-core/src/cli-program.ts

Comment thread packages/cli-core/src/commands/env/index.ts Outdated
Comment thread packages/cli-core/src/cli-program.ts
Move Commander wiring out of the monolithic cli-program.ts into per-command
registerX(program) functions co-located with each command. createProgram()
now just calls them in registration order, shrinking cli-program.ts from
1109 to ~320 lines and making each command's surface owned by its own folder.

- Each command folder exposes registerX(program): void (mirrors the existing
  registerExtras precedent)
- New commands/toggles/ owns the shared enable/disable parents that wire the
  orgs + billing handlers (neither feature folder cleanly owns them)
- Shared option parsers (parseIntegerOption, collectOptionValues) extracted to
  lib/option-parsers.ts
- clerk --help output verified byte-identical across all 39 command nodes;
  no user-facing change
…add option-parsers tests

Address PR review comments from wyattjoh:
- Refactor command registration into a static `registrants` array iterated with a
  single loop, providing a common/standard registration pattern (comment on cli-program.ts)
- Add unit tests for `option-parsers.ts` covering `collectOptionValues` and
  `parseIntegerOption` (comment on option-parsers.ts)
- Also wire in `registerDeploy` for the deploy command added in main and
  remove the now-deleted `registerSkill` (cleanup after rebase)
@rafa-thayto rafa-thayto force-pushed the modular-cli-imports branch from ab4de1f to b8056d9 Compare June 12, 2026 18:00
@rafa-thayto rafa-thayto requested a review from wyattjoh June 12, 2026 18:10
Comment thread packages/cli-core/src/cli-program.ts
@rafa-thayto rafa-thayto requested a review from wyattjoh June 12, 2026 18:34
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.

2 participants