goke 6.5.1 → 6.6.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.
- package/README.md +39 -9
- package/dist/__test__/index.test.js +16 -6
- package/dist/__test__/readme-examples.test.d.ts +5 -0
- package/dist/__test__/readme-examples.test.d.ts.map +1 -0
- package/dist/__test__/readme-examples.test.js +169 -0
- package/dist/__test__/types.test-d.js +242 -1
- package/dist/goke.d.ts +122 -17
- package/dist/goke.d.ts.map +1 -1
- package/dist/goke.js +35 -5
- package/dist/runtime-node.js +2 -2
- package/package.json +2 -2
- package/src/__test__/index.test.ts +16 -6
- package/src/__test__/readme-examples.test.ts +225 -0
- package/src/__test__/types.test-d.ts +271 -1
- package/src/goke.ts +186 -20
- package/src/runtime-node.ts +2 -2
package/README.md
CHANGED
|
@@ -2,22 +2,52 @@
|
|
|
2
2
|
<br/>
|
|
3
3
|
<br/>
|
|
4
4
|
<h3>goke</h3>
|
|
5
|
-
<p>
|
|
5
|
+
<p>Build CLIs like you'd build an API. Type-safe, chainable, zero dependencies.</p>
|
|
6
6
|
<br/>
|
|
7
7
|
<br/>
|
|
8
8
|
</div>
|
|
9
9
|
|
|
10
|
+
goke is a TypeScript CLI framework with a [Hono](https://hono.dev)-like API. You chain `.use()` for middleware and `.command()` for routes — the same mental model as a REST API, applied to the terminal.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { goke } from 'goke'
|
|
14
|
+
import { z } from 'zod'
|
|
15
|
+
|
|
16
|
+
const cli = goke('deploy')
|
|
17
|
+
|
|
18
|
+
// middleware — runs before every command
|
|
19
|
+
cli
|
|
20
|
+
.option('--env <env>', z.enum(['staging', 'production']).default('staging').describe('Target environment'))
|
|
21
|
+
.use((options, { console }) => {
|
|
22
|
+
console.log(`Environment: ${options.env}`)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
// commands — like route handlers
|
|
26
|
+
cli
|
|
27
|
+
.command('up', 'Deploy the app')
|
|
28
|
+
.option('--dry-run', 'Preview without deploying')
|
|
29
|
+
.action((options, { console, process }) => {
|
|
30
|
+
console.log(`Deploying from ${process.cwd}`)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
cli
|
|
34
|
+
.command('logs <deploymentId>', 'Stream logs')
|
|
35
|
+
.option('--lines <n>', z.number().default(100).describe('Lines to tail'))
|
|
36
|
+
.action((id, options) => streamLogs(id, options.lines))
|
|
37
|
+
|
|
38
|
+
cli.help()
|
|
39
|
+
cli.parse()
|
|
40
|
+
```
|
|
10
41
|
|
|
11
42
|
## Features
|
|
12
43
|
|
|
13
|
-
- **
|
|
14
|
-
- **
|
|
15
|
-
- **
|
|
16
|
-
- **
|
|
17
|
-
- **
|
|
18
|
-
- **Injected
|
|
19
|
-
- **
|
|
20
|
-
- **Developer friendly**. Written in TypeScript.
|
|
44
|
+
- **Hono-like chaining** — `.use()` for middleware, `.command()` for handlers. Build a CLI the same way you'd design a REST API.
|
|
45
|
+
- **Zod type safety** — pass a Zod schema to `.option()` and get automatic coercion, TypeScript inference, and help text for free. Works with Valibot, ArkType, or any Standard Schema library.
|
|
46
|
+
- **MCP server in 2 lines** — expose your entire CLI as an MCP server with `createMcpAction({ cli })`. Every command becomes a tool, ready for Claude Desktop, Cursor, VS Code, and [any MCP client](https://github.com/supermemoryai/install-mcp#supported-clients).
|
|
47
|
+
- **JustBash support** — `cli.createJustBashCommand()` exposes your CLI as a sandboxed JustBash command. Same action code, no changes needed.
|
|
48
|
+
- **Space-separated subcommands** — `git remote add`, `mcp login`, `db migrate` — multi-word commands work out of the box.
|
|
49
|
+
- **Injected `{ fs, console, process }`** — commands receive a portable runtime context. Swap it in tests, or let JustBash replace it with a sandbox. No global side effects.
|
|
50
|
+
- **Zero runtime dependencies** — install `goke` without pulling extra runtime packages into your CLI.
|
|
21
51
|
|
|
22
52
|
## Install
|
|
23
53
|
|
|
@@ -230,7 +230,9 @@ test('dot-nested options', () => {
|
|
|
230
230
|
.option('--scale [level]', 'Scaling level');
|
|
231
231
|
const { options: options1 } = cli.parse(`node bin --externals.env.prod production --scale`.split(' '));
|
|
232
232
|
expect(options1.externals).toEqual({ env: { prod: 'production' } });
|
|
233
|
-
|
|
233
|
+
// Bare `--scale` normalizes to `''` (new uniform string-or-undefined shape
|
|
234
|
+
// for untyped optional-value flags).
|
|
235
|
+
expect(options1.scale).toEqual('');
|
|
234
236
|
});
|
|
235
237
|
describe('schema-based options', () => {
|
|
236
238
|
test('schema coerces string to number', () => {
|
|
@@ -334,11 +336,14 @@ describe('no-schema behavior (mri no longer auto-converts)', () => {
|
|
|
334
336
|
const { options } = cli.parse('node bin --verbose'.split(' '));
|
|
335
337
|
expect(options.verbose).toBe(true);
|
|
336
338
|
});
|
|
337
|
-
test('optional value flag returns
|
|
339
|
+
test('optional value flag returns empty string when no value given', () => {
|
|
340
|
+
// Bare `--format` is normalized from the mri `true` sentinel to `''` so
|
|
341
|
+
// callers see a uniform `string | undefined` shape. `''` still lets them
|
|
342
|
+
// distinguish "flag present but no value" from "flag omitted entirely".
|
|
338
343
|
const cli = goke();
|
|
339
344
|
cli.option('--format [fmt]', 'Format');
|
|
340
345
|
const { options } = cli.parse('node bin --format'.split(' '));
|
|
341
|
-
expect(options.format).toBe(
|
|
346
|
+
expect(options.format).toBe('');
|
|
342
347
|
});
|
|
343
348
|
test('optional value flag returns string when value given', () => {
|
|
344
349
|
const cli = goke();
|
|
@@ -545,12 +550,17 @@ describe('regression: oracle-found issues', () => {
|
|
|
545
550
|
const { options } = cli.parse('node bin --count'.split(' '));
|
|
546
551
|
expect(options.count).toBe(undefined);
|
|
547
552
|
});
|
|
548
|
-
test('optional value option without schema
|
|
553
|
+
test('optional value option without schema normalizes bare flag to empty string', () => {
|
|
549
554
|
const cli = goke();
|
|
550
555
|
cli.option('--count [count]', 'Count');
|
|
551
|
-
//
|
|
556
|
+
// Untyped optional-value flags uniformly expose `string | undefined`:
|
|
557
|
+
// - `--count` → '' (flag present, no value)
|
|
558
|
+
// - `--count 42` → '42' (flag present, with value)
|
|
559
|
+
// - (omitted) → undefined (flag absent)
|
|
560
|
+
// This lets callers use a single `typeof options.count === 'string'`
|
|
561
|
+
// check and distinguish the three cases via `=== ''` if they need to.
|
|
552
562
|
const { options } = cli.parse('node bin --count'.split(' '));
|
|
553
|
-
expect(options.count).toBe(
|
|
563
|
+
expect(options.count).toBe('');
|
|
554
564
|
});
|
|
555
565
|
test('optional value option with schema coerces when value given', () => {
|
|
556
566
|
const cli = goke();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"readme-examples.test.d.ts","sourceRoot":"","sources":["../../src/__test__/readme-examples.test.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Smoke tests that keep README examples and documented APIs executable.
|
|
3
|
+
*/
|
|
4
|
+
import { describe, expect, test } from 'vitest';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import goke, { openInBrowser } from '../index.js';
|
|
7
|
+
const ANSI_RE = /\x1B\[[0-9;]*m/g;
|
|
8
|
+
const stripAnsi = (text) => text.replace(ANSI_RE, '');
|
|
9
|
+
function createTestOutputStream() {
|
|
10
|
+
const lines = [];
|
|
11
|
+
return {
|
|
12
|
+
lines,
|
|
13
|
+
get text() { return stripAnsi(lines.join('')); },
|
|
14
|
+
write(data) { lines.push(data); },
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function gokeTestable(name = '', options) {
|
|
18
|
+
return goke(name, {
|
|
19
|
+
...options,
|
|
20
|
+
exit: () => { },
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
describe('README smoke tests', () => {
|
|
24
|
+
test('intro example runs middleware and both command forms', async () => {
|
|
25
|
+
const stdout = createTestOutputStream();
|
|
26
|
+
const cli = gokeTestable('deploy', { stdout });
|
|
27
|
+
cli
|
|
28
|
+
.option('--env <env>', z.enum(['staging', 'production']).default('staging').describe('Target environment'))
|
|
29
|
+
.use((options, { console }) => {
|
|
30
|
+
console.log(`Environment: ${options.env}`);
|
|
31
|
+
});
|
|
32
|
+
cli
|
|
33
|
+
.command('up', 'Deploy the app')
|
|
34
|
+
.option('--dry-run', 'Preview without deploying')
|
|
35
|
+
.action((options, { console, process }) => {
|
|
36
|
+
console.log(`Deploying from ${process.cwd} dryRun=${String(options.dryRun)}`);
|
|
37
|
+
});
|
|
38
|
+
cli
|
|
39
|
+
.command('logs <deploymentId>', 'Stream logs')
|
|
40
|
+
.option('--lines <n>', z.number().default(100).describe('Lines to tail'))
|
|
41
|
+
.action((deploymentId, options, { console }) => {
|
|
42
|
+
console.log(`logs ${deploymentId} ${options.lines}`);
|
|
43
|
+
});
|
|
44
|
+
cli.parse(['node', 'bin', '--env', 'production', 'up', '--dry-run'], { run: false });
|
|
45
|
+
await cli.runMatchedCommand();
|
|
46
|
+
expect(stdout.text).toBe(`Environment: production\nDeploying from ${process.cwd()} dryRun=true\n`);
|
|
47
|
+
stdout.lines.length = 0;
|
|
48
|
+
cli.parse(['node', 'bin', 'logs', 'dep_123'], { run: false });
|
|
49
|
+
await cli.runMatchedCommand();
|
|
50
|
+
expect(stdout.text).toBe('Environment: staging\nlogs dep_123 100\n');
|
|
51
|
+
});
|
|
52
|
+
test('simple parsing example stays executable and keeps examples in help output', async () => {
|
|
53
|
+
const stdout = createTestOutputStream();
|
|
54
|
+
const cli = gokeTestable('mycli', { stdout });
|
|
55
|
+
cli.option('--type [type]', z.string().default('node').describe('Choose a project type'));
|
|
56
|
+
cli.option('--name <name>', 'Provide your name');
|
|
57
|
+
cli.command('lint [...files]', 'Lint files').action((files, options, { console, process }) => {
|
|
58
|
+
console.log(JSON.stringify({ files, options, cwd: process.cwd }));
|
|
59
|
+
});
|
|
60
|
+
cli
|
|
61
|
+
.command('build [entry]', 'Build your app')
|
|
62
|
+
.option('--minify', 'Minify output')
|
|
63
|
+
.example('build src/index.ts')
|
|
64
|
+
.example('build src/index.ts --minify')
|
|
65
|
+
.action(async (entry, options, { console, process }) => {
|
|
66
|
+
console.log(JSON.stringify({ entry, options, nodeEnv: process.env.NODE_ENV }));
|
|
67
|
+
});
|
|
68
|
+
cli.example((bin) => `${bin} lint src/**/*.ts`);
|
|
69
|
+
cli.help();
|
|
70
|
+
cli.version('0.0.0');
|
|
71
|
+
expect(stripAnsi(cli.helpText())).toContain('mycli lint src/**/*.ts');
|
|
72
|
+
cli.parse(['node', 'bin', '--type', 'bun', '--name', 'Tommy', 'build', 'src/index.ts', '--minify'], { run: false });
|
|
73
|
+
await cli.runMatchedCommand();
|
|
74
|
+
expect(stdout.text).toBe(`${JSON.stringify({
|
|
75
|
+
entry: 'src/index.ts',
|
|
76
|
+
options: {
|
|
77
|
+
'--': [],
|
|
78
|
+
type: 'bun',
|
|
79
|
+
name: 'Tommy',
|
|
80
|
+
minify: true,
|
|
81
|
+
},
|
|
82
|
+
nodeEnv: process.env.NODE_ENV,
|
|
83
|
+
})}\n`);
|
|
84
|
+
});
|
|
85
|
+
test('many-commands README example runs root and nested commands', async () => {
|
|
86
|
+
const stdout = createTestOutputStream();
|
|
87
|
+
const cli = gokeTestable('deploy', { stdout });
|
|
88
|
+
cli
|
|
89
|
+
.command('', 'Deploy the current project')
|
|
90
|
+
.option('--env <env>', z.string().default('production').describe('Target environment'))
|
|
91
|
+
.option('--dry-run', 'Preview without deploying')
|
|
92
|
+
.action((options, { console, process }) => {
|
|
93
|
+
console.log(`Deploying to ${options.env} from ${process.cwd} dryRun=${String(options.dryRun)}`);
|
|
94
|
+
});
|
|
95
|
+
cli
|
|
96
|
+
.command('logs <deploymentId>', 'Stream logs for a deployment')
|
|
97
|
+
.option('--follow', 'Follow log output')
|
|
98
|
+
.option('--lines <n>', z.number().default(100).describe('Number of lines'))
|
|
99
|
+
.action((deploymentId, options, { console, process }) => {
|
|
100
|
+
console.log(`Streaming logs for ${deploymentId} from ${process.cwd} follow=${String(options.follow)} lines=${options.lines}`);
|
|
101
|
+
});
|
|
102
|
+
cli.parse(['node', 'bin', '--env', 'staging', '--dry-run'], { run: false });
|
|
103
|
+
await cli.runMatchedCommand();
|
|
104
|
+
expect(stdout.text).toBe(`Deploying to staging from ${process.cwd()} dryRun=true\n`);
|
|
105
|
+
stdout.lines.length = 0;
|
|
106
|
+
cli.parse(['node', 'bin', 'logs', 'abc123', '--follow'], { run: false });
|
|
107
|
+
await cli.runMatchedCommand();
|
|
108
|
+
expect(stdout.text).toBe(`Streaming logs for abc123 from ${process.cwd()} follow=true lines=100\n`);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
describe('documented command APIs', () => {
|
|
112
|
+
test('alias runs the same command through a short name', () => {
|
|
113
|
+
const cli = gokeTestable('mycli');
|
|
114
|
+
let seen = '';
|
|
115
|
+
cli.command('install', 'Install packages').alias('i').action(() => {
|
|
116
|
+
seen = 'install';
|
|
117
|
+
});
|
|
118
|
+
cli.parse(['node', 'bin', 'i'], { run: true });
|
|
119
|
+
expect(seen).toBe('install');
|
|
120
|
+
});
|
|
121
|
+
test('command helpText returns command-specific help without printing', () => {
|
|
122
|
+
const stdout = createTestOutputStream();
|
|
123
|
+
const cli = goke('mycli', { stdout });
|
|
124
|
+
const command = cli
|
|
125
|
+
.command('deploy <env>', 'Deploy to an environment')
|
|
126
|
+
.option('--dry-run', 'Preview without deploying')
|
|
127
|
+
.example('# Deploy safely first')
|
|
128
|
+
.example('mycli deploy staging --dry-run');
|
|
129
|
+
cli.help();
|
|
130
|
+
const help = stripAnsi(command.helpText());
|
|
131
|
+
expect(help).toContain('$ mycli deploy <env>');
|
|
132
|
+
expect(help).toContain('--dry-run');
|
|
133
|
+
expect(help).toContain('Deploy safely first');
|
|
134
|
+
expect(stdout.text).toBe('');
|
|
135
|
+
});
|
|
136
|
+
test('openInBrowser prints the URL to stdout in non-tty environments', () => {
|
|
137
|
+
const url = 'https://example.com/dashboard';
|
|
138
|
+
const originalStdoutWrite = process.stdout.write;
|
|
139
|
+
const originalStderrWrite = process.stderr.write;
|
|
140
|
+
const originalIsTTY = process.stdout.isTTY;
|
|
141
|
+
let stdout = '';
|
|
142
|
+
let stderr = '';
|
|
143
|
+
Object.defineProperty(process.stdout, 'isTTY', {
|
|
144
|
+
configurable: true,
|
|
145
|
+
value: false,
|
|
146
|
+
});
|
|
147
|
+
process.stdout.write = ((chunk) => {
|
|
148
|
+
stdout += String(chunk);
|
|
149
|
+
return true;
|
|
150
|
+
});
|
|
151
|
+
process.stderr.write = ((chunk) => {
|
|
152
|
+
stderr += String(chunk);
|
|
153
|
+
return true;
|
|
154
|
+
});
|
|
155
|
+
try {
|
|
156
|
+
openInBrowser(url);
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
process.stdout.write = originalStdoutWrite;
|
|
160
|
+
process.stderr.write = originalStderrWrite;
|
|
161
|
+
Object.defineProperty(process.stdout, 'isTTY', {
|
|
162
|
+
configurable: true,
|
|
163
|
+
value: originalIsTTY,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
expect(stdout).toBe(`${url}\n`);
|
|
167
|
+
expect(stderr).toBe('');
|
|
168
|
+
});
|
|
169
|
+
});
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Type-level tests for schema-based option inference.
|
|
3
3
|
* These tests verify that TypeScript infers the correct types from
|
|
4
|
-
* option names (template literals) and StandardJSONSchemaV1 schemas
|
|
4
|
+
* option names (template literals) and StandardJSONSchemaV1 schemas,
|
|
5
|
+
* and that `.action()` callbacks receive fully-typed positional args
|
|
6
|
+
* and options objects.
|
|
5
7
|
*
|
|
6
8
|
* These use expectTypeOf from vitest for compile-time type assertions.
|
|
7
9
|
*/
|
|
8
10
|
import { describe, test, expectTypeOf } from 'vitest';
|
|
11
|
+
import { z } from 'zod';
|
|
9
12
|
import goke from '../index.js';
|
|
10
13
|
describe('type-level: ExtractOptionName', () => {
|
|
11
14
|
test('extracts name from --name <value>', () => {
|
|
@@ -117,3 +120,241 @@ describe('type-level: middleware use() callback inference', () => {
|
|
|
117
120
|
});
|
|
118
121
|
});
|
|
119
122
|
});
|
|
123
|
+
describe('type-level: command() .action() positional args inference', () => {
|
|
124
|
+
test('command with no args → action receives only (options, ctx)', () => {
|
|
125
|
+
goke('test')
|
|
126
|
+
.command('deploy', 'Deploy the app')
|
|
127
|
+
.action((options, ctx) => {
|
|
128
|
+
expectTypeOf(options).toEqualTypeOf();
|
|
129
|
+
expectTypeOf(ctx).toEqualTypeOf();
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
test('command with one required arg → action receives (arg, options, ctx)', () => {
|
|
133
|
+
goke('test')
|
|
134
|
+
.command('get <id>', 'Fetch a resource by id')
|
|
135
|
+
.action((id, options, ctx) => {
|
|
136
|
+
expectTypeOf(id).toEqualTypeOf();
|
|
137
|
+
expectTypeOf(options).toEqualTypeOf();
|
|
138
|
+
expectTypeOf(ctx).toEqualTypeOf();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
test('command with two required args → action receives both as strings', () => {
|
|
142
|
+
goke('test')
|
|
143
|
+
.command('convert <input> <output>', 'Convert file formats')
|
|
144
|
+
.action((input, output, options) => {
|
|
145
|
+
expectTypeOf(input).toEqualTypeOf();
|
|
146
|
+
expectTypeOf(output).toEqualTypeOf();
|
|
147
|
+
expectTypeOf(options).toEqualTypeOf();
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
test('command with optional arg → arg type includes undefined', () => {
|
|
151
|
+
goke('test')
|
|
152
|
+
.command('run [script]', 'Run a script')
|
|
153
|
+
.action((script, options) => {
|
|
154
|
+
expectTypeOf(script).toEqualTypeOf();
|
|
155
|
+
expectTypeOf(options).toEqualTypeOf();
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
test('command with variadic required arg → arg is string[]', () => {
|
|
159
|
+
goke('test')
|
|
160
|
+
.command('exec <...args>', 'Run a binary with args')
|
|
161
|
+
.action((args, options) => {
|
|
162
|
+
expectTypeOf(args).toEqualTypeOf();
|
|
163
|
+
expectTypeOf(options).toEqualTypeOf();
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
test('command with variadic optional arg → arg is string[]', () => {
|
|
167
|
+
goke('test')
|
|
168
|
+
.command('run [...rest]', 'Variadic optional')
|
|
169
|
+
.action((rest, options) => {
|
|
170
|
+
expectTypeOf(rest).toEqualTypeOf();
|
|
171
|
+
expectTypeOf(options).toEqualTypeOf();
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
test('multi-word command with required arg', () => {
|
|
175
|
+
goke('test')
|
|
176
|
+
.command('mcp getNodeXml <id>', 'Get XML for a node')
|
|
177
|
+
.action((id, options) => {
|
|
178
|
+
expectTypeOf(id).toEqualTypeOf();
|
|
179
|
+
expectTypeOf(options).toEqualTypeOf();
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
test('default command with one positional arg', () => {
|
|
183
|
+
goke('test')
|
|
184
|
+
.command('<file>', 'Default command')
|
|
185
|
+
.action((file, options) => {
|
|
186
|
+
expectTypeOf(file).toEqualTypeOf();
|
|
187
|
+
expectTypeOf(options).toEqualTypeOf();
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
test('mixed required and optional positional args', () => {
|
|
191
|
+
goke('test')
|
|
192
|
+
.command('send <to> [cc]', 'Send a message')
|
|
193
|
+
.action((to, cc, options) => {
|
|
194
|
+
expectTypeOf(to).toEqualTypeOf();
|
|
195
|
+
expectTypeOf(cc).toEqualTypeOf();
|
|
196
|
+
expectTypeOf(options).toEqualTypeOf();
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
describe('type-level: command() .action() option inference', () => {
|
|
201
|
+
test('single schema-based option is visible on options param', () => {
|
|
202
|
+
goke('test')
|
|
203
|
+
.command('serve', 'Start server')
|
|
204
|
+
.option('--port <port>', z.number())
|
|
205
|
+
.action((options, ctx) => {
|
|
206
|
+
expectTypeOf(options.port).toEqualTypeOf();
|
|
207
|
+
expectTypeOf(ctx).toEqualTypeOf();
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
test('multiple schema-based options are accumulated', () => {
|
|
211
|
+
goke('test')
|
|
212
|
+
.command('serve', 'Start server')
|
|
213
|
+
.option('--port <port>', z.number())
|
|
214
|
+
.option('--host <host>', z.string())
|
|
215
|
+
.option('--verbose', z.boolean())
|
|
216
|
+
.action((options) => {
|
|
217
|
+
expectTypeOf(options.port).toEqualTypeOf();
|
|
218
|
+
expectTypeOf(options.host).toEqualTypeOf();
|
|
219
|
+
// Boolean flag is optional (no <...> brackets)
|
|
220
|
+
expectTypeOf(options.verbose).toEqualTypeOf();
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
test('required vs optional option shape', () => {
|
|
224
|
+
goke('test')
|
|
225
|
+
.command('cmd', 'Command')
|
|
226
|
+
.option('--name <name>', z.string())
|
|
227
|
+
.option('--count [count]', z.number())
|
|
228
|
+
.action((options) => {
|
|
229
|
+
expectTypeOf(options.name).toEqualTypeOf();
|
|
230
|
+
expectTypeOf(options.count).toEqualTypeOf();
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
test('camelCase conversion for kebab-case option names', () => {
|
|
234
|
+
goke('test')
|
|
235
|
+
.command('build', 'Build')
|
|
236
|
+
.option('--out-dir <dir>', z.string())
|
|
237
|
+
.option('--my-long-flag <val>', z.string())
|
|
238
|
+
.action((options) => {
|
|
239
|
+
expectTypeOf(options.outDir).toEqualTypeOf();
|
|
240
|
+
expectTypeOf(options.myLongFlag).toEqualTypeOf();
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
test('options combined with positional args', () => {
|
|
244
|
+
goke('test')
|
|
245
|
+
.command('convert <input> <output>', 'Convert file format')
|
|
246
|
+
.option('--quality <quality>', z.number())
|
|
247
|
+
.option('--format <format>', z.enum(['png', 'jpg', 'webp']))
|
|
248
|
+
.action((input, output, options, ctx) => {
|
|
249
|
+
expectTypeOf(input).toEqualTypeOf();
|
|
250
|
+
expectTypeOf(output).toEqualTypeOf();
|
|
251
|
+
expectTypeOf(options.quality).toEqualTypeOf();
|
|
252
|
+
expectTypeOf(options.format).toEqualTypeOf();
|
|
253
|
+
expectTypeOf(ctx).toEqualTypeOf();
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
test('global options from Goke are visible inside command actions', () => {
|
|
257
|
+
goke('test')
|
|
258
|
+
.option('--verbose', z.boolean())
|
|
259
|
+
.command('serve', 'Start server')
|
|
260
|
+
.option('--port <port>', z.number())
|
|
261
|
+
.action((options) => {
|
|
262
|
+
// Global option from cli.option()
|
|
263
|
+
expectTypeOf(options.verbose).toEqualTypeOf();
|
|
264
|
+
// Command-local option
|
|
265
|
+
expectTypeOf(options.port).toEqualTypeOf();
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
test('untyped option (string description) produces loose value type', () => {
|
|
269
|
+
goke('test')
|
|
270
|
+
.command('serve', 'Start server')
|
|
271
|
+
.option('--port <port>', 'Port number')
|
|
272
|
+
.action((options) => {
|
|
273
|
+
// Without a schema the runtime still guarantees required value options are strings.
|
|
274
|
+
expectTypeOf(options.port).toEqualTypeOf();
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
test('untyped optional value options surface as string | undefined', () => {
|
|
278
|
+
goke('test')
|
|
279
|
+
.command('serve', 'Start server')
|
|
280
|
+
.option('--host [host]', 'Optional host override')
|
|
281
|
+
.option('--verbose', 'Verbose output')
|
|
282
|
+
.action((options) => {
|
|
283
|
+
// `[value]` options always resolve to `string | undefined`:
|
|
284
|
+
// - omitted → undefined
|
|
285
|
+
// - `--host` → '' (flag present, no value)
|
|
286
|
+
// - `--host example` → 'example'
|
|
287
|
+
expectTypeOf(options.host).toEqualTypeOf();
|
|
288
|
+
expectTypeOf(options.verbose).toEqualTypeOf();
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
test('accessing a non-existent option in action is a type error', () => {
|
|
292
|
+
goke('test')
|
|
293
|
+
.command('serve', 'Start server')
|
|
294
|
+
.option('--port <port>', z.number())
|
|
295
|
+
.action((options) => {
|
|
296
|
+
expectTypeOf(options.port).toEqualTypeOf();
|
|
297
|
+
// @ts-expect-error nonExistent was never declared
|
|
298
|
+
options.nonExistent;
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
test('accessing a non-existent positional arg in action is a type error', () => {
|
|
302
|
+
goke('test')
|
|
303
|
+
.command('get <id>', 'Fetch resource')
|
|
304
|
+
.action((id, options, ctx, ...rest) => {
|
|
305
|
+
expectTypeOf(id).toEqualTypeOf();
|
|
306
|
+
expectTypeOf(options).toEqualTypeOf();
|
|
307
|
+
expectTypeOf(ctx).toEqualTypeOf();
|
|
308
|
+
// No more positional slots — rest should be empty
|
|
309
|
+
expectTypeOf(rest).toEqualTypeOf();
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
test('action callback can omit trailing params (fewer-args is valid)', () => {
|
|
313
|
+
// Dropping context is fine
|
|
314
|
+
goke('test')
|
|
315
|
+
.command('serve', 'Start server')
|
|
316
|
+
.option('--port <port>', z.number())
|
|
317
|
+
.action((options) => {
|
|
318
|
+
expectTypeOf(options.port).toEqualTypeOf();
|
|
319
|
+
});
|
|
320
|
+
// Dropping everything is fine
|
|
321
|
+
goke('test')
|
|
322
|
+
.command('serve', 'Start server')
|
|
323
|
+
.option('--port <port>', z.number())
|
|
324
|
+
.action(() => { });
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
describe('type-level: README TypeScript examples', () => {
|
|
328
|
+
test('README TypeScript example infers positional args and typed options', () => {
|
|
329
|
+
goke('my-program')
|
|
330
|
+
.command('serve <entry>', 'Start the app')
|
|
331
|
+
.option('--port <port>', z.number().default(3000).describe('Port number'))
|
|
332
|
+
.option('--watch', 'Watch files')
|
|
333
|
+
.action((entry, options, { console, process }) => {
|
|
334
|
+
expectTypeOf(entry).toEqualTypeOf();
|
|
335
|
+
expectTypeOf(options.port).toEqualTypeOf();
|
|
336
|
+
expectTypeOf(options.watch).toEqualTypeOf();
|
|
337
|
+
expectTypeOf(console.log).toBeFunction();
|
|
338
|
+
expectTypeOf(process.cwd).toEqualTypeOf();
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
test('README global options and middleware example stays typed end-to-end', () => {
|
|
342
|
+
goke('mycli')
|
|
343
|
+
.option('--verbose', z.boolean().default(false).describe('Enable verbose logging'))
|
|
344
|
+
.option('--api-url [url]', z.string().default('https://api.example.com').describe('API base URL'))
|
|
345
|
+
.use((options, { process }) => {
|
|
346
|
+
expectTypeOf(options.verbose).toEqualTypeOf();
|
|
347
|
+
expectTypeOf(options.apiUrl).toEqualTypeOf();
|
|
348
|
+
expectTypeOf(process.stdin).toEqualTypeOf();
|
|
349
|
+
})
|
|
350
|
+
.command('deploy <env>', 'Deploy to an environment')
|
|
351
|
+
.option('--dry-run', 'Preview without deploying')
|
|
352
|
+
.action((env, options, ctx) => {
|
|
353
|
+
expectTypeOf(env).toEqualTypeOf();
|
|
354
|
+
expectTypeOf(options.verbose).toEqualTypeOf();
|
|
355
|
+
expectTypeOf(options.apiUrl).toEqualTypeOf();
|
|
356
|
+
expectTypeOf(options.dryRun).toEqualTypeOf();
|
|
357
|
+
expectTypeOf(ctx).toEqualTypeOf();
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
});
|