Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CODEBASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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.

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions skills/comms-cli/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command>, 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:
Expand Down
3 changes: 2 additions & 1 deletion src/commands/channel/archive.ts
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
34 changes: 28 additions & 6 deletions src/commands/channel/channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Comment thread
scottlovegrove marked this conversation as resolved.
Resolved
'Would delete: Engineering (id:CH500)',
'Use --yes to confirm.',
])
})

it('deletes when --yes is passed', async () => {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/commands/channel/create.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
32 changes: 15 additions & 17 deletions src/commands/channel/delete.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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
}

Expand Down
3 changes: 2 additions & 1 deletion src/commands/channel/membership-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { printDryRun } from '@todoist/cli-core'
import type { Channel, Group, VisibleWorkspaceUser } from '@todoist/comms-sdk'
import {
addUsersToChannel,
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/commands/channel/set.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { printDryRun } from '@todoist/cli-core'
import type { User } from '@todoist/comms-sdk'
import {
addUsersToChannel,
Expand All @@ -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'

Expand Down
3 changes: 2 additions & 1 deletion src/commands/channel/update.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
23 changes: 22 additions & 1 deletion src/commands/comment/comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
34 changes: 14 additions & 20 deletions src/commands/comment/delete.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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([
Expand 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
}

Expand Down
3 changes: 2 additions & 1 deletion src/commands/comment/pin.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
3 changes: 2 additions & 1 deletion src/commands/comment/update.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 14 additions & 18 deletions src/commands/conversation/archive.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -10,30 +10,26 @@ async function setConversationArchiveState(
archive: boolean,
): Promise<void> {
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
}

Expand Down
Loading