Skip to content

Commit 7f6a272

Browse files
committed
feat: consistently export same type for all entrypoints
Defines a `TabFunction` that all entrypoints export, such that we consistently support the same call signature.
1 parent 1d0beed commit 7f6a272

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

src/cac.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as fish from './fish';
44
import * as powershell from './powershell';
55
import type { CAC } from 'cac';
66
import { Completion } from './index';
7-
import { CompletionConfig, noopHandler } from './shared';
7+
import { noopHandler, TabFunction } from './shared';
88

99
const execPath = process.execPath;
1010
const processArgs = process.argv.slice(1);
@@ -18,10 +18,7 @@ function quoteIfNeeded(path: string): string {
1818
return path.includes(' ') ? `'${path}'` : path;
1919
}
2020

21-
export default async function tab(
22-
instance: CAC,
23-
completionConfig?: CompletionConfig
24-
) {
21+
const tab: TabFunction<CAC> = async (instance, completionConfig) => {
2522
const completion = new Completion();
2623

2724
// Add all commands and their options
@@ -104,3 +101,5 @@ export default async function tab(
104101

105102
return completion;
106103
}
104+
105+
export default tab;

src/citty.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
SubCommandsDef,
1212
} from 'citty';
1313
import { generateFigSpec } from './fig';
14-
import { CompletionConfig, noopHandler } from './shared';
14+
import { CompletionConfig, noopHandler, TabFunction } from './shared';
1515

1616
function quoteIfNeeded(path: string) {
1717
return path.includes(' ') ? `'${path}'` : path;
@@ -92,10 +92,7 @@ async function handleSubCommands(
9292
}
9393
}
9494

95-
export default async function tab<TArgs extends ArgsDef>(
96-
instance: CommandDef<TArgs>,
97-
completionConfig?: CompletionConfig
98-
) {
95+
const tab: TabFunction<CommandDef<ArgsDef>> = async (instance, completionConfig) => {
9996
const completion = new Completion();
10097

10198
const meta = await resolve(instance.meta);
@@ -206,6 +203,8 @@ export default async function tab<TArgs extends ArgsDef>(
206203
return completion;
207204
}
208205

206+
export default tab;
207+
209208
type Resolvable<T> = T | Promise<T> | (() => T) | (() => Promise<T>);
210209

211210
async function resolve<T>(resolvable: Resolvable<T>): Promise<T> {

src/shared.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Handler } from './index';
1+
import { Handler, Completion } from './index';
22

33
export const noopHandler: Handler = () => {
44
return [];
@@ -16,3 +16,8 @@ export interface CompletionConfig {
1616
}
1717
>;
1818
}
19+
20+
export type TabFunction<T> = (
21+
instance: T,
22+
completionConfig?: CompletionConfig
23+
) => Promise<Completion>;

0 commit comments

Comments
 (0)