forked from sanbuphy/learn-coding-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.ts
More file actions
53 lines (48 loc) · 1.4 KB
/
registry.ts
File metadata and controls
53 lines (48 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { memoizeWithLRU } from '../memoize.js'
import specs from './specs/index.js'
export type CommandSpec = {
name: string
description?: string
subcommands?: CommandSpec[]
args?: Argument | Argument[]
options?: Option[]
}
export type Argument = {
name?: string
description?: string
isDangerous?: boolean
isVariadic?: boolean // repeats infinitely e.g. echo hello world
isOptional?: boolean
isCommand?: boolean // wrapper commands e.g. timeout, sudo
isModule?: string | boolean // for python -m and similar module args
isScript?: boolean // script files e.g. node script.js
}
export type Option = {
name: string | string[]
description?: string
args?: Argument | Argument[]
isRequired?: boolean
}
export async function loadFigSpec(
command: string,
): Promise<CommandSpec | null> {
if (!command || command.includes('/') || command.includes('\\')) return null
if (command.includes('..')) return null
if (command.startsWith('-') && command !== '-') return null
try {
const module = await import(`@withfig/autocomplete/build/${command}.js`)
return module.default || module
} catch {
return null
}
}
export const getCommandSpec = memoizeWithLRU(
async (command: string): Promise<CommandSpec | null> => {
const spec =
specs.find(s => s.name === command) ||
(await loadFigSpec(command)) ||
null
return spec
},
(command: string) => command,
)