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: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,11 @@ Agents calling the CLI as a subprocess automatically get JSON output (non-TTY de
- **Output:** Success JSON on stdout; error JSON on stderr (use `2>` or combined capture if you need both)
- **Exit code:** `0` success, `1` error
- **Errors:** Always include `message` and `code` fields
- **Discovery:** `resend commands` prints the full command tree as JSON (subcommands, options, descriptions).

### `resend commands`

Prints the CLI command tree as JSON for scripting and AI agents. In an interactive terminal, pass global `--json` if you need machine output; when stdout is piped, JSON is used automatically.

---

Expand Down
2 changes: 1 addition & 1 deletion skills/resend-cli/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Auth resolves: `--api-key` flag > `RESEND_API_KEY` env > config file (`resend lo
| `topics` | create, update, delete, list |
| `webhooks` | create, update, listen, delete, list |
| `auth` | login, logout, switch, rename, remove |
| `whoami` / `doctor` / `update` / `open` | Utility commands |
| `whoami` / `doctor` / `update` / `open` / `commands` | Utility commands |

Read the matching reference file for detailed flags and output shapes.

Expand Down
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { authCommand } from './commands/auth/index';
import { loginCommand } from './commands/auth/login';
import { logoutCommand } from './commands/auth/logout';
import { broadcastsCommand } from './commands/broadcasts/index';
import { listCommandsCommand } from './commands/commands';
import { completionCommand } from './commands/completion';
import { contactPropertiesCommand } from './commands/contact-properties/index';
import { contactsCommand } from './commands/contacts/index';
Expand Down Expand Up @@ -145,6 +146,7 @@ ${pc.gray('Examples:')}
.addCommand(docsCommand)
.addCommand(updateCommand)
.addCommand(teamsDeprecatedCommand)
.addCommand(listCommandsCommand)
.addCommand(completionCommand);

const telemetryCommand = new Command('telemetry')
Expand Down
29 changes: 29 additions & 0 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Command } from '@commander-js/extra-typings';
import type { GlobalOpts } from '../lib/client';
import { collectCommandTree } from '../lib/completion';
import { buildHelpText } from '../lib/help-text';
import { outputResult } from '../lib/output';

export const listCommandsCommand = new Command('commands')
.description('Print the full command tree as JSON (for agents and tooling)')
.addHelpText(
'after',
buildHelpText({
context: `Outputs every subcommand, option, and description from the CLI definition.
In machine mode (piped, CI, or --json), the tree is JSON you can feed to agents or scripts.`,
examples: [
'resend commands',
'resend commands --json',
'resend commands | jq ".subcommands[].name"',
],
}),
)
.action((_opts, cmd) => {
const root = cmd.parent;
if (!root) {
throw new Error('commands must be registered on the root program');
}
const globalOpts = cmd.optsWithGlobals() as GlobalOpts;
const tree = collectCommandTree(root as Command);
outputResult(tree, { json: globalOpts.json });
});
31 changes: 31 additions & 0 deletions tests/commands/commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Command } from '@commander-js/extra-typings';
import { afterEach, describe, expect, test } from 'vitest';
import { listCommandsCommand } from '../../src/commands/commands';
import { captureTestEnv, setupOutputSpies } from '../helpers';

describe('commands', () => {
const restoreEnv = captureTestEnv();

afterEach(() => {
restoreEnv();
});

test('prints JSON tree with root name and subcommands', async () => {
const spies = setupOutputSpies();
const program = new Command('resend')
.description('Resend CLI')
.option('--json', 'JSON output')
.addCommand(new Command('emails').description('Emails'))
.addCommand(new Command('domains').description('Domains'))
.addCommand(listCommandsCommand);

await program.parseAsync(['commands'], { from: 'user' });

const tree = JSON.parse(spies.logSpy.mock.calls[0][0] as string);
expect(tree.name).toBe('resend');
expect(Array.isArray(tree.subcommands)).toBe(true);
const names = tree.subcommands.map((s: { name: string }) => s.name);
expect(names).toContain('emails');
expect(names).toContain('domains');
});
});