-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcompletion-entrypoint.ts
More file actions
44 lines (34 loc) · 1.18 KB
/
Copy pathcompletion-entrypoint.ts
File metadata and controls
44 lines (34 loc) · 1.18 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
export type CompletionEntrypoint = readonly string[];
export interface ScriptOptions {
readonly completionEntrypoint?: CompletionEntrypoint;
}
export const DEFAULT_COMPLETION_ENTRYPOINT = ['complete', '--'] as const;
const SAFE_POSIX_ARG = /^[A-Za-z0-9_./:=+-]+$/;
const SAFE_POWERSHELL_ARG = /^[A-Za-z0-9_./:=+-]+$/;
function quotePosixArg(value: string): string {
if (value !== '' && SAFE_POSIX_ARG.test(value)) {
return value;
}
return `'${value.replace(/'/g, `'\\''`)}'`;
}
function quotePowerShellArg(value: string): string {
if (value !== '--' && value !== '' && SAFE_POWERSHELL_ARG.test(value)) {
return value;
}
return `'${value.replace(/'/g, `''`)}'`;
}
export function getCompletionEntrypoint(
options: ScriptOptions = {}
): CompletionEntrypoint {
return options.completionEntrypoint ?? DEFAULT_COMPLETION_ENTRYPOINT;
}
export function formatCompletionEntrypointForShell(
options: ScriptOptions = {}
): string {
return getCompletionEntrypoint(options).map(quotePosixArg).join(' ');
}
export function formatCompletionEntrypointForPowerShell(
options: ScriptOptions = {}
): string {
return getCompletionEntrypoint(options).map(quotePowerShellArg).join(' ');
}