goke 6.9.0 → 6.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -160,6 +160,39 @@ describe('error formatting', () => {
160
160
  })
161
161
  })
162
162
 
163
+ describe('anonymous action naming', () => {
164
+ test('inline anonymous function gets named after the command', () => {
165
+ const cli = gokeTestable('mycli')
166
+ const cmd = cli.command('deploy', 'Deploy app')
167
+ // Inline arrow functions passed directly to .action() have no name,
168
+ // so goke assigns one based on the command name for better stack traces.
169
+ cmd.action(() => {})
170
+ expect(cmd.commandAction!.name).toBe('command:deploy')
171
+ })
172
+
173
+ test('inline anonymous function on multi-word command gets full name', () => {
174
+ const cli = gokeTestable('mycli')
175
+ const cmd = cli.command('db migrate', 'Run migrations')
176
+ cmd.action(() => {})
177
+ expect(cmd.commandAction!.name).toBe('command:db migrate')
178
+ })
179
+
180
+ test('named function keeps its original name', () => {
181
+ const cli = gokeTestable('mycli')
182
+ const cmd = cli.command('build', 'Build app')
183
+ function myBuildAction() {}
184
+ cmd.action(myBuildAction)
185
+ expect(cmd.commandAction!.name).toBe('myBuildAction')
186
+ })
187
+
188
+ test('default command action gets "command:default" name', () => {
189
+ const cli = gokeTestable('mycli')
190
+ const cmd = cli.command('', 'Default command')
191
+ cmd.action(() => {})
192
+ expect(cmd.commandAction!.name).toBe('command:default')
193
+ })
194
+ })
195
+
163
196
  describe('injected fs', () => {
164
197
  test('command actions can use the default node fs for cli storage', async () => {
165
198
  const stdout = createTestOutputStream()
@@ -2502,3 +2535,45 @@ describe('use() with sub-CLI composition', () => {
2502
2535
  expect(matched).toBe('rollback')
2503
2536
  })
2504
2537
  })
2538
+
2539
+ describe('getAction()', () => {
2540
+ test('returns the action callable with correct behavior', () => {
2541
+ const stdout = createTestOutputStream()
2542
+ const cli = goke('mycli', { stdout, exit: () => {} })
2543
+
2544
+ const cmd = cli
2545
+ .command('deploy', 'Deploy the app')
2546
+ .option('--env <env>', z.enum(['staging', 'production']).describe('Target environment'))
2547
+ .action((options, { console }) => {
2548
+ console.log(`Deploying to ${options.env}`)
2549
+ })
2550
+
2551
+ const action = cmd.getAction()
2552
+ const ctx = cli.createExecutionContext()
2553
+ action({ env: 'staging' as const, '--': [] }, ctx)
2554
+ expect(stdout.text).toBe('Deploying to staging\n')
2555
+ })
2556
+
2557
+ test('works with positional args', () => {
2558
+ const stdout = createTestOutputStream()
2559
+ const cli = goke('mycli', { stdout, exit: () => {} })
2560
+
2561
+ const cmd = cli
2562
+ .command('get <id>', 'Fetch by id')
2563
+ .option('--format <format>', z.string().describe('Output format'))
2564
+ .action((id, options, { console }) => {
2565
+ console.log(`${id}:${options.format}`)
2566
+ })
2567
+
2568
+ const action = cmd.getAction()
2569
+ const ctx = cli.createExecutionContext()
2570
+ action('abc123', { format: 'json', '--': [] }, ctx)
2571
+ expect(stdout.text).toBe('abc123:json\n')
2572
+ })
2573
+
2574
+ test('throws when no action is registered', () => {
2575
+ const cli = goke('mycli')
2576
+ const cmd = cli.command('noop', 'No action')
2577
+ expect(() => cmd.getAction()).toThrow(/No action registered/)
2578
+ })
2579
+ })
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { describe, expect, test } from 'vitest'
6
6
  import { z } from 'zod'
7
- import goke, { openInBrowser } from '../index.js'
7
+ import goke, { openInBrowser, generateDocs } from '../index.js'
8
8
  import type { GokeOptions, GokeOutputStream } from '../index.js'
9
9
 
10
10
  const ANSI_RE = /\x1B\[[0-9;]*m/g
@@ -187,7 +187,7 @@ describe('documented command APIs', () => {
187
187
  expect(stdout.text).toBe('')
188
188
  })
189
189
 
190
- test('openInBrowser prints the URL to stdout in non-tty environments', () => {
190
+ test('openInBrowser prints the URL to stdout in non-tty environments', async () => {
191
191
  const url = 'https://example.com/dashboard'
192
192
  const originalStdoutWrite = process.stdout.write
193
193
  const originalStderrWrite = process.stderr.write
@@ -209,7 +209,7 @@ describe('documented command APIs', () => {
209
209
  }) as typeof process.stderr.write
210
210
 
211
211
  try {
212
- openInBrowser(url)
212
+ await openInBrowser(url)
213
213
  } finally {
214
214
  process.stdout.write = originalStdoutWrite
215
215
  process.stderr.write = originalStderrWrite
@@ -219,7 +219,155 @@ describe('documented command APIs', () => {
219
219
  })
220
220
  }
221
221
 
222
- expect(stdout).toBe(`${url}\n`)
223
- expect(stderr).toBe('')
222
+ expect(stdout).toBe('')
223
+ expect(stderr).toBe(`${url}\n`)
224
+ })
225
+ })
226
+
227
+ describe('generateDocs', () => {
228
+ test('generates pages for CLI with multiple commands', () => {
229
+ const cli = gokeTestable('sentry')
230
+ .version('1.0.0')
231
+ .help()
232
+
233
+ cli
234
+ .command('event view <id>', 'View details of a specific event')
235
+ .option('-w, --web', 'Open in browser')
236
+ .option('--spans <spans>', z.string().default('3').describe('Span tree depth limit'))
237
+ .example('```\nsentry event view abc123\n```')
238
+
239
+ cli
240
+ .command('event list <issue>', 'List events for an issue')
241
+ .option('-n, --limit <limit>', z.number().default(25).describe('Number of events'))
242
+ .option('-q, --query <query>', 'Search query')
243
+
244
+ cli
245
+ .command('hidden-cmd', 'Should not appear')
246
+ .hidden()
247
+
248
+ const pages = generateDocs({ cli })
249
+
250
+ expect(pages.map((p) => p.slug)).toMatchInlineSnapshot(`
251
+ [
252
+ "index",
253
+ "event-view",
254
+ "event-list",
255
+ ]
256
+ `)
257
+
258
+ // Index page
259
+ expect(pages[0].content).toMatchInlineSnapshot(`
260
+ "# sentry
261
+
262
+ Version: 1.0.0
263
+
264
+ ## Commands
265
+
266
+ | Command | Description |
267
+ |---------|-------------|
268
+ | [\`event view\`](./event-view.md) | View details of a specific event |
269
+ | [\`event list\`](./event-list.md) | List events for an issue |
270
+
271
+ ## Global Options
272
+
273
+ | Option | Default | Description |
274
+ |--------|---------|-------------|
275
+ | \`-v, --version\` | - | Display version number |
276
+ | \`-h, --help\` | - | Display this message |
277
+ "
278
+ `)
279
+
280
+ // Command page with examples
281
+ expect(pages[1].content).toMatchInlineSnapshot(`
282
+ "# event view
283
+
284
+ View details of a specific event
285
+
286
+ ## Usage
287
+
288
+ \`\`\`sh
289
+ sentry event view <id>
290
+ \`\`\`
291
+
292
+ ## Arguments
293
+
294
+ | Argument | Required | Description |
295
+ |----------|----------|-------------|
296
+ | \`<id>\` | Yes | id |
297
+
298
+ ## Options
299
+
300
+ | Option | Default | Description |
301
+ |--------|---------|-------------|
302
+ | \`-w, --web\` | - | Open in browser |
303
+ | \`--spans <spans>\` | \`3\` | Span tree depth limit |
304
+
305
+ ## Global Options
306
+
307
+ | Option | Default | Description |
308
+ |--------|---------|-------------|
309
+ | \`-v, --version\` | - | Display version number |
310
+ | \`-h, --help\` | - | Display this message |
311
+
312
+ ## Examples
313
+
314
+ \`\`\`
315
+ sentry event view abc123
316
+ \`\`\`
317
+ "
318
+ `)
319
+
320
+ // Command page without examples
321
+ expect(pages[2].content).toMatchInlineSnapshot(`
322
+ "# event list
323
+
324
+ List events for an issue
325
+
326
+ ## Usage
327
+
328
+ \`\`\`sh
329
+ sentry event list <issue>
330
+ \`\`\`
331
+
332
+ ## Arguments
333
+
334
+ | Argument | Required | Description |
335
+ |----------|----------|-------------|
336
+ | \`<issue>\` | Yes | issue |
337
+
338
+ ## Options
339
+
340
+ | Option | Default | Description |
341
+ |--------|---------|-------------|
342
+ | \`-n, --limit <limit>\` | \`25\` | Number of events |
343
+ | \`-q, --query <query>\` | - | Search query |
344
+
345
+ ## Global Options
346
+
347
+ | Option | Default | Description |
348
+ |--------|---------|-------------|
349
+ | \`-v, --version\` | - | Display version number |
350
+ | \`-h, --help\` | - | Display this message |
351
+ "
352
+ `)
353
+ })
354
+
355
+ test('handles CLI with no commands', () => {
356
+ const cli = gokeTestable('empty')
357
+ const pages = generateDocs({ cli })
358
+ expect(pages).toEqual([])
359
+ })
360
+
361
+ test('skips deprecated options', () => {
362
+ const cli = gokeTestable('mycli')
363
+ cli
364
+ .command('deploy', 'Deploy the app')
365
+ .option('--force', 'Skip confirmation')
366
+ .option('--old-flag', z.boolean().meta({ deprecated: true }).describe('Use --force instead'))
367
+
368
+ const pages = generateDocs({ cli })
369
+ const deployPage = pages.find((p) => p.slug === 'deploy')!
370
+ // The deprecated option should not appear
371
+ expect(deployPage.content).not.toContain('old-flag')
224
372
  })
225
373
  })
@@ -522,6 +522,33 @@ describe('type-level: README TypeScript examples', () => {
522
522
  })
523
523
  })
524
524
 
525
+ test('getAction() returns correctly typed function', () => {
526
+ const cmd = goke('test')
527
+ .command('convert <input> <output>', 'Convert file format')
528
+ .option('--quality <quality>', z.number())
529
+ .option('--format <format>', z.enum(['png', 'jpg']))
530
+ .action((input, output, options, ctx) => {
531
+ void input; void output; void options; void ctx
532
+ })
533
+
534
+ const action = cmd.getAction()
535
+ expectTypeOf(action).parameter(0).toEqualTypeOf<string>() // input
536
+ expectTypeOf(action).parameter(1).toEqualTypeOf<string>() // output
537
+ })
538
+
539
+ test('getAction() with no positional args has (options, ctx) signature', () => {
540
+ const cmd = goke('test')
541
+ .command('deploy', 'Deploy')
542
+ .option('--env <env>', z.enum(['staging', 'production']))
543
+ .action((options, ctx) => {
544
+ void options; void ctx
545
+ })
546
+
547
+ const action = cmd.getAction()
548
+ // First param is options with env
549
+ expectTypeOf(action).parameter(0).toMatchTypeOf<{ env: 'staging' | 'production' }>()
550
+ })
551
+
525
552
  test('README global options and middleware example stays typed end-to-end', () => {
526
553
  // `z.boolean().default(false)` and `z.string().default(...)` are
527
554
  // effectively required at runtime: the default applies when the flag is
package/src/agents.ts ADDED
@@ -0,0 +1,101 @@
1
+ /**
2
+ * AI coding agent detection for goke CLIs.
3
+ *
4
+ * Detects whether the current process is running inside an AI coding agent
5
+ * (Claude, Cursor, Codex, Gemini, etc.) by checking environment variables.
6
+ * Ported from unjs/std-env with the same detection logic.
7
+ *
8
+ * CLI authors can use this to adjust behavior: skip interactive prompts,
9
+ * prefer structured output, avoid browser opens, etc.
10
+ */
11
+
12
+ const env: Record<string, string | undefined> =
13
+ globalThis.process?.env || Object.create(null)
14
+
15
+ /**
16
+ * Known AI coding agent names.
17
+ */
18
+ export type AgentName =
19
+ | (string & {})
20
+ | 'cursor'
21
+ | 'claude'
22
+ | 'devin'
23
+ | 'replit'
24
+ | 'gemini'
25
+ | 'codex'
26
+ | 'auggie'
27
+ | 'opencode'
28
+ | 'kiro'
29
+ | 'goose'
30
+ | 'pi'
31
+
32
+ type EnvCheck = string | (() => boolean)
33
+
34
+ type InternalAgent = [agentName: AgentName, envChecks: EnvCheck[]]
35
+
36
+ function envMatcher(envKey: string, regex: RegExp) {
37
+ return () => {
38
+ const value = env[envKey]
39
+ return value ? regex.test(value) : false
40
+ }
41
+ }
42
+
43
+ // Detection order matters: specific agents first, IDE-based agents last
44
+ // so that agents running inside those IDEs are detected by their own env vars first.
45
+ const agents: InternalAgent[] = [
46
+ // CLI agents
47
+ ['claude', ['CLAUDECODE', 'CLAUDE_CODE']],
48
+ ['replit', ['REPL_ID']],
49
+ ['gemini', ['GEMINI_CLI']],
50
+ ['codex', ['CODEX_SANDBOX', 'CODEX_THREAD_ID']],
51
+ ['opencode', ['OPENCODE']],
52
+ ['pi', [envMatcher('PATH', /\.pi[\\/]agent/)]],
53
+ ['auggie', ['AUGMENT_AGENT']],
54
+ ['goose', ['GOOSE_PROVIDER']],
55
+
56
+ // IDE-based agents (checked last)
57
+ ['devin', [envMatcher('EDITOR', /devin/)]],
58
+ ['cursor', ['CURSOR_AGENT']],
59
+ ['kiro', [envMatcher('TERM_PROGRAM', /kiro/)]],
60
+ ]
61
+
62
+ /**
63
+ * Information about the detected AI coding agent.
64
+ */
65
+ export type AgentInfo = {
66
+ /** The name of the detected agent, or undefined if no agent was detected. */
67
+ name?: AgentName
68
+ }
69
+
70
+ /**
71
+ * Detect the current AI coding agent from environment variables.
72
+ *
73
+ * Checks `AI_AGENT` env var first (explicit override), then scans for
74
+ * known agent-specific env vars in priority order.
75
+ *
76
+ * Supported agents: `cursor`, `claude`, `devin`, `replit`, `gemini`,
77
+ * `codex`, `auggie`, `opencode`, `kiro`, `goose`, `pi`
78
+ */
79
+ export function detectAgent(): AgentInfo {
80
+ const aiAgent = env.AI_AGENT
81
+ if (aiAgent) {
82
+ return { name: aiAgent.toLowerCase() }
83
+ }
84
+ for (const [name, checks] of agents) {
85
+ for (const check of checks) {
86
+ if (typeof check === 'string' ? env[check] : check()) {
87
+ return { name }
88
+ }
89
+ }
90
+ }
91
+ return {}
92
+ }
93
+
94
+ /** Detected agent info, evaluated once at import time. */
95
+ export const agentInfo: AgentInfo = /* #__PURE__ */ detectAgent()
96
+
97
+ /** Name of the detected agent, or undefined if not running inside one. */
98
+ export const agent: AgentName | undefined = agentInfo.name
99
+
100
+ /** Whether the current process is running inside an AI coding agent. */
101
+ export const isAgent: boolean = !!agentInfo.name