diff --git a/CODEBASE.md b/CODEBASE.md index 5d9a0bb..b4c9ebb 100644 --- a/CODEBASE.md +++ b/CODEBASE.md @@ -145,7 +145,7 @@ don't duplicate it here. `resolveGroupRef`, `resolveUserRefs`, `parseNotifyIdRefs`. - **`output.ts`** — `formatJson` / `formatNdjson` (+ paginated variants) with per-`EntityType` essential-field filtering, `formatError` / `formatErrorJson`, - `printJson` / `printNdjson` / `printEmpty` / `printDryRun`, `colors`, `pluralize`, + `printJson` / `printNdjson` / `printEmpty`, `colors`, `pluralize`, `closedThreadBadge` (the `(closed)` marker after a thread title), `pinnedBadge` (the `(pinned)` marker after a pinned comment's or message's time). - **`options.ts`** — `ViewOptions`, `PaginatedViewOptions`, `MutationOptions` @@ -205,6 +205,9 @@ don't duplicate it here. - **Mutation with `--json`:** `src/commands/channel/create.ts` — the reference impl for `MutationOptions`, `ensureWriteAllowed`, `printDryRun`, and `formatJson(entity, 'channel', options.full)`. +- **Destructive command behind `--yes`:** `src/commands/channel/delete.ts` — + `requireConfirmation` before any request and `previewUnlessConfirmed` once + the target is resolved, both from `@todoist/cli-core` (as is `printDryRun`). - **Grouped command:** `src/commands/thread/index.ts` + siblings — implicit `view` default, one file per subcommand, `thread/helpers.ts` for shared logic. diff --git a/package-lock.json b/package-lock.json index bcb1265..9c7603f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "license": "MIT", "dependencies": { "@pnpm/tabtab": "0.5.4", - "@todoist/cli-core": "2.0.3", + "@todoist/cli-core": "2.1.0", "@todoist/comms-sdk": "6.2.1", "chalk": "5.6.2", "commander": "14.0.3", @@ -2656,9 +2656,9 @@ "license": "MIT" }, "node_modules/@todoist/cli-core": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/@todoist/cli-core/-/cli-core-2.0.3.tgz", - "integrity": "sha512-i8d7c9xurigZ8Zn1jRsuyqSoxhCM5tWG7TJcQR/TM3d4g2ClIECX6EcH7NYDZAKJWg20p9tsreVwJ8qE3p/xhw==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@todoist/cli-core/-/cli-core-2.1.0.tgz", + "integrity": "sha512-dCOi1JRWcDxbf9oBrM9JJ8s/aLcWnl9c+NYKr+jDrQO83r4oQT96XQsB9D0kP+X/ias80juj7tRnKbJ8tR3cyw==", "license": "MIT", "dependencies": { "chalk": "6.0.1", diff --git a/package.json b/package.json index 0c2b149..620e489 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ ], "dependencies": { "@pnpm/tabtab": "0.5.4", - "@todoist/cli-core": "2.0.3", + "@todoist/cli-core": "2.1.0", "@todoist/comms-sdk": "6.2.1", "chalk": "5.6.2", "commander": "14.0.3", diff --git a/skills/comms-cli/SKILL.md b/skills/comms-cli/SKILL.md index 84db988..1ae8309 100644 --- a/skills/comms-cli/SKILL.md +++ b/skills/comms-cli/SKILL.md @@ -474,6 +474,8 @@ Mutating commands accept `--dry-run` to preview the operation without making the Run without --dry-run to execute. ``` +Destructive commands (`channel delete`, `comment delete`, `msg delete`, `groups delete`, `thread delete`, `thread done`/`undone`, `conversation done`/`undone`, and bulk `thread mark-read`/`mark-unread`) only preview without `--yes`: they print `Would …` and `Use --yes to confirm.` and change nothing. Under `--json`, a missing `--yes` (without `--dry-run`) fails with `CONFIRMATION_REQUIRED` ("Pass --yes to execute , or --dry-run to preview it.") before any API call, so a script never reads a preview as the result. `--dry-run --json` prints one `{ "status": "preview", "command", "messages" }` object instead of the text preview; bulk `thread mark-read`/`mark-unread` keep their per-thread rows with `dryRun: true`. + ## Reference System Commands accept flexible references: diff --git a/src/commands/channel/archive.ts b/src/commands/channel/archive.ts index 0fff20e..d30a0b5 100644 --- a/src/commands/channel/archive.ts +++ b/src/commands/channel/archive.ts @@ -1,6 +1,7 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveChannelByRef } from './helpers.js' type ArchiveChannelOptions = MutationOptions & { workspace?: string } diff --git a/src/commands/channel/channel.test.ts b/src/commands/channel/channel.test.ts index aa64fc1..4a5ed59 100644 --- a/src/commands/channel/channel.test.ts +++ b/src/commands/channel/channel.test.ts @@ -668,10 +668,16 @@ describe('channels delete', () => { apiMocks.getCommsClient.mockResolvedValue(client) const consoleSpy = captureConsole('log') - await runChannelCommand(['delete', 'Engineering']) + // A ref that differs from the channel's name, so the preview has to + // name the resolved channel rather than echo what was typed. + await runChannelCommand(['delete', 'eng']) + expect(refsMocks.resolveChannelRef).toHaveBeenCalledWith('eng', 1) expect(client.channels.deleteChannel).not.toHaveBeenCalled() - expect(consoleSpy.mock.calls.some((c) => String(c[0]).includes('Use --yes'))).toBe(true) + expect(consoleSpy.mock.calls.map((c) => c[0])).toEqual([ + 'Would delete: Engineering (id:CH500)', + 'Use --yes to confirm.', + ]) }) it('deletes when --yes is passed', async () => { @@ -714,15 +720,31 @@ describe('channels delete', () => { const client = createClient() apiMocks.getCommsClient.mockResolvedValue(client) - await expect(runChannelCommand(['delete', 'Engineering', '--json'])).rejects.toHaveProperty( - 'code', - 'MISSING_YES_FLAG', - ) + await expect(runChannelCommand(['delete', 'Engineering', '--json'])).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc channel delete, or --dry-run to preview it.', + }) expect(client.channels.deleteChannel).not.toHaveBeenCalled() expect(refsMocks.resolveChannelRef).not.toHaveBeenCalled() expect(apiMocks.getCurrentWorkspaceId).not.toHaveBeenCalled() }) + it('prints a JSON preview with --dry-run --json', async () => { + const client = createClient() + apiMocks.getCommsClient.mockResolvedValue(client) + const consoleSpy = captureConsole('log') + + await runChannelCommand(['delete', 'eng', '--dry-run', '--json']) + + expect(client.channels.deleteChannel).not.toHaveBeenCalled() + expect(consoleSpy).toHaveBeenCalledTimes(1) + expect(JSON.parse(consoleSpy.mock.calls[0][0])).toEqual({ + status: 'preview', + command: 'tdc channel delete', + messages: ['Would delete: Engineering (id:CH500)'], + }) + }) + it('outputs JSON result with --yes --json', async () => { const client = createClient() apiMocks.getCommsClient.mockResolvedValue(client) diff --git a/src/commands/channel/create.ts b/src/commands/channel/create.ts index 4a36daa..4d9926e 100644 --- a/src/commands/channel/create.ts +++ b/src/commands/channel/create.ts @@ -1,7 +1,8 @@ +import { printDryRun } from '@todoist/cli-core' import type { CreateChannelArgs } from '@todoist/comms-sdk' import { getCommsClient } from '../../lib/api.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveUserRefs } from '../../lib/refs.js' import { resolveChannelWorkspaceId, diff --git a/src/commands/channel/delete.ts b/src/commands/channel/delete.ts index aaa47b7..fb160cc 100644 --- a/src/commands/channel/delete.ts +++ b/src/commands/channel/delete.ts @@ -1,31 +1,29 @@ +import { previewUnlessConfirmed, requireConfirmation } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' -import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveChannelByRef } from './helpers.js' type DeleteChannelOptions = MutationOptions & { yes?: boolean; workspace?: string } +const COMMAND = 'tdc channel delete' + export async function deleteChannel(ref: string, options: DeleteChannelOptions): Promise { - if (!options.yes && !options.dryRun) { - if (options.json) { - throw new CliError( - 'MISSING_YES_FLAG', - '--yes is required to execute deletion in --json mode.', - ) - } - console.log(`Would delete: ${ref}`) - console.log('Use --yes to confirm.') - return - } + requireConfirmation(options, { command: COMMAND }) const channel = await resolveChannelByRef(ref, options.workspace) - if (options.dryRun) { - printDryRun('delete channel', { - Channel: `${channel.name} (id:${channel.id})`, - Visibility: channel.public ? 'public' : 'private', + if ( + previewUnlessConfirmed(options, { + command: COMMAND, + action: 'delete channel', + summary: `Would delete: ${channel.name} (id:${channel.id})`, + details: { + Channel: `${channel.name} (id:${channel.id})`, + Visibility: channel.public ? 'public' : 'private', + }, }) + ) { return } diff --git a/src/commands/channel/membership-helpers.ts b/src/commands/channel/membership-helpers.ts index ea1969a..1a43aad 100644 --- a/src/commands/channel/membership-helpers.ts +++ b/src/commands/channel/membership-helpers.ts @@ -1,3 +1,4 @@ +import { printDryRun } from '@todoist/cli-core' import type { Channel, Group, VisibleWorkspaceUser } from '@todoist/comms-sdk' import { addUsersToChannel, @@ -6,7 +7,7 @@ import { removeUsersFromChannel, } from '../../lib/api.js' import type { MutationOptions } from '../../lib/options.js' -import { colors, formatJson, pluralize, printDryRun } from '../../lib/output.js' +import { colors, formatJson, pluralize } from '../../lib/output.js' import { resolveChannelMemberRefs, resolveChannelRef } from '../../lib/refs.js' export type ChannelMutationOptions = MutationOptions diff --git a/src/commands/channel/set.ts b/src/commands/channel/set.ts index ee3adef..f858007 100644 --- a/src/commands/channel/set.ts +++ b/src/commands/channel/set.ts @@ -1,3 +1,4 @@ +import { printDryRun } from '@todoist/cli-core' import type { User } from '@todoist/comms-sdk' import { addUsersToChannel, @@ -8,7 +9,7 @@ import { } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, pluralize, printDryRun } from '../../lib/output.js' +import { formatJson, pluralize } from '../../lib/output.js' import { resolveChannelMemberRefs, resolveChannelRef } from '../../lib/refs.js' import { channelUserIds, describeExpansion, logExpansion } from './membership-helpers.js' diff --git a/src/commands/channel/update.ts b/src/commands/channel/update.ts index ca45f2e..254cfdf 100644 --- a/src/commands/channel/update.ts +++ b/src/commands/channel/update.ts @@ -1,8 +1,9 @@ +import { printDryRun } from '@todoist/cli-core' import type { UpdateChannelArgs } from '@todoist/comms-sdk' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { getDirectChannelId, resolveChannelRef } from '../../lib/refs.js' import { resolveChannelWorkspaceId, diff --git a/src/commands/comment/comment.test.ts b/src/commands/comment/comment.test.ts index 4ba7633..1128913 100644 --- a/src/commands/comment/comment.test.ts +++ b/src/commands/comment/comment.test.ts @@ -361,10 +361,31 @@ describe('comment delete', () => { await expect( program.parseAsync(['node', 'tdc', 'comment', 'delete', '300', '--json']), - ).rejects.toHaveProperty('code', 'MISSING_YES_FLAG') + ).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc comment delete, or --dry-run to preview it.', + }) + expect(client.comments.getComment).not.toHaveBeenCalled() expect(client.comments.deleteComment).not.toHaveBeenCalled() }) + + it('prints a JSON preview with --dry-run --json', async () => { + const client = createClient() + apiMocks.getCommsClient.mockResolvedValue(client) + const program = createProgram() + const consoleSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'comment', 'delete', '300', '--dry-run', '--json']) + + expect(client.comments.deleteComment).not.toHaveBeenCalled() + expect(consoleSpy).toHaveBeenCalledTimes(1) + expect(JSON.parse(consoleSpy.mock.calls[0][0])).toEqual({ + status: 'preview', + command: 'tdc comment delete', + messages: ['Would delete comment 300'], + }) + }) }) describe('comment pin and unpin', () => { diff --git a/src/commands/comment/delete.ts b/src/commands/comment/delete.ts index 2eddf55..87f1a29 100644 --- a/src/commands/comment/delete.ts +++ b/src/commands/comment/delete.ts @@ -1,24 +1,16 @@ +import { previewUnlessConfirmed, requireConfirmation } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { assertChannelIsPublic } from '../../lib/public-channels.js' import { resolveCommentId } from '../../lib/refs.js' +const COMMAND = 'tdc comment delete' + export async function deleteComment(ref: string, options: MutationOptions): Promise { const commentId = resolveCommentId(ref) - - if (!options.yes && !options.dryRun) { - if (options.json) { - throw new CliError( - 'MISSING_YES_FLAG', - '--yes is required to execute deletion in --json mode.', - ) - } - console.log(`Would delete comment ${commentId}`) - console.log('Use --yes to confirm.') - return - } + requireConfirmation(options, { command: COMMAND }) const client = await getCommsClient() const [comment, user] = await Promise.all([ @@ -32,14 +24,16 @@ export async function deleteComment(ref: string, options: MutationOptions): Prom throw new CliError('NOT_CREATOR', 'You can only delete comments that you created.') } - if (options.dryRun) { - const preview = - comment.content.length > 200 ? `${comment.content.slice(0, 200)}...` : comment.content - printDryRun('delete comment', { - Comment: commentId, - Thread: comment.threadId, - Content: preview, + const contentPreview = + comment.content.length > 200 ? `${comment.content.slice(0, 200)}...` : comment.content + if ( + previewUnlessConfirmed(options, { + command: COMMAND, + action: 'delete comment', + summary: `Would delete comment ${commentId}`, + details: { Comment: commentId, Thread: comment.threadId, Content: contentPreview }, }) + ) { return } diff --git a/src/commands/comment/pin.ts b/src/commands/comment/pin.ts index 12db1cd..7a0ec2b 100644 --- a/src/commands/comment/pin.ts +++ b/src/commands/comment/pin.ts @@ -1,6 +1,7 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { assertChannelIsPublic } from '../../lib/public-channels.js' import { resolveCommentId } from '../../lib/refs.js' diff --git a/src/commands/comment/update.ts b/src/commands/comment/update.ts index 991b675..cc22ae6 100644 --- a/src/commands/comment/update.ts +++ b/src/commands/comment/update.ts @@ -1,8 +1,9 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import { openEditor, readContent } from '../../lib/input.js' import type { MutationOptions, StdinOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveCommentId } from '../../lib/refs.js' type UpdateOptions = MutationOptions & StdinOptions diff --git a/src/commands/conversation/archive.ts b/src/commands/conversation/archive.ts index f43b4e3..b1e9144 100644 --- a/src/commands/conversation/archive.ts +++ b/src/commands/conversation/archive.ts @@ -1,6 +1,6 @@ +import { previewUnlessConfirmed, requireConfirmation } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' -import { CliError } from '../../lib/errors.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveConversationId } from '../../lib/refs.js' import { conversationLabel, type DoneOptions } from './helpers.js' @@ -10,30 +10,26 @@ async function setConversationArchiveState( archive: boolean, ): Promise { const action = archive ? 'archive' : 'unarchive' + const command = `tdc conversation ${archive ? 'done' : 'undone'}` const conversationId = resolveConversationId(ref) + requireConfirmation(options, { command }) const client = await getCommsClient() const conversation = await client.conversations.getConversation(conversationId) const noop = conversation.archived === archive - if (options.dryRun) { - printDryRun(`${action} conversation`, { - Conversation: conversationLabel(conversation), - Status: noop ? (archive ? 'already archived' : 'not archived') : undefined, + if ( + previewUnlessConfirmed(options, { + command, + action: `${action} conversation`, + summary: `Would ${action}: ${conversationLabel(conversation)}`, + details: { + Conversation: conversationLabel(conversation), + Status: noop ? (archive ? 'already archived' : 'not archived') : undefined, + }, }) - return - } - - if (!options.yes) { - if (options.json) { - throw new CliError( - 'MISSING_YES_FLAG', - `--yes is required to execute ${action} in --json mode.`, - ) - } - console.log(`Would ${action}: ${conversationLabel(conversation)}`) - console.log('Use --yes to confirm.') + ) { return } diff --git a/src/commands/conversation/conversation.test.ts b/src/commands/conversation/conversation.test.ts index c6b754c..c9fe97c 100644 --- a/src/commands/conversation/conversation.test.ts +++ b/src/commands/conversation/conversation.test.ts @@ -1199,9 +1199,39 @@ describe('conversation done', () => { await expect( program.parseAsync(['node', 'tdc', 'conversation', 'done', '42', '--json']), - ).rejects.toHaveProperty('code', 'MISSING_YES_FLAG') + ).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc conversation done, or --dry-run to preview it.', + }) + + expect(client.conversations.getConversation).not.toHaveBeenCalled() + expect(client.conversations.archiveConversation).not.toHaveBeenCalled() + }) + + it('prints a JSON preview with --dry-run --json', async () => { + const conversation = createConversation(42, [1, 2], '2026-03-08T10:00:00.000Z') + const client = createClient({ activeConversations: [conversation] }) + apiMocks.getCommsClient.mockResolvedValue(client) + const program = createProgram() + const consoleSpy = captureConsole('log') + + await program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'done', + '42', + '--dry-run', + '--json', + ]) expect(client.conversations.archiveConversation).not.toHaveBeenCalled() + expect(consoleSpy).toHaveBeenCalledTimes(1) + expect(JSON.parse(consoleSpy.mock.calls[0][0])).toEqual({ + status: 'preview', + command: 'tdc conversation done', + messages: ['Would archive: conversation 42'], + }) }) it('shows dry run output', async () => { @@ -1317,11 +1347,40 @@ describe('conversation undone', () => { await expect( program.parseAsync(['node', 'tdc', 'conversation', 'undone', '42', '--json']), - ).rejects.toHaveProperty('code', 'MISSING_YES_FLAG') + ).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc conversation undone, or --dry-run to preview it.', + }) + expect(client.conversations.getConversation).not.toHaveBeenCalled() expect(client.conversations.unarchiveConversation).not.toHaveBeenCalled() }) + it('prints a JSON preview with --dry-run --json', async () => { + const conversation = createConversation(42, [1, 2], '2026-03-08T10:00:00.000Z') + const client = createClient({ activeConversations: [conversation] }) + apiMocks.getCommsClient.mockResolvedValue(client) + const program = createProgram() + const consoleSpy = captureConsole('log') + + await program.parseAsync([ + 'node', + 'tdc', + 'conversation', + 'undone', + '42', + '--dry-run', + '--json', + ]) + + expect(consoleSpy).toHaveBeenCalledTimes(1) + expect(JSON.parse(consoleSpy.mock.calls[0][0])).toEqual({ + status: 'preview', + command: 'tdc conversation undone', + messages: ['Would unarchive: conversation 42'], + }) + }) + it('shows dry run output for an archived conversation without a status line', async () => { const conversation = { ...createConversation(42, [1, 2], '2026-03-08T10:00:00.000Z'), diff --git a/src/commands/conversation/mute.ts b/src/commands/conversation/mute.ts index b37220a..6b5fad4 100644 --- a/src/commands/conversation/mute.ts +++ b/src/commands/conversation/mute.ts @@ -1,5 +1,6 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveConversationId } from '../../lib/refs.js' import { conversationLabel, type MuteOptions, parseMinutes } from './helpers.js' diff --git a/src/commands/conversation/reply.ts b/src/commands/conversation/reply.ts index 6626080..74f9557 100644 --- a/src/commands/conversation/reply.ts +++ b/src/commands/conversation/reply.ts @@ -1,9 +1,10 @@ +import { printDryRun } from '@todoist/cli-core' import chalk from 'chalk' import { getCommsClient } from '../../lib/api.js' import { uploadAttachments, validateAttachmentFiles } from '../../lib/attachments.js' import { CliError } from '../../lib/errors.js' import { openEditor, readContent } from '../../lib/input.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveConversationId } from '../../lib/refs.js' import type { ReplyOptions } from './helpers.js' diff --git a/src/commands/conversation/unmute.ts b/src/commands/conversation/unmute.ts index 6adca4b..b26b842 100644 --- a/src/commands/conversation/unmute.ts +++ b/src/commands/conversation/unmute.ts @@ -1,6 +1,7 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveConversationId } from '../../lib/refs.js' import { conversationLabel } from './helpers.js' diff --git a/src/commands/groups/create.ts b/src/commands/groups/create.ts index df6d6de..879f7c3 100644 --- a/src/commands/groups/create.ts +++ b/src/commands/groups/create.ts @@ -1,6 +1,7 @@ +import { printDryRun } from '@todoist/cli-core' import { createGroup, getCurrentWorkspaceId } from '../../lib/api.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveUserRefs, resolveWorkspaceRef } from '../../lib/refs.js' import { validateNonEmptyName } from '../../lib/validation.js' diff --git a/src/commands/groups/delete.ts b/src/commands/groups/delete.ts index 6fc09b1..05c0848 100644 --- a/src/commands/groups/delete.ts +++ b/src/commands/groups/delete.ts @@ -1,32 +1,30 @@ +import { previewUnlessConfirmed, requireConfirmation } from '@todoist/cli-core' import { deleteGroup, getCurrentWorkspaceId } from '../../lib/api.js' -import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveGroupRef } from '../../lib/refs.js' type DeleteGroupOptions = MutationOptions & { yes?: boolean } +const COMMAND = 'tdc groups delete' + export async function deleteGroupCommand(ref: string, options: DeleteGroupOptions): Promise { + requireConfirmation(options, { command: COMMAND }) + const workspaceId = await getCurrentWorkspaceId() const group = await resolveGroupRef(ref, workspaceId) - if (options.dryRun) { - printDryRun('delete group', { - Group: `${group.name} (id:${group.id})`, - Members: `${group.userIds.length}`, + if ( + previewUnlessConfirmed(options, { + command: COMMAND, + action: 'delete group', + summary: `Would delete: ${group.name} (id:${group.id}, ${group.userIds.length} members)`, + details: { + Group: `${group.name} (id:${group.id})`, + Members: `${group.userIds.length}`, + }, }) - return - } - - if (!options.yes) { - if (options.json) { - throw new CliError( - 'MISSING_YES_FLAG', - '--yes is required to execute deletion in --json mode.', - ) - } - console.log(`Would delete: ${group.name} (id:${group.id}, ${group.userIds.length} members)`) - console.log('Use --yes to confirm.') + ) { return } diff --git a/src/commands/groups/groups.test.ts b/src/commands/groups/groups.test.ts index 08f7b04..522b84d 100644 --- a/src/commands/groups/groups.test.ts +++ b/src/commands/groups/groups.test.ts @@ -377,7 +377,10 @@ describe('tdc groups delete', () => { await program.parseAsync(['node', 'tdc', 'groups', 'delete', 'Frontend']) expect(apiMocks.deleteGroup).not.toHaveBeenCalled() - expect(consoleSpy.mock.calls.some((c) => String(c[0]).includes('Use --yes'))).toBe(true) + expect(consoleSpy.mock.calls.map((c) => c[0])).toEqual([ + 'Would delete: Frontend (id:GR100, 3 members)', + 'Use --yes to confirm.', + ]) }) it('deletes when --yes is passed', async () => { @@ -389,11 +392,39 @@ describe('tdc groups delete', () => { expect(apiMocks.deleteGroup).toHaveBeenCalledWith('GR100', 1) }) - it('errors in --json mode without --yes', async () => { + it('errors in --json mode without --yes before looking the group up', async () => { const program = createProgram() await expect( program.parseAsync(['node', 'tdc', 'groups', 'delete', 'Frontend', '--json']), - ).rejects.toMatchObject({ code: 'MISSING_YES_FLAG' }) + ).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc groups delete, or --dry-run to preview it.', + }) + expect(refsMocks.resolveGroupRef).not.toHaveBeenCalled() + expect(apiMocks.deleteGroup).not.toHaveBeenCalled() + }) + + it('prints a JSON preview with --dry-run --json', async () => { + const program = createProgram() + const consoleSpy = captureConsole('log') + + await program.parseAsync([ + 'node', + 'tdc', + 'groups', + 'delete', + 'Frontend', + '--dry-run', + '--json', + ]) + + expect(apiMocks.deleteGroup).not.toHaveBeenCalled() + expect(consoleSpy).toHaveBeenCalledTimes(1) + expect(JSON.parse(consoleSpy.mock.calls[0][0])).toEqual({ + status: 'preview', + command: 'tdc groups delete', + messages: ['Would delete: Frontend (id:GR100, 3 members)'], + }) }) }) diff --git a/src/commands/groups/members.ts b/src/commands/groups/members.ts index d0284e8..e8ba5f5 100644 --- a/src/commands/groups/members.ts +++ b/src/commands/groups/members.ts @@ -1,3 +1,4 @@ +import { printDryRun } from '@todoist/cli-core' import { addUsersToGroup, getCurrentWorkspaceId, @@ -6,7 +7,7 @@ import { } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveGroupRef, resolveUserRefs } from '../../lib/refs.js' function joinUserRefs(userRefs: string[]): string { diff --git a/src/commands/groups/rename.ts b/src/commands/groups/rename.ts index 52481d0..2b18010 100644 --- a/src/commands/groups/rename.ts +++ b/src/commands/groups/rename.ts @@ -1,6 +1,7 @@ +import { printDryRun } from '@todoist/cli-core' import { getCurrentWorkspaceId, updateGroup } from '../../lib/api.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveGroupRef } from '../../lib/refs.js' import { validateNonEmptyName } from '../../lib/validation.js' diff --git a/src/commands/msg/delete.ts b/src/commands/msg/delete.ts index 3f35ac7..035cbe3 100644 --- a/src/commands/msg/delete.ts +++ b/src/commands/msg/delete.ts @@ -1,23 +1,15 @@ +import { previewUnlessConfirmed, requireConfirmation } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveMessageId } from '../../lib/refs.js' +const COMMAND = 'tdc msg delete' + export async function deleteMessage(ref: string, options: MutationOptions): Promise { const messageId = resolveMessageId(ref) - - if (!options.yes && !options.dryRun) { - if (options.json) { - throw new CliError( - 'MISSING_YES_FLAG', - '--yes is required to execute deletion in --json mode.', - ) - } - console.log(`Would delete message ${messageId}`) - console.log('Use --yes to confirm.') - return - } + requireConfirmation(options, { command: COMMAND }) const client = await getCommsClient() const [message, user] = await Promise.all([ @@ -29,14 +21,20 @@ export async function deleteMessage(ref: string, options: MutationOptions): Prom throw new CliError('NOT_CREATOR', 'You can only delete messages that you created.') } - if (options.dryRun) { - const preview = - message.content.length > 200 ? `${message.content.slice(0, 200)}...` : message.content - printDryRun('delete message', { - Message: messageId, - Conversation: message.conversationId, - Content: preview, + const contentPreview = + message.content.length > 200 ? `${message.content.slice(0, 200)}...` : message.content + if ( + previewUnlessConfirmed(options, { + command: COMMAND, + action: 'delete message', + summary: `Would delete message ${messageId}`, + details: { + Message: messageId, + Conversation: message.conversationId, + Content: contentPreview, + }, }) + ) { return } diff --git a/src/commands/msg/msg.test.ts b/src/commands/msg/msg.test.ts index 0b112fb..dae3904 100644 --- a/src/commands/msg/msg.test.ts +++ b/src/commands/msg/msg.test.ts @@ -213,10 +213,31 @@ describe('msg delete', () => { await expect( program.parseAsync(['node', 'tdc', 'msg', 'delete', '200', '--json']), - ).rejects.toHaveProperty('code', 'MISSING_YES_FLAG') + ).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc msg delete, or --dry-run to preview it.', + }) + expect(client.conversationMessages.getMessage).not.toHaveBeenCalled() expect(client.conversationMessages.deleteMessage).not.toHaveBeenCalled() }) + + it('prints a JSON preview with --dry-run --json', async () => { + const client = createClient() + apiMocks.getCommsClient.mockResolvedValue(client) + const program = createProgram() + const consoleSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'msg', 'delete', '200', '--dry-run', '--json']) + + expect(client.conversationMessages.deleteMessage).not.toHaveBeenCalled() + expect(consoleSpy).toHaveBeenCalledTimes(1) + expect(JSON.parse(consoleSpy.mock.calls[0][0])).toEqual({ + status: 'preview', + command: 'tdc msg delete', + messages: ['Would delete message 200'], + }) + }) }) describe('msg update', () => { diff --git a/src/commands/msg/pin.ts b/src/commands/msg/pin.ts index 41b0562..026dc79 100644 --- a/src/commands/msg/pin.ts +++ b/src/commands/msg/pin.ts @@ -1,6 +1,7 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveMessageId } from '../../lib/refs.js' export function pinMessage(ref: string, options: MutationOptions): Promise { diff --git a/src/commands/msg/update.ts b/src/commands/msg/update.ts index cebc982..ae1c094 100644 --- a/src/commands/msg/update.ts +++ b/src/commands/msg/update.ts @@ -1,8 +1,9 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import { openEditor, readContent } from '../../lib/input.js' import type { MutationOptions, StdinOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { resolveMessageId } from '../../lib/refs.js' type UpdateOptions = MutationOptions & StdinOptions diff --git a/src/commands/react.ts b/src/commands/react.ts index 16ea6b5..e54c802 100644 --- a/src/commands/react.ts +++ b/src/commands/react.ts @@ -1,8 +1,9 @@ +import { printDryRun } from '@todoist/cli-core' import { Command } from 'commander' import { getCommsClient } from '../lib/api.js' import { CliError } from '../lib/errors.js' import type { MutationOptions } from '../lib/options.js' -import { formatJson, printDryRun } from '../lib/output.js' +import { formatJson } from '../lib/output.js' import { resolveCommentId, resolveMessageId, resolveThreadId } from '../lib/refs.js' type TargetType = 'thread' | 'comment' | 'message' diff --git a/src/commands/status/clear.ts b/src/commands/status/clear.ts index 8d0da42..cc1b898 100644 --- a/src/commands/status/clear.ts +++ b/src/commands/status/clear.ts @@ -1,7 +1,7 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { printDryRun } from '../../lib/output.js' import { formatStatus } from '../../lib/status.js' import { formatStatusJson } from './helpers.js' diff --git a/src/commands/status/set.ts b/src/commands/status/set.ts index b9bc5b0..8a9aa98 100644 --- a/src/commands/status/set.ts +++ b/src/commands/status/set.ts @@ -1,10 +1,10 @@ +import { printDryRun } from '@todoist/cli-core' import type { SetStatusArgs } from '@todoist/comms-sdk' import { getCommsClient } from '../../lib/api.js' import { formatDateTime, parseLocalDate } from '../../lib/dates.js' import { parseDuration } from '../../lib/duration.js' import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { printDryRun } from '../../lib/output.js' import { formatStatus } from '../../lib/status.js' import { formatStatusJson } from './helpers.js' diff --git a/src/commands/thread/create.ts b/src/commands/thread/create.ts index 276a8fe..974feda 100644 --- a/src/commands/thread/create.ts +++ b/src/commands/thread/create.ts @@ -1,3 +1,4 @@ +import { printDryRun } from '@todoist/cli-core' import chalk from 'chalk' import { getCommsClient } from '../../lib/api.js' import { uploadAttachments, validateAttachmentFiles } from '../../lib/attachments.js' @@ -5,7 +6,7 @@ import { getConfig } from '../../lib/config.js' import { CliError } from '../../lib/errors.js' import { openEditor, readContent } from '../../lib/input.js' import type { MutationOptions, StdinOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { assertChannelIsPublic } from '../../lib/public-channels.js' import { parseNotifyIdRefs, resolveChannelId } from '../../lib/refs.js' import { type ResolvedNotify, formatNotifyLabel, resolveNotifyIds } from './helpers.js' diff --git a/src/commands/thread/delete.ts b/src/commands/thread/delete.ts index 3a6d406..09796dc 100644 --- a/src/commands/thread/delete.ts +++ b/src/commands/thread/delete.ts @@ -1,12 +1,16 @@ +import { previewUnlessConfirmed, requireConfirmation } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { assertChannelIsPublic } from '../../lib/public-channels.js' import { resolveThreadId } from '../../lib/refs.js' +const COMMAND = 'tdc thread delete' + export async function deleteThread(ref: string, options: MutationOptions): Promise { const threadId = resolveThreadId(ref) + requireConfirmation(options, { command: COMMAND }) const client = await getCommsClient() const [thread, user] = await Promise.all([ @@ -20,22 +24,14 @@ export async function deleteThread(ref: string, options: MutationOptions): Promi throw new CliError('NOT_CREATOR', 'You can only delete threads that you created.') } - if (options.dryRun) { - printDryRun('delete thread', { - Thread: `${thread.title} (${threadId})`, + if ( + previewUnlessConfirmed(options, { + command: COMMAND, + action: 'delete thread', + summary: `Would delete: ${thread.title}`, + details: { Thread: `${thread.title} (${threadId})` }, }) - return - } - - if (!options.yes) { - if (options.json) { - throw new CliError( - 'MISSING_YES_FLAG', - '--yes is required to execute deletion in --json mode.', - ) - } - console.log(`Would delete: ${thread.title}`) - console.log('Use --yes to confirm.') + ) { return } diff --git a/src/commands/thread/helpers.ts b/src/commands/thread/helpers.ts index b37f105..004a971 100644 --- a/src/commands/thread/helpers.ts +++ b/src/commands/thread/helpers.ts @@ -1,3 +1,4 @@ +import { requireConfirmation } from '@todoist/cli-core' import type { CommsApi, Thread } from '@todoist/comms-sdk' import chalk from 'chalk' import { getCommsClient, getWorkspaceGroups, getWorkspaceUsers } from '../../lib/api.js' @@ -134,12 +135,11 @@ export async function runThreadReadStateMutation( } mutation.validateRefs?.(rawRefs) + // One thread runs without --yes; a bulk run needs it. The dry-run and + // preview output stays per thread, so only the guard is shared. const needsConfirmation = rawRefs.length > 1 && !options.yes && !options.dryRun - if (options.json && needsConfirmation) { - throw new CliError( - 'MISSING_YES_FLAG', - `--yes is required to execute bulk mark-${mutation.verb} in --json mode.`, - ) + if (needsConfirmation) { + requireConfirmation(options, { command: `tdc thread mark-${mutation.verb}` }) } const client = await getCommsClient() diff --git a/src/commands/thread/mutate.ts b/src/commands/thread/mutate.ts index 4264d32..1fba0ac 100644 --- a/src/commands/thread/mutate.ts +++ b/src/commands/thread/mutate.ts @@ -1,7 +1,7 @@ +import { previewUnlessConfirmed, requireConfirmation } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' -import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { assertChannelIsPublic } from '../../lib/public-channels.js' import { resolveThreadId } from '../../lib/refs.js' import { threadLabel } from './helpers.js' @@ -12,7 +12,9 @@ async function setThreadArchiveState( archive: boolean, ): Promise { const action = archive ? 'archive' : 'unarchive' + const command = `tdc thread ${archive ? 'done' : 'undone'}` const threadId = resolveThreadId(ref) + requireConfirmation(options, { command }) const client = await getCommsClient() const thread = await client.threads.getThread(threadId) @@ -20,23 +22,17 @@ async function setThreadArchiveState( const noop = thread.isArchived === archive - if (options.dryRun) { - printDryRun(`${action} thread`, { - Thread: threadLabel(thread), - Status: noop ? (archive ? 'already archived' : 'already in inbox') : undefined, + if ( + previewUnlessConfirmed(options, { + command, + action: `${action} thread`, + summary: `Would ${action}: ${threadLabel(thread)}`, + details: { + Thread: threadLabel(thread), + Status: noop ? (archive ? 'already archived' : 'already in inbox') : undefined, + }, }) - return - } - - if (!options.yes) { - if (options.json) { - throw new CliError( - 'MISSING_YES_FLAG', - `--yes is required to execute ${action} in --json mode.`, - ) - } - console.log(`Would ${action}: ${threadLabel(thread)}`) - console.log('Use --yes to confirm.') + ) { return } diff --git a/src/commands/thread/mute.ts b/src/commands/thread/mute.ts index af8256c..04cd6b7 100644 --- a/src/commands/thread/mute.ts +++ b/src/commands/thread/mute.ts @@ -1,7 +1,8 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { assertChannelIsPublic } from '../../lib/public-channels.js' import { resolveThreadId } from '../../lib/refs.js' diff --git a/src/commands/thread/rename.ts b/src/commands/thread/rename.ts index e8e544e..233c35b 100644 --- a/src/commands/thread/rename.ts +++ b/src/commands/thread/rename.ts @@ -1,6 +1,7 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import type { MutationOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { assertChannelIsPublic } from '../../lib/public-channels.js' import { resolveThreadId } from '../../lib/refs.js' diff --git a/src/commands/thread/reply.ts b/src/commands/thread/reply.ts index 2c4475a..03fb54c 100644 --- a/src/commands/thread/reply.ts +++ b/src/commands/thread/reply.ts @@ -1,3 +1,4 @@ +import { printDryRun } from '@todoist/cli-core' import type { ThreadAction } from '@todoist/comms-sdk' import chalk from 'chalk' import { getCommsClient } from '../../lib/api.js' @@ -5,7 +6,7 @@ import { uploadAttachments, validateAttachmentFiles } from '../../lib/attachment import { CliError } from '../../lib/errors.js' import { openEditor, readContent } from '../../lib/input.js' import type { MutationOptions, StdinOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { assertChannelIsPublic } from '../../lib/public-channels.js' import { parseNotifyIdRefs, resolveThreadId } from '../../lib/refs.js' import { type ResolvedNotify, formatNotifyLabel, resolveNotifyIds } from './helpers.js' diff --git a/src/commands/thread/thread.test.ts b/src/commands/thread/thread.test.ts index 76aaba4..d3eff06 100644 --- a/src/commands/thread/thread.test.ts +++ b/src/commands/thread/thread.test.ts @@ -1602,7 +1602,10 @@ describe('thread read', () => { await expect( program.parseAsync(['node', 'tdc', 'thread', 'mark-read', '500', '501', '--json']), - ).rejects.toHaveProperty('code', 'MISSING_YES_FLAG') + ).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc thread mark-read, or --dry-run to preview it.', + }) expect(client.threads.markRead).not.toHaveBeenCalled() }) @@ -1738,11 +1741,31 @@ describe('thread delete', () => { await expect( program.parseAsync(['node', 'tdc', 'thread', 'delete', '500', '--json']), - ).rejects.toHaveProperty('code', 'MISSING_YES_FLAG') + ).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc thread delete, or --dry-run to preview it.', + }) + expect(client.threads.getThread).not.toHaveBeenCalled() expect(client.threads.deleteThread).not.toHaveBeenCalled() }) + it('prints a JSON preview with --dry-run --json', async () => { + const client = createClient() + apiMocks.getCommsClient.mockResolvedValue(client) + const program = createProgram() + const consoleSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'thread', 'delete', '500', '--dry-run', '--json']) + + expect(consoleSpy).toHaveBeenCalledTimes(1) + expect(JSON.parse(consoleSpy.mock.calls[0][0])).toEqual({ + status: 'preview', + command: 'tdc thread delete', + messages: ['Would delete: Test Thread'], + }) + }) + it('errors when thread creator does not match session user', async () => { const client = createClient({ sessionUser: { id: 999, fullName: 'Other User' } }) apiMocks.getCommsClient.mockResolvedValue(client) @@ -2059,9 +2082,30 @@ describe('thread done', () => { await expect( program.parseAsync(['node', 'tdc', 'thread', 'done', '500', '--json']), - ).rejects.toHaveProperty('code', 'MISSING_YES_FLAG') + ).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc thread done, or --dry-run to preview it.', + }) + + expect(client.threads.getThread).not.toHaveBeenCalled() + expect(client.inbox.archiveThread).not.toHaveBeenCalled() + }) + + it('prints a JSON preview with --dry-run --json', async () => { + const client = createClient() + apiMocks.getCommsClient.mockResolvedValue(client) + const program = createProgram() + const consoleSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'thread', 'done', '500', '--dry-run', '--json']) expect(client.inbox.archiveThread).not.toHaveBeenCalled() + expect(consoleSpy).toHaveBeenCalledTimes(1) + expect(JSON.parse(consoleSpy.mock.calls[0][0])).toEqual({ + status: 'preview', + command: 'tdc thread done', + messages: ['Would archive: Test Thread (500)'], + }) }) it('shows dry run output', async () => { @@ -2173,11 +2217,31 @@ describe('thread undone', () => { await expect( program.parseAsync(['node', 'tdc', 'thread', 'undone', '500', '--json']), - ).rejects.toHaveProperty('code', 'MISSING_YES_FLAG') + ).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc thread undone, or --dry-run to preview it.', + }) + expect(client.threads.getThread).not.toHaveBeenCalled() expect(client.inbox.unarchiveThread).not.toHaveBeenCalled() }) + it('prints a JSON preview with --dry-run --json', async () => { + const client = createClient() + apiMocks.getCommsClient.mockResolvedValue(client) + const program = createProgram() + const consoleSpy = captureConsole('log') + + await program.parseAsync(['node', 'tdc', 'thread', 'undone', '500', '--dry-run', '--json']) + + expect(consoleSpy).toHaveBeenCalledTimes(1) + expect(JSON.parse(consoleSpy.mock.calls[0][0])).toEqual({ + status: 'preview', + command: 'tdc thread undone', + messages: ['Would unarchive: Test Thread (500)'], + }) + }) + it('shows dry run output for an archived thread without a status line', async () => { const client = createClient({ thread: { ...createThreadFixture(500), isArchived: true } }) apiMocks.getCommsClient.mockResolvedValue(client) @@ -2503,7 +2567,10 @@ describe('thread mark-unread', () => { await expect( program.parseAsync(['node', 'tdc', 'thread', 'mark-unread', '500', '501', '--json']), - ).rejects.toHaveProperty('code', 'MISSING_YES_FLAG') + ).rejects.toMatchObject({ + code: 'CONFIRMATION_REQUIRED', + message: 'Pass --yes to execute tdc thread mark-unread, or --dry-run to preview it.', + }) expect(client.threads.markUnread).not.toHaveBeenCalled() }) diff --git a/src/commands/thread/update.ts b/src/commands/thread/update.ts index 13d2eb4..beca7f4 100644 --- a/src/commands/thread/update.ts +++ b/src/commands/thread/update.ts @@ -1,8 +1,9 @@ +import { printDryRun } from '@todoist/cli-core' import { getCommsClient } from '../../lib/api.js' import { CliError } from '../../lib/errors.js' import { openEditor, readContent } from '../../lib/input.js' import type { MutationOptions, StdinOptions } from '../../lib/options.js' -import { formatJson, printDryRun } from '../../lib/output.js' +import { formatJson } from '../../lib/output.js' import { assertChannelIsPublic } from '../../lib/public-channels.js' import { resolveThreadId } from '../../lib/refs.js' diff --git a/src/lib/errors.ts b/src/lib/errors.ts index e880d6e..22ec7fa 100644 --- a/src/lib/errors.ts +++ b/src/lib/errors.ts @@ -37,7 +37,6 @@ export type ErrorCode = | 'INVALID_URL' | 'INVALID_VALUE' | 'MISSING_CONTENT' - | 'MISSING_YES_FLAG' | 'UNKNOWN_KEY' | 'INVALID_NAME' | 'MISSING_USERS' diff --git a/src/lib/output.test.ts b/src/lib/output.test.ts index 2c8ba8a..6b07009 100644 --- a/src/lib/output.test.ts +++ b/src/lib/output.test.ts @@ -1,4 +1,3 @@ -import { captureConsole } from '@todoist/cli-core/testing' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { BaseCliError } from './errors.js' import { isAccessible, resetGlobalArgs } from './global-args.js' @@ -9,7 +8,6 @@ import { formatJson, formatNdjson, formatPaginatedJson, - printDryRun, } from './output.js' vi.mock('chalk') @@ -51,56 +49,6 @@ describe('isAccessible', () => { }) }) -describe('printDryRun', () => { - it('prints header, details, and footer', () => { - const logSpy = captureConsole('log') - - printDryRun('delete thread', { Thread: 'My thread (500)' }) - - expect(logSpy).toHaveBeenNthCalledWith(1, '[dry-run] Would delete thread:') - expect(logSpy).toHaveBeenNthCalledWith(2, ' Thread: My thread (500)') - expect(logSpy).toHaveBeenNthCalledWith(3, 'Run without --dry-run to execute.') - }) - - it('skips undefined values', () => { - const logSpy = captureConsole('log') - - printDryRun('mute thread', { - Thread: 'My thread (500)', - Notes: undefined, - Duration: '60 minutes', - }) - - const calls = logSpy.mock.calls.map((c) => c[0]) - expect(calls).toContain(' Thread: My thread (500)') - expect(calls).toContain(' Duration: 60 minutes') - expect(calls.some((line) => String(line).includes('Notes'))).toBe(false) - }) - - it('indents continuation lines for multiline values', () => { - const logSpy = captureConsole('log') - - printDryRun('update thread', { - Content: 'First line\nSecond line\nThird line', - }) - - expect(logSpy).toHaveBeenNthCalledWith(1, '[dry-run] Would update thread:') - expect(logSpy).toHaveBeenNthCalledWith(2, ' Content: First line') - expect(logSpy).toHaveBeenNthCalledWith(3, ' Second line') - expect(logSpy).toHaveBeenNthCalledWith(4, ' Third line') - expect(logSpy).toHaveBeenNthCalledWith(5, 'Run without --dry-run to execute.') - }) - - it('works without details', () => { - const logSpy = captureConsole('log') - - printDryRun('clear away status') - - expect(logSpy).toHaveBeenCalledWith('[dry-run] Would clear away status:') - expect(logSpy).toHaveBeenCalledWith('Run without --dry-run to execute.') - }) -}) - describe('formatError with BaseCliError', () => { it('formats a cli-core CliError instance (code, message, hints)', () => { const err = new BaseCliError('FILE_READ_ERROR', 'Could not read changelog file', { diff --git a/src/lib/output.ts b/src/lib/output.ts index 630046b..2f61093 100644 --- a/src/lib/output.ts +++ b/src/lib/output.ts @@ -279,20 +279,3 @@ export function printNdjson(items: T[], type?: EntityType, ful export function pluralize(count: number, singular: string): string { return count === 1 ? singular : `${singular}s` } - -export function printDryRun( - action: string, - details: Record = {}, -): void { - console.log(chalk.yellow(`[dry-run] Would ${action}:`)) - for (const [key, value] of Object.entries(details)) { - if (value !== undefined) { - const [firstLine, ...rest] = value.split('\n') - console.log(` ${key}: ${firstLine}`) - for (const line of rest) { - console.log(` ${line}`) - } - } - } - console.log(chalk.dim('Run without --dry-run to execute.')) -} diff --git a/src/lib/skills/content.ts b/src/lib/skills/content.ts index 20c2bb5..235d21a 100644 --- a/src/lib/skills/content.ts +++ b/src/lib/skills/content.ts @@ -478,6 +478,8 @@ Mutating commands accept \`--dry-run\` to preview the operation without making t Run without --dry-run to execute. \`\`\` +Destructive commands (\`channel delete\`, \`comment delete\`, \`msg delete\`, \`groups delete\`, \`thread delete\`, \`thread done\`/\`undone\`, \`conversation done\`/\`undone\`, and bulk \`thread mark-read\`/\`mark-unread\`) only preview without \`--yes\`: they print \`Would …\` and \`Use --yes to confirm.\` and change nothing. Under \`--json\`, a missing \`--yes\` (without \`--dry-run\`) fails with \`CONFIRMATION_REQUIRED\` ("Pass --yes to execute , or --dry-run to preview it.") before any API call, so a script never reads a preview as the result. \`--dry-run --json\` prints one \`{ "status": "preview", "command", "messages" }\` object instead of the text preview; bulk \`thread mark-read\`/\`mark-unread\` keep their per-thread rows with \`dryRun: true\`. + ## Reference System Commands accept flexible references: