Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/commands/view.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Command } from 'commander'
import { configureCommandOutput } from '../lib/command-output.js'
import { CliError } from '../lib/errors.js'
import { classifyCommsUrl } from '../lib/refs.js'

Expand All @@ -17,8 +18,7 @@ async function runRoutedCommand(
loadRegister: () => Promise<(p: Command) => void>,
argv: string[],
): Promise<void> {
const proxy = new Command()
proxy.exitOverride()
const proxy = configureCommandOutput(new Command())
const register = await loadRegister()
register(proxy)
await proxy.parseAsync(['node', 'tdc', ...argv])
Expand Down
92 changes: 92 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import type { Command } from 'commander'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

describe('CLI entrypoint', () => {
const originalArgv = [...process.argv]
const originalExitCode = process.exitCode

async function expectSpinnerStoppedBeforeParseError(argv: string[]): Promise<void> {
const startEarlySpinner = vi.fn()
const stopEarlySpinner = vi.fn()
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(((
code?: string | number | null,
) => {
throw new Error(`process.exit(${String(code)}) should not be called`)
}) as typeof process.exit)
const stderrWriteSpy = vi
.spyOn(process.stderr, 'write')
.mockImplementation((() => true) as typeof process.stderr.write)

vi.doMock('commander', async (importOriginal) => {
const actual = await importOriginal<typeof import('commander')>()
return {
...actual,
program: new actual.Command(),
}
})
vi.doMock('./lib/spinner.js', () => ({
startEarlySpinner,
stopEarlySpinner,
}))
vi.doMock('./lib/markdown.js', () => ({
preloadMarkdown: vi.fn().mockResolvedValue(undefined),
}))
vi.doMock('./commands/conversation/index.js', () => ({
registerConversationCommand: (program: Command) => {
program
.command('conversation')
.command('view [conversation-ref]', { isDefault: true })
.action(() => undefined)
},
}))

process.argv = argv

await import('./index.js')

expect(startEarlySpinner).toHaveBeenCalledTimes(1)
expect(stopEarlySpinner).toHaveBeenCalled()
expect(exitSpy).not.toHaveBeenCalled()
expect(process.exitCode).toBe(1)
expect(stderrWriteSpy).toHaveBeenCalledWith(expect.stringContaining('--from'))
expect(stopEarlySpinner.mock.invocationCallOrder[0]).toBeLessThan(
stderrWriteSpy.mock.invocationCallOrder[0],
)
}

beforeEach(() => {
vi.resetModules()
process.argv = [...originalArgv]
process.exitCode = undefined
})

afterEach(() => {
process.argv = [...originalArgv]
process.exitCode = originalExitCode
vi.restoreAllMocks()
vi.resetModules()
})

it('stops the early spinner before the root parser writes parse errors', async () => {
await expectSpinnerStoppedBeforeParseError([
'node',
'tdc',
'conversation',
'view',
'https://comms.todoist.com/123/msg/ANON_MESSAGE_ID/',
'--from',
'2026-06-26',
])
})

it('stops the early spinner before the view proxy parser writes parse errors', async () => {
await expectSpinnerStoppedBeforeParseError([
'node',
'tdc',
'view',
'https://comms.todoist.com/a/123/msg/456',
'--from',
'2026-06-26',
])
})
})
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env node

import { stripUserFlag } from '@doist/cli-core'
import { type Command, program } from 'commander'
import { CommanderError, type Command, program } from 'commander'
import pkg from '../package.json' with { type: 'json' }
import { configureCommandOutput } from './lib/command-output.js'
import { BaseCliError } from './lib/errors.js'
import { getRequestedUserRef, isJsonMode, isNdjsonMode } from './lib/global-args.js'
import { preloadMarkdown } from './lib/markdown.js'
Expand Down Expand Up @@ -107,6 +108,8 @@ Note for AI/LLM agents:
Default JSON shows essential fields; use --full for all fields.`,
)

configureCommandOutput(program)

// Register lightweight placeholders so --help lists all commands
for (const [name, [description]] of Object.entries(commands)) {
const cmd = program.command(name).description(description)
Expand Down Expand Up @@ -215,6 +218,10 @@ await program
.parseAsync()
.catch((err: Error) => {
stopEarlySpinner()
if (err instanceof CommanderError) {
process.exitCode = err.exitCode
return
}
if (err instanceof BaseCliError) {
console.error(isJsonMode() ? formatErrorJson(err) : formatError(err))
} else {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/command-output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Command } from 'commander'
import { stopEarlySpinner } from './spinner.js'

export function configureCommandOutput(command: Command): Command {
return command
.configureOutput({
writeOut: (str) => {
stopEarlySpinner()
process.stdout.write(str)
},
writeErr: (str) => {
stopEarlySpinner()
process.stderr.write(str)
},
})
.exitOverride()
}
Loading