forked from heygen-com/hyperframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-options.ts
More file actions
174 lines (153 loc) · 4.3 KB
/
Copy pathcli-options.ts
File metadata and controls
174 lines (153 loc) · 4.3 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
export type InlineValueOption<Key extends string> = {
prefix: string;
key: Key;
};
export const CLI_SEMVER_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
type ParserConfig<
Parsed extends object,
ValueKey extends keyof Parsed & string,
BooleanKey extends keyof Parsed & string,
> = {
inlineValueOptions: Array<InlineValueOption<ValueKey>>;
valueOptions: Map<string, ValueKey>;
booleanOptions: Map<string, BooleanKey>;
parsePositional: (arg: string, index: number) => number;
fail: (message: string) => never;
};
export function parseMappedArgument<
Parsed extends object,
ValueKey extends keyof Parsed & string,
BooleanKey extends keyof Parsed & string,
>(
args: string[],
index: number,
parsed: Parsed,
config: ParserConfig<Parsed, ValueKey, BooleanKey>,
) {
const arg = args[index];
if (applyInlineValueOption(arg, parsed, config.inlineValueOptions)) {
return index;
}
if (applyBooleanOption(arg, parsed, config.booleanOptions)) {
return index;
}
return applyValueOrPositionalOption(args, index, parsed, config, arg);
}
function applyInlineValueOption<Parsed extends object, ValueKey extends keyof Parsed & string>(
arg: string,
parsed: Parsed,
inlineOptions: Array<InlineValueOption<ValueKey>>,
) {
const option = inlineOptions.find((candidate) => arg.startsWith(candidate.prefix));
if (!option) {
return false;
}
parsed[option.key] = arg.slice(option.prefix.length) as Parsed[ValueKey];
return true;
}
function applyBooleanOption<Parsed extends object, BooleanKey extends keyof Parsed & string>(
arg: string,
parsed: Parsed,
booleanOptions: Map<string, BooleanKey>,
) {
const option = booleanOptions.get(arg);
if (!option) {
return false;
}
parsed[option] = true as Parsed[BooleanKey];
return true;
}
function applyValueOrPositionalOption<
Parsed extends object,
ValueKey extends keyof Parsed & string,
BooleanKey extends keyof Parsed & string,
>(
args: string[],
index: number,
parsed: Parsed,
config: ParserConfig<Parsed, ValueKey, BooleanKey>,
arg: string,
) {
const option = config.valueOptions.get(arg);
if (!option) {
return config.parsePositional(arg, index);
}
parsed[option] = readNextArg(args, index, arg, config.fail) as Parsed[ValueKey];
return index + 1;
}
export function parseVersionOptionArgument<
Parsed extends { version?: string },
ValueKey extends keyof Parsed & string,
BooleanKey extends keyof Parsed & string,
>(
args: string[],
index: number,
parsed: Parsed,
config: Omit<ParserConfig<Parsed, ValueKey, BooleanKey>, "parsePositional"> & {
printUsage: () => void;
},
) {
return parseMappedArgument(args, index, parsed, {
...config,
parsePositional: (arg, positionalIndex) =>
parseVersionOrHelp(arg, positionalIndex, parsed, config),
});
}
function parseVersionOrHelp<Parsed extends { version?: string }>(
arg: string,
index: number,
parsed: Parsed,
config: { printUsage: () => void; fail: (message: string) => never },
) {
if (arg === "--help" || arg === "-h") {
config.printUsage();
process.exit(0);
}
parsed.version = parseVersionPositionalArg(arg, parsed.version, config.fail);
return index;
}
export function parseVersionPositionalArg(
arg: string,
currentVersion: string | undefined,
fail: (message: string) => never,
) {
if (arg.startsWith("--")) {
fail(`Unknown option: ${arg}`);
}
if (currentVersion) {
fail(`Unexpected positional argument: ${arg}`);
}
return arg.replace(/^v/, "");
}
export function readNextArg(
args: string[],
index: number,
flag: string,
fail: (message: string) => never,
) {
const value = args[index + 1];
if (!value || value.startsWith("--")) {
fail(`Missing value for ${flag}`);
}
return value;
}
export function validateCliVersion(
version: string,
pattern: RegExp,
fail: (message: string) => never,
) {
if (!pattern.test(version)) {
fail(`Invalid semver: ${version}`);
}
}
export function validateCliDate(date: string, fail: (message: string) => never) {
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) {
fail(`Invalid date: ${date}. Expected YYYY-MM-DD.`);
}
}
export function optionalFlagArg(flag: string, enabled: boolean) {
return enabled ? [flag] : [];
}
export function optionalValueArg(flag: string, value: string | undefined) {
return value ? [flag, value] : [];
}