@hop-top/kit 0.4.0-alpha.1
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/CHANGELOG.md +86 -0
- package/LICENSE +25 -0
- package/README.md +189 -0
- package/dist/aim.d.ts +116 -0
- package/dist/aim.js +417 -0
- package/dist/alias.d.ts +73 -0
- package/dist/alias.js +207 -0
- package/dist/api.d.ts +62 -0
- package/dist/api.js +165 -0
- package/dist/auth.d.ts +26 -0
- package/dist/auth.js +41 -0
- package/dist/cli.d.ts +309 -0
- package/dist/cli.js +885 -0
- package/dist/config.d.ts +97 -0
- package/dist/config.js +79 -0
- package/dist/errcorrect.d.ts +37 -0
- package/dist/errcorrect.js +69 -0
- package/dist/llm.d.ts +188 -0
- package/dist/llm.js +356 -0
- package/dist/output.d.ts +213 -0
- package/dist/output.js +447 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +56 -0
- package/dist/provenance.d.ts +24 -0
- package/dist/provenance.js +32 -0
- package/dist/routellm.d.ts +64 -0
- package/dist/routellm.js +258 -0
- package/dist/rpc.d.ts +25 -0
- package/dist/rpc.js +101 -0
- package/dist/safety.d.ts +24 -0
- package/dist/safety.js +44 -0
- package/dist/scope.d.ts +121 -0
- package/dist/scope.js +380 -0
- package/dist/sqlstore.d.ts +84 -0
- package/dist/sqlstore.js +85 -0
- package/dist/stream.d.ts +35 -0
- package/dist/stream.js +52 -0
- package/dist/tui/index.d.ts +300 -0
- package/dist/tui/index.js +332 -0
- package/dist/upgrade.d.ts +95 -0
- package/dist/upgrade.js +115 -0
- package/dist/xdg.d.ts +169 -0
- package/dist/xdg.js +129 -0
- package/eslint.config.mjs +17 -0
- package/package.json +133 -0
- package/src/README.md +3 -0
- package/src/aim.test.ts +331 -0
- package/src/aim.ts +388 -0
- package/src/alias.test.ts +423 -0
- package/src/alias.ts +256 -0
- package/src/api.test.ts +190 -0
- package/src/api.ts +214 -0
- package/src/auth.test.ts +24 -0
- package/src/auth.ts +37 -0
- package/src/bus.test.ts +178 -0
- package/src/bus.ts +114 -0
- package/src/cli.test.ts +690 -0
- package/src/cli.ts +741 -0
- package/src/completion-values.test.ts +202 -0
- package/src/completion-values.ts +173 -0
- package/src/completion.test.ts +212 -0
- package/src/completion.ts +158 -0
- package/src/compliance.test.ts +134 -0
- package/src/compliance.ts +669 -0
- package/src/config.test.ts +110 -0
- package/src/config.ts +164 -0
- package/src/errcorrect.test.ts +134 -0
- package/src/errcorrect.ts +84 -0
- package/src/flagregister.test.ts +103 -0
- package/src/flagregister.ts +78 -0
- package/src/gen/crud_connect.ts +76 -0
- package/src/gen/crud_pb.ts +269 -0
- package/src/hint.test.ts +315 -0
- package/src/hint.ts +178 -0
- package/src/llm.test.ts +612 -0
- package/src/llm.ts +577 -0
- package/src/log.test.ts +145 -0
- package/src/log.ts +155 -0
- package/src/output/builtins.ts +34 -0
- package/src/output/dispatch.ts +195 -0
- package/src/output/flags.ts +151 -0
- package/src/output/format_help.ts +80 -0
- package/src/output/formatter.ts +167 -0
- package/src/output/formatters/csv.ts +72 -0
- package/src/output/formatters/json.ts +44 -0
- package/src/output/formatters/table.ts +44 -0
- package/src/output/formatters/text.ts +109 -0
- package/src/output/formatters/yaml.ts +47 -0
- package/src/output/projection.ts +126 -0
- package/src/output/registry.ts +88 -0
- package/src/output/template.ts +63 -0
- package/src/output.test.ts +117 -0
- package/src/output.ts +90 -0
- package/src/progress.test.ts +93 -0
- package/src/progress.ts +58 -0
- package/src/provenance.test.ts +59 -0
- package/src/provenance.ts +26 -0
- package/src/routellm.test.ts +179 -0
- package/src/routellm.ts +365 -0
- package/src/router/bert.test.ts +180 -0
- package/src/router/bert.ts +145 -0
- package/src/router/controller.test.ts +183 -0
- package/src/router/controller.ts +165 -0
- package/src/router/integration.test.ts +309 -0
- package/src/router/intent.test.ts +187 -0
- package/src/router/intent.ts +208 -0
- package/src/router/mf.test.ts +66 -0
- package/src/router/mf.ts +52 -0
- package/src/router/random.test.ts +23 -0
- package/src/router/random.ts +13 -0
- package/src/router/router.test.ts +31 -0
- package/src/router/router.ts +62 -0
- package/src/router/server.test.ts +193 -0
- package/src/router/server.ts +134 -0
- package/src/router/sw.test.ts +199 -0
- package/src/router/sw.ts +232 -0
- package/src/rpc.test.ts +47 -0
- package/src/rpc.ts +91 -0
- package/src/safety.test.ts +53 -0
- package/src/safety.ts +42 -0
- package/src/scope-defaults.json +72 -0
- package/src/scope.test.ts +177 -0
- package/src/scope.ts +382 -0
- package/src/setflag.test.ts +110 -0
- package/src/setflag.ts +102 -0
- package/src/sqlstore.test.ts +110 -0
- package/src/sqlstore.ts +144 -0
- package/src/stream.test.ts +69 -0
- package/src/stream.ts +47 -0
- package/src/textflag.test.ts +105 -0
- package/src/textflag.ts +68 -0
- package/src/toolspec.test.ts +187 -0
- package/src/toolspec.ts +279 -0
- package/src/triton/client.test.ts +205 -0
- package/src/triton/client.ts +261 -0
- package/src/tui/anim.test.ts +146 -0
- package/src/tui/anim.ts +134 -0
- package/src/tui/badge.test.ts +41 -0
- package/src/tui/badge.ts +55 -0
- package/src/tui/confirm.test.ts +68 -0
- package/src/tui/confirm.ts +49 -0
- package/src/tui/index.ts +29 -0
- package/src/tui/list.test.ts +84 -0
- package/src/tui/list.ts +72 -0
- package/src/tui/parity.ts +41 -0
- package/src/tui/pills.test.ts +40 -0
- package/src/tui/pills.ts +43 -0
- package/src/tui/progress.test.ts +164 -0
- package/src/tui/progress.ts +120 -0
- package/src/tui/spinner.test.ts +111 -0
- package/src/tui/spinner.ts +96 -0
- package/src/tui/status.test.ts +68 -0
- package/src/tui/status.ts +66 -0
- package/src/upgrade.test.ts +298 -0
- package/src/upgrade.ts +205 -0
- package/src/uri.ts +199 -0
- package/src/wizard.test.ts +441 -0
- package/src/wizard.ts +662 -0
- package/src/xdg.test.ts +202 -0
- package/src/xdg.ts +280 -0
- package/test/output/cols.test.ts +148 -0
- package/test/output/csv.test.ts +129 -0
- package/test/output/dispatch.test.ts +194 -0
- package/test/output/format_help.test.ts +131 -0
- package/test/output/formatter.test.ts +137 -0
- package/test/output/output_flag.test.ts +124 -0
- package/test/output/registry.test.ts +127 -0
- package/test/output/smoke-capture.ts +83 -0
- package/test/output/smoke.test.ts +142 -0
- package/test/output/template.test.ts +95 -0
- package/test/output/text.test.ts +75 -0
- package/test/testdata/api-fixture.json +115 -0
- package/test/testdata/query-vectors.json +187 -0
- package/tsconfig.json +12 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
CompletionRegistry,
|
|
4
|
+
staticCompleter,
|
|
5
|
+
staticValues,
|
|
6
|
+
funcCompleter,
|
|
7
|
+
prefixedCompleter,
|
|
8
|
+
configKeysCompleter,
|
|
9
|
+
fileCompleter,
|
|
10
|
+
dirCompleter,
|
|
11
|
+
type CompletionItem,
|
|
12
|
+
} from './completion-values';
|
|
13
|
+
|
|
14
|
+
describe('staticCompleter', () => {
|
|
15
|
+
it('returns all items when prefix is empty', () => {
|
|
16
|
+
const c = staticCompleter(
|
|
17
|
+
{ value: 'leo', description: 'Low Earth Orbit' },
|
|
18
|
+
{ value: 'geo', description: 'Geostationary' },
|
|
19
|
+
);
|
|
20
|
+
expect(c.complete('')).toEqual([
|
|
21
|
+
{ value: 'leo', description: 'Low Earth Orbit' },
|
|
22
|
+
{ value: 'geo', description: 'Geostationary' },
|
|
23
|
+
]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('filters by prefix', () => {
|
|
27
|
+
const c = staticCompleter(
|
|
28
|
+
{ value: 'leo' },
|
|
29
|
+
{ value: 'lunar' },
|
|
30
|
+
{ value: 'geo' },
|
|
31
|
+
);
|
|
32
|
+
expect(c.complete('l')).toEqual([
|
|
33
|
+
{ value: 'leo' },
|
|
34
|
+
{ value: 'lunar' },
|
|
35
|
+
]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('filters case-insensitively', () => {
|
|
39
|
+
const c = staticCompleter(
|
|
40
|
+
{ value: 'LEO' },
|
|
41
|
+
{ value: 'geo' },
|
|
42
|
+
);
|
|
43
|
+
expect(c.complete('le')).toEqual([{ value: 'LEO' }]);
|
|
44
|
+
expect(c.complete('GE')).toEqual([{ value: 'geo' }]);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('staticValues', () => {
|
|
49
|
+
it('wraps strings into CompletionItems', () => {
|
|
50
|
+
const c = staticValues('leo', 'geo', 'lunar');
|
|
51
|
+
expect(c.complete('')).toEqual([
|
|
52
|
+
{ value: 'leo' },
|
|
53
|
+
{ value: 'geo' },
|
|
54
|
+
{ value: 'lunar' },
|
|
55
|
+
]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('filters by prefix', () => {
|
|
59
|
+
const c = staticValues('leo', 'geo', 'lunar');
|
|
60
|
+
expect(c.complete('g')).toEqual([{ value: 'geo' }]);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('funcCompleter', () => {
|
|
65
|
+
it('invokes callback with prefix', () => {
|
|
66
|
+
let captured = '';
|
|
67
|
+
const c = funcCompleter((prefix) => {
|
|
68
|
+
captured = prefix;
|
|
69
|
+
return [{ value: 'dynamic-' + prefix }];
|
|
70
|
+
});
|
|
71
|
+
const result = c.complete('foo');
|
|
72
|
+
expect(captured).toBe('foo');
|
|
73
|
+
expect(result).toEqual([{ value: 'dynamic-foo' }]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('returns empty when callback returns empty', () => {
|
|
77
|
+
const c = funcCompleter(() => []);
|
|
78
|
+
expect(c.complete('x')).toEqual([]);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe('prefixedCompleter', () => {
|
|
83
|
+
it('completes dimension prefix when no colon', () => {
|
|
84
|
+
const inner = staticValues('a', 'b');
|
|
85
|
+
const c = prefixedCompleter('env', inner);
|
|
86
|
+
const result = c.complete('');
|
|
87
|
+
expect(result).toEqual([{ value: 'env:', description: 'env:...' }]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('completes values after colon', () => {
|
|
91
|
+
const inner = staticValues('dev', 'prod', 'staging');
|
|
92
|
+
const c = prefixedCompleter('env', inner);
|
|
93
|
+
const result = c.complete('env:p');
|
|
94
|
+
expect(result).toEqual([{ value: 'env:prod' }]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('returns all values when prefix matches dimension + colon', () => {
|
|
98
|
+
const inner = staticValues('x', 'y');
|
|
99
|
+
const c = prefixedCompleter('dim', inner);
|
|
100
|
+
const result = c.complete('dim:');
|
|
101
|
+
expect(result).toEqual([
|
|
102
|
+
{ value: 'dim:x' },
|
|
103
|
+
{ value: 'dim:y' },
|
|
104
|
+
]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('returns nothing when prefix does not match dimension', () => {
|
|
108
|
+
const inner = staticValues('a');
|
|
109
|
+
const c = prefixedCompleter('env', inner);
|
|
110
|
+
expect(c.complete('other:')).toEqual([]);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('configKeysCompleter', () => {
|
|
115
|
+
it('returns top-level keys', () => {
|
|
116
|
+
const c = configKeysCompleter({ name: 'x', version: '1', debug: true });
|
|
117
|
+
const items = c.complete('');
|
|
118
|
+
const values = items.map((i: CompletionItem) => i.value);
|
|
119
|
+
expect(values).toContain('name');
|
|
120
|
+
expect(values).toContain('version');
|
|
121
|
+
expect(values).toContain('debug');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('returns nested keys with dot notation', () => {
|
|
125
|
+
const c = configKeysCompleter({ db: { host: 'localhost', port: 5432 } });
|
|
126
|
+
const items = c.complete('db.');
|
|
127
|
+
const values = items.map((i: CompletionItem) => i.value);
|
|
128
|
+
expect(values).toContain('db.host');
|
|
129
|
+
expect(values).toContain('db.port');
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('filters by prefix', () => {
|
|
133
|
+
const c = configKeysCompleter({ name: 'x', namespace: 'y', version: '1' });
|
|
134
|
+
const items = c.complete('na');
|
|
135
|
+
const values = items.map((i: CompletionItem) => i.value);
|
|
136
|
+
expect(values).toEqual(['name', 'namespace']);
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe('fileCompleter', () => {
|
|
141
|
+
it('returns items with value field', () => {
|
|
142
|
+
const c = fileCompleter('.ts', '.js');
|
|
143
|
+
const items = c.complete('');
|
|
144
|
+
// file completer returns a marker item for shell-level file completion
|
|
145
|
+
expect(items.length).toBeGreaterThanOrEqual(1);
|
|
146
|
+
expect(items[0]).toHaveProperty('value');
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe('dirCompleter', () => {
|
|
151
|
+
it('returns items with value field', () => {
|
|
152
|
+
const c = dirCompleter();
|
|
153
|
+
const items = c.complete('');
|
|
154
|
+
expect(items.length).toBeGreaterThanOrEqual(1);
|
|
155
|
+
expect(items[0]).toHaveProperty('value');
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe('CompletionRegistry', () => {
|
|
160
|
+
it('registers and retrieves flag completer', () => {
|
|
161
|
+
const reg = new CompletionRegistry();
|
|
162
|
+
const c = staticValues('a', 'b');
|
|
163
|
+
reg.register('--orbit', c);
|
|
164
|
+
expect(reg.forFlag('--orbit')).toBe(c);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('returns undefined for unregistered flag', () => {
|
|
168
|
+
const reg = new CompletionRegistry();
|
|
169
|
+
expect(reg.forFlag('--unknown')).toBeUndefined();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('registers and retrieves arg completer', () => {
|
|
173
|
+
const reg = new CompletionRegistry();
|
|
174
|
+
const c = staticValues('mission-1', 'mission-2');
|
|
175
|
+
reg.registerArg('launch', 0, c);
|
|
176
|
+
expect(reg.forArg('launch', 0)).toBe(c);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('returns undefined for unregistered arg', () => {
|
|
180
|
+
const reg = new CompletionRegistry();
|
|
181
|
+
expect(reg.forArg('launch', 0)).toBeUndefined();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('distinguishes arg positions', () => {
|
|
185
|
+
const reg = new CompletionRegistry();
|
|
186
|
+
const c0 = staticValues('pos0');
|
|
187
|
+
const c1 = staticValues('pos1');
|
|
188
|
+
reg.registerArg('cmd', 0, c0);
|
|
189
|
+
reg.registerArg('cmd', 1, c1);
|
|
190
|
+
expect(reg.forArg('cmd', 0)).toBe(c0);
|
|
191
|
+
expect(reg.forArg('cmd', 1)).toBe(c1);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('overwrites previous registration for same flag', () => {
|
|
195
|
+
const reg = new CompletionRegistry();
|
|
196
|
+
const c1 = staticValues('old');
|
|
197
|
+
const c2 = staticValues('new');
|
|
198
|
+
reg.register('--flag', c1);
|
|
199
|
+
reg.register('--flag', c2);
|
|
200
|
+
expect(reg.forFlag('--flag')).toBe(c2);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module completion-values
|
|
3
|
+
* @package @hop-top/kit
|
|
4
|
+
*
|
|
5
|
+
* Dynamic value completion system for Commander-based CLIs.
|
|
6
|
+
* Provides a Completer interface and built-in completers for
|
|
7
|
+
* flags and positional arguments. Shell scripts (from completion.ts)
|
|
8
|
+
* call back via __complete; the registry resolves the right completer.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export interface CompletionItem {
|
|
12
|
+
value: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface Completer {
|
|
17
|
+
complete(prefix: string): CompletionItem[] | Promise<CompletionItem[]>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class CompletionRegistry {
|
|
21
|
+
private flags = new Map<string, Completer>();
|
|
22
|
+
private args = new Map<string, Completer>();
|
|
23
|
+
|
|
24
|
+
register(flag: string, completer: Completer): void {
|
|
25
|
+
this.flags.set(flag, completer);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
registerArg(cmd: string, pos: number, completer: Completer): void {
|
|
29
|
+
this.args.set(`${cmd}:${pos}`, completer);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
forFlag(flag: string): Completer | undefined {
|
|
33
|
+
return this.flags.get(flag);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
forArg(cmd: string, pos: number): Completer | undefined {
|
|
37
|
+
return this.args.get(`${cmd}:${pos}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Filter items by case-insensitive prefix match. */
|
|
42
|
+
function filterByPrefix(
|
|
43
|
+
items: CompletionItem[],
|
|
44
|
+
prefix: string,
|
|
45
|
+
): CompletionItem[] {
|
|
46
|
+
if (!prefix) return items;
|
|
47
|
+
const lp = prefix.toLowerCase();
|
|
48
|
+
return items.filter(i => i.value.toLowerCase().startsWith(lp));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Completer from pre-defined CompletionItems. */
|
|
52
|
+
export function staticCompleter(
|
|
53
|
+
...items: CompletionItem[]
|
|
54
|
+
): Completer {
|
|
55
|
+
return {
|
|
56
|
+
complete(prefix: string) {
|
|
57
|
+
return filterByPrefix(items, prefix);
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Completer from plain string values. */
|
|
63
|
+
export function staticValues(...values: string[]): Completer {
|
|
64
|
+
const items = values.map(v => ({ value: v }));
|
|
65
|
+
return staticCompleter(...items);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Completer backed by a callback function. */
|
|
69
|
+
export function funcCompleter(
|
|
70
|
+
fn: (prefix: string) => CompletionItem[],
|
|
71
|
+
): Completer {
|
|
72
|
+
return { complete: fn };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Completer for dimensioned values like `env:prod`.
|
|
77
|
+
* Before the colon, suggests the dimension prefix.
|
|
78
|
+
* After the colon, delegates to the inner completer.
|
|
79
|
+
*/
|
|
80
|
+
export function prefixedCompleter(
|
|
81
|
+
dimension: string,
|
|
82
|
+
values: Completer,
|
|
83
|
+
): Completer {
|
|
84
|
+
return {
|
|
85
|
+
complete(prefix: string) {
|
|
86
|
+
const colonIdx = prefix.indexOf(':');
|
|
87
|
+
if (colonIdx < 0) {
|
|
88
|
+
// no colon yet — suggest dimension prefix
|
|
89
|
+
return [{ value: `${dimension}:`, description: `${dimension}:...` }];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const dim = prefix.slice(0, colonIdx);
|
|
93
|
+
if (dim !== dimension) return [];
|
|
94
|
+
|
|
95
|
+
const valPrefix = prefix.slice(colonIdx + 1);
|
|
96
|
+
const inner = values.complete(valPrefix);
|
|
97
|
+
if (Array.isArray(inner)) {
|
|
98
|
+
return inner.map(i => ({
|
|
99
|
+
value: `${dimension}:${i.value}`,
|
|
100
|
+
description: i.description,
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
// async path
|
|
104
|
+
return (inner as Promise<CompletionItem[]>).then(items =>
|
|
105
|
+
items.map(i => ({
|
|
106
|
+
value: `${dimension}:${i.value}`,
|
|
107
|
+
description: i.description,
|
|
108
|
+
})),
|
|
109
|
+
);
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Flatten object keys into dot-notation paths. */
|
|
115
|
+
function flattenKeys(
|
|
116
|
+
obj: Record<string, any>,
|
|
117
|
+
prefix = '',
|
|
118
|
+
): string[] {
|
|
119
|
+
const keys: string[] = [];
|
|
120
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
121
|
+
const full = prefix ? `${prefix}.${k}` : k;
|
|
122
|
+
keys.push(full);
|
|
123
|
+
if (v != null && typeof v === 'object' && !Array.isArray(v)) {
|
|
124
|
+
keys.push(...flattenKeys(v, full));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return keys;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Completer that suggests config keys in dot-notation. */
|
|
131
|
+
export function configKeysCompleter(
|
|
132
|
+
config: Record<string, any>,
|
|
133
|
+
): Completer {
|
|
134
|
+
return {
|
|
135
|
+
complete(prefix: string) {
|
|
136
|
+
const all = flattenKeys(config);
|
|
137
|
+
return filterByPrefix(
|
|
138
|
+
all.map(k => ({ value: k })),
|
|
139
|
+
prefix,
|
|
140
|
+
);
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Completer that signals shell-level file completion.
|
|
147
|
+
* Returns a marker item; the __complete handler translates
|
|
148
|
+
* this to shell-native file glob directives.
|
|
149
|
+
*/
|
|
150
|
+
export function fileCompleter(...extensions: string[]): Completer {
|
|
151
|
+
const desc = extensions.length
|
|
152
|
+
? `Files: ${extensions.join(', ')}`
|
|
153
|
+
: 'Files';
|
|
154
|
+
return {
|
|
155
|
+
complete() {
|
|
156
|
+
return [
|
|
157
|
+
{
|
|
158
|
+
value: '__file__',
|
|
159
|
+
description: desc,
|
|
160
|
+
},
|
|
161
|
+
];
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Completer that signals shell-level directory completion. */
|
|
167
|
+
export function dirCompleter(): Completer {
|
|
168
|
+
return {
|
|
169
|
+
complete() {
|
|
170
|
+
return [{ value: '__dir__', description: 'Directories' }];
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { generateBashCompletion, generateZshCompletion,
|
|
3
|
+
generateFishCompletion, completionCommand } from './completion';
|
|
4
|
+
import { createCLI, setCommandGroup } from './cli';
|
|
5
|
+
import { Command } from 'commander';
|
|
6
|
+
|
|
7
|
+
/** Strip ANSI SGR sequences. */
|
|
8
|
+
function stripAnsi(s: string): string {
|
|
9
|
+
return s.replace(/\x1b\[[^m]*m/g, '');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe('generateBashCompletion', () => {
|
|
13
|
+
it('contains program name in function name', () => {
|
|
14
|
+
const { program } = createCLI({
|
|
15
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
16
|
+
});
|
|
17
|
+
program.command('deploy').description('Deploy');
|
|
18
|
+
const script = generateBashCompletion(program);
|
|
19
|
+
expect(script).toContain('_mytool_completions');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('includes subcommand names', () => {
|
|
23
|
+
const { program } = createCLI({
|
|
24
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
25
|
+
});
|
|
26
|
+
program.command('deploy').description('Deploy');
|
|
27
|
+
program.command('build').description('Build');
|
|
28
|
+
const script = generateBashCompletion(program);
|
|
29
|
+
expect(script).toContain('deploy');
|
|
30
|
+
expect(script).toContain('build');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('includes flag names', () => {
|
|
34
|
+
const { program } = createCLI({
|
|
35
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
36
|
+
});
|
|
37
|
+
const script = generateBashCompletion(program);
|
|
38
|
+
expect(script).toContain('--format');
|
|
39
|
+
expect(script).toContain('--quiet');
|
|
40
|
+
expect(script).toContain('--no-color');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('includes alias names', () => {
|
|
44
|
+
const { program } = createCLI({
|
|
45
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
46
|
+
});
|
|
47
|
+
const deploy = program.command('deploy').description('Deploy');
|
|
48
|
+
deploy.alias('d');
|
|
49
|
+
const script = generateBashCompletion(program);
|
|
50
|
+
expect(script).toContain('deploy');
|
|
51
|
+
expect(script).toContain(' d ');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('registers complete -F', () => {
|
|
55
|
+
const { program } = createCLI({
|
|
56
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
57
|
+
});
|
|
58
|
+
const script = generateBashCompletion(program);
|
|
59
|
+
expect(script).toContain('complete -F _mytool_completions mytool');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('generateZshCompletion', () => {
|
|
64
|
+
it('contains compdef for program name', () => {
|
|
65
|
+
const { program } = createCLI({
|
|
66
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
67
|
+
});
|
|
68
|
+
program.command('deploy').description('Deploy');
|
|
69
|
+
const script = generateZshCompletion(program);
|
|
70
|
+
expect(script).toContain('compdef _mytool mytool');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('includes subcommand names', () => {
|
|
74
|
+
const { program } = createCLI({
|
|
75
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
76
|
+
});
|
|
77
|
+
program.command('deploy').description('Deploy');
|
|
78
|
+
const script = generateZshCompletion(program);
|
|
79
|
+
expect(script).toContain('deploy');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('includes alias names with description', () => {
|
|
83
|
+
const { program } = createCLI({
|
|
84
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
85
|
+
});
|
|
86
|
+
const deploy = program.command('deploy').description('Deploy');
|
|
87
|
+
deploy.alias('d');
|
|
88
|
+
const script = generateZshCompletion(program);
|
|
89
|
+
expect(script).toContain("'d:Deploy'");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('includes flag names', () => {
|
|
93
|
+
const { program } = createCLI({
|
|
94
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
95
|
+
});
|
|
96
|
+
const script = generateZshCompletion(program);
|
|
97
|
+
expect(script).toContain('--format');
|
|
98
|
+
expect(script).toContain('--quiet');
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('generateFishCompletion', () => {
|
|
103
|
+
it('uses program name in complete commands', () => {
|
|
104
|
+
const { program } = createCLI({
|
|
105
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
106
|
+
});
|
|
107
|
+
program.command('deploy').description('Deploy');
|
|
108
|
+
const script = generateFishCompletion(program);
|
|
109
|
+
expect(script).toContain('complete -c mytool');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('includes subcommand completions', () => {
|
|
113
|
+
const { program } = createCLI({
|
|
114
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
115
|
+
});
|
|
116
|
+
program.command('deploy').description('Deploy');
|
|
117
|
+
const script = generateFishCompletion(program);
|
|
118
|
+
expect(script).toContain('deploy');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('includes alias names', () => {
|
|
122
|
+
const { program } = createCLI({
|
|
123
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
124
|
+
});
|
|
125
|
+
const deploy = program.command('deploy').description('Deploy');
|
|
126
|
+
deploy.alias('d');
|
|
127
|
+
const script = generateFishCompletion(program);
|
|
128
|
+
expect(script).toContain("'d'");
|
|
129
|
+
expect(script).toContain("'Deploy'");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('includes flag completions with long names', () => {
|
|
133
|
+
const { program } = createCLI({
|
|
134
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
135
|
+
});
|
|
136
|
+
const script = generateFishCompletion(program);
|
|
137
|
+
// fish uses -l 'name' (no -- prefix)
|
|
138
|
+
expect(script).toContain("-l 'format'");
|
|
139
|
+
expect(script).toContain("-l 'quiet'");
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe('completionCommand', () => {
|
|
144
|
+
it('returns a Command named completion', () => {
|
|
145
|
+
const { program } = createCLI({
|
|
146
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
147
|
+
});
|
|
148
|
+
const cmd = completionCommand(program);
|
|
149
|
+
expect(cmd).toBeInstanceOf(Command);
|
|
150
|
+
expect(cmd.name()).toBe('completion');
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('hidden when placed in a hidden management group', () => {
|
|
154
|
+
const { program } = createCLI({
|
|
155
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
156
|
+
groups: [{ id: 'management', title: 'MANAGEMENT', hidden: true }],
|
|
157
|
+
});
|
|
158
|
+
program.command('run').description('Run');
|
|
159
|
+
const cmd = completionCommand(program);
|
|
160
|
+
setCommandGroup(cmd, 'management');
|
|
161
|
+
program.addCommand(cmd);
|
|
162
|
+
|
|
163
|
+
const help = stripAnsi(program.helpInformation());
|
|
164
|
+
expect(help).not.toContain('completion');
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('has bash, zsh, fish subcommands', () => {
|
|
168
|
+
const { program } = createCLI({
|
|
169
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
170
|
+
});
|
|
171
|
+
const cmd = completionCommand(program);
|
|
172
|
+
const subNames = cmd.commands.map(c => c.name());
|
|
173
|
+
expect(subNames).toContain('bash');
|
|
174
|
+
expect(subNames).toContain('zsh');
|
|
175
|
+
expect(subNames).toContain('fish');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('hidden from default --help', () => {
|
|
179
|
+
const { program } = createCLI({
|
|
180
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
181
|
+
groups: [{ id: 'management', title: 'MANAGEMENT', hidden: true }],
|
|
182
|
+
});
|
|
183
|
+
program.command('run').description('Run something');
|
|
184
|
+
const cmd = completionCommand(program);
|
|
185
|
+
setCommandGroup(cmd, 'management');
|
|
186
|
+
program.addCommand(cmd);
|
|
187
|
+
|
|
188
|
+
const help = stripAnsi(program.helpInformation());
|
|
189
|
+
expect(help).not.toContain('completion');
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('visible in --help-all', () => {
|
|
193
|
+
const origArgv = process.argv;
|
|
194
|
+
try {
|
|
195
|
+
process.argv = ['node', 'mytool', '--help-all'];
|
|
196
|
+
const { program } = createCLI({
|
|
197
|
+
name: 'mytool', version: '1.0.0', description: 'A tool',
|
|
198
|
+
groups: [{ id: 'management', title: 'MANAGEMENT', hidden: true }],
|
|
199
|
+
});
|
|
200
|
+
program.command('run').description('Run something');
|
|
201
|
+
const cmd = completionCommand(program);
|
|
202
|
+
setCommandGroup(cmd, 'management');
|
|
203
|
+
program.addCommand(cmd);
|
|
204
|
+
|
|
205
|
+
const help = stripAnsi(program.helpInformation());
|
|
206
|
+
expect(help).toContain('MANAGEMENT:');
|
|
207
|
+
expect(help).toContain('completion');
|
|
208
|
+
} finally {
|
|
209
|
+
process.argv = origArgv;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
});
|