Skip to content

feat: add requireConfirmation, previewUnlessConfirmed and printDryRun - #198

Merged
scottlovegrove merged 2 commits into
mainfrom
scottl/confirm-or-preview
Dec 3, 2026
Merged

scottlovegrove merged 2 commits into
mainfrom
scottl/confirm-or-preview

Conversation

@scottlovegrove

Copy link
Copy Markdown
Collaborator

The CLIs each hand-roll the --yes / --dry-run flow for destructive commands, and they have drifted apart. todoist-cli throws CONFIRMATION_REQUIRED ("Pass --yes to execute …, or --dry-run to preview it.") and emits a { status: "preview", command, messages } object under --dry-run --json. twist-cli and comms-cli throw MISSING_YES_FLAG and print a plain-text dry run even under --json. todoist-cli, twist-cli and comms-cli each carry their own printDryRun.

This adds the shared version to the root entry point:

  • requireConfirmation(options, { command, hints }) throws CONFIRMATION_REQUIRED when a --json or --quiet run has neither --yes nor --dry-run. Callers run it before any request, so a refused run sends nothing. quiet comes from each CLI's own global args store.
  • previewUnlessConfirmed(options, { command, action, summary, notes, details }) runs the same guard, then prints the JSON preview envelope (--dry-run --json), the printDryRun rows (--dry-run), or the summary, notes and Use --yes to confirm. (no --yes), and returns true when it printed a preview. notes double as the error's hints and the envelope's extra messages.
  • printDryRun(action, details) is the twist-cli and comms-cli version, which indents the continuation lines of a multi-line value. For single-line values it prints exactly what todoist-cli's copy prints.

CONFIRMATION_REQUIRED joins CliErrorCode. The change is additive. todoist-cli adopts it in a follow-up once this is released, with no behaviour change there. twist-cli and comms-cli would switch from MISSING_YES_FLAG to CONFIRMATION_REQUIRED when they adopt it, which is a breaking change for their scripts, so those are separate decisions.

The CLIs each hand-roll the --yes / --dry-run flow for destructive
commands, and they have drifted: todoist-cli throws
CONFIRMATION_REQUIRED and emits a { status: "preview" } object under
--dry-run --json, twist-cli and comms-cli throw MISSING_YES_FLAG and
print a plain-text dry run even under --json, and three copies of
printDryRun live in the CLIs.

requireConfirmation refuses a --json or --quiet run that has neither
--yes nor --dry-run with CONFIRMATION_REQUIRED, before any request.
previewUnlessConfirmed prints the dry-run preview (or the JSON preview
envelope), or the summary and "Use --yes to confirm." without --yes,
and reports whether it did. printDryRun is the multi-line twist-cli and
comms-cli version. CONFIRMATION_REQUIRED joins CliErrorCode.
@scottlovegrove scottlovegrove self-assigned this Dec 3, 2026

@doistbot doistbot left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds shared requireConfirmation, previewUnlessConfirmed and printDryRun helpers to the root entry point, unifying the --yes/--dry-run flow with a new CONFIRMATION_REQUIRED error code, plus tests and docs.

Few things worth tightening:

  • Under --quiet --dry-run without --json, the guard treats the run as script mode but the preview branch still prints human-readable rows to stdout — the exact failure the doc says it prevents. Either emit the JSON envelope for quiet too or document the required quiet+json pairing, and pin it with a test.
  • --ndjson is a first-class machine mode elsewhere in the package, but the helper ignores it: --ndjson without --yes prints human lines, and --ndjson --dry-run prints the plain-text dry-run block. Add ndjson to ConfirmationOptions and include it in both the guard and the preview branch.

I also included a few optional follow-up notes in the details below.

Optional follow-up notes (2)
  • P3 src/confirmation.test.ts:41: The allowed-through matrix covers --json --dry-run but not --quiet --dry-run. Both pass only because the guard checks !options.dryRun, so a regression that made the quiet branch refuse dry runs would still be caught on the JSON half. Add ['--quiet --dry-run', { quiet: true, dryRun: true }] beside the --quiet --yes row to pin the symmetry the two script-mode flags are meant to have.
  • P3 README.md:23: This row only has two cells (module + exports); the Purpose cell is missing, so the row renders short and readers get no description of what confirmation does. AGENTS.md ("README maintenance") requires the "What's in it" table to reflect the change. Add the third cell, e.g. what requireConfirmation / previewUnlessConfirmed / printDryRun do (--yes / --dry-run gating for destructive commands, one shared implementation for the CLIs).

Share Feedback • Review Logs • Trace

Comment thread src/confirmation.ts Resolved
Comment thread src/confirmation.ts Resolved
--quiet hides success messages rather than switching to JSON, so a
--quiet --dry-run run prints the dry-run rows it asked for. Reword the
docs, which called --quiet a script mode, and pin that behaviour in
both helpers.
@scottlovegrove scottlovegrove added the 🚢 Ship PR requires passing checks before merge, but no independent human review. label Dec 3, 2026
@scottlovegrove
scottlovegrove merged commit 368d0c2 into main Dec 3, 2026
5 checks passed
@scottlovegrove
scottlovegrove deleted the scottl/confirm-or-preview branch December 3, 2026 12:39
doist-release-bot Bot added a commit that referenced this pull request Dec 3, 2026
## [2.1.0](v2.0.3...v2.1.0) (2026-12-03)

### Features

* add requireConfirmation, previewUnlessConfirmed and printDryRun ([#198](#198)) ([368d0c2](368d0c2))
@doist-release-bot

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 2.1.0 🎉

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

released 🚢 Ship PR requires passing checks before merge, but no independent human review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants