Skip to content

refactor(cli): extract withClient helper to remove command boilerplate#181

Merged
pchuri merged 1 commit into
mainfrom
refactor/cli-with-client-helper
May 10, 2026
Merged

refactor(cli): extract withClient helper to remove command boilerplate#181
pchuri merged 1 commit into
mainfrom
refactor/cli-with-client-helper

Conversation

@pchuri
Copy link
Copy Markdown
Owner

@pchuri pchuri commented May 10, 2026

Summary

  • Extract withClient(name, handler, { writable }) helper that wraps the standard Analytics + getConfig + assertWritable + ConfluenceClient + try/handleCommandError scaffolding.
  • Migrate 26 commands (read, info, search, spaces, create, create-child, update, move, delete, versions, version-delete, versions-purge, edit, find, attachments, attachment-upload, attachment-delete, property-{list,get,set,delete}, comments, comment-delete, export, copy-tree, children) to the helper.
  • bin/confluence.js: 940 insertions, 1128 deletions (net −188 lines, 2249 → 2061).

Why

Every client-using command had the same ~6-line pre/post-amble. Extracting it shortens the file, makes each command's intent obvious, and centralizes the read-only profile guard so future commands can opt in via { writable: true } instead of re-implementing the gate.

Behavior preservation

  • Handlers still call analytics.track(name, true) themselves, so conditional / cancel / dry-run tracking keys (*_cancel, copy_tree_dry_run, versions_purge with result.failed === 0) are unchanged.
  • All user-facing strings (console.log, thrown error messages) are identical — diff only differs by indentation.
  • comment <pageId> is intentionally not migrated: it has a custom error path that prints inline-comment-specific hints based on the API error keys.
  • init, stats, install-skill, profile add/remove, convert are not migrated either: they don't construct a ConfluenceClient.

Test plan

  • npm test — 670/670 tests pass (incl. existing CLI integration tests in cli-entry.test.js, metadata-cli.test.js, export.test.js that exercise the migrated read, info, children paths and the error → handleCommandError → exit-1 path)
  • npm run lint — clean
  • node bin/index.js --help — all commands registered and visible
  • Syntax check via node -c

Each of the 26 client-using commands repeated the same Analytics +
getConfig + assertWritable + ConfluenceClient + try/handleCommandError
scaffolding. Extracted into a single withClient(name, handler, { writable })
helper so handlers focus on command logic. Tracking semantics are
preserved (handlers still call analytics.track on success so cancel/
dry-run/conditional keys keep working).

bin/confluence.js: 940 insertions, 1128 deletions.
@pchuri pchuri self-assigned this May 10, 2026
@pchuri pchuri merged commit 1b27732 into main May 10, 2026
6 checks passed
@pchuri pchuri deleted the refactor/cli-with-client-helper branch May 10, 2026 12:26
pchuri added a commit that referenced this pull request May 11, 2026
* refactor(cli): migrate comment command to withClient with onError hook

The comment command was the only client-using command left with manual
analytics + getConfig + assertWritable + ConfluenceClient + try/catch
scaffolding after #181, because its catch block needed to print API
response details and inline-comment-specific hints.

Add an `onError(error, ...actionArgs)` option to withClient that runs
between the standard `Error:` log line and process.exit, and extend
handleCommandError with a matching `onExtra` callback. The hook is
wrapped in try/catch so a throwing hint path cannot break the error
flow. Move the comment command's API-response dump and inline-meta
hint into the hook. Existing behavior (output ordering, exit code,
failure tracking) is preserved.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(cli): add unit tests for withClient wrapper

The withClient helper sits on the shared error/analytics/client path for
every CLI command that talks to the server but had no direct test
coverage. Add six cases covering: context injection ({ client, config,
analytics } + forwarded action args), the writable-on-readOnly exit
path, failure tracking on handler throw, the new onError hook
(invocation order before process.exit, action-arg forwarding, throw
isolation), and getConfig failure being treated as a tracked failure.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 2.6.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant