-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy patharguments.ts
More file actions
203 lines (174 loc) · 6.66 KB
/
Copy patharguments.ts
File metadata and controls
203 lines (174 loc) · 6.66 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
export type WizardMode = 'compose' | 'dev' | 'k8s'
export const LIFECYCLE_COMMANDS = [
'start',
'stop',
'restart',
'update',
'status',
'logs',
'down',
'reset',
] as const
export type LifecycleCommand = (typeof LIFECYCLE_COMMANDS)[number]
export type SetupInvocation =
| { kind: 'help' }
| { kind: 'version' }
| { kind: 'wizard'; quick: boolean; mode?: WizardMode }
| { kind: 'config' }
| { kind: 'add'; feature: string; args: string[] }
| { kind: 'doctor'; fix: boolean; json: boolean }
| { kind: 'desktop'; url?: string; noOpen: boolean }
| { kind: 'lifecycle'; command: LifecycleCommand }
export class SetupArgumentError extends Error {
constructor(message: string) {
super(message)
this.name = 'SetupArgumentError'
}
}
function isLifecycleCommand(value: string | undefined): value is LifecycleCommand {
return Boolean(value && (LIFECYCLE_COMMANDS as readonly string[]).includes(value))
}
function fail(message: string): never {
throw new SetupArgumentError(message)
}
function stripDirectoryOption(args: readonly string[]): string[] {
const filtered: string[] = []
let found = false
for (let index = 0; index < args.length; index += 1) {
const arg = args[index]
if (arg === '--dir' || arg.startsWith('--dir=')) {
if (found) fail('--dir may only be provided once')
found = true
if (arg === '--dir') {
const value = args[index + 1]
if (!value || value.startsWith('-')) fail('--dir requires a directory path')
index += 1
} else if (!arg.slice('--dir='.length)) {
fail('--dir requires a directory path')
}
continue
}
filtered.push(arg)
}
return filtered
}
function oneFlag(args: readonly string[], flag: string): boolean {
const count = args.filter((arg) => arg === flag).length
if (count > 1) fail(`${flag} may only be provided once`)
return count === 1
}
/**
* Pulls one `--flag value` / `--flag=value` option out of an argument list,
* returning it alongside everything the caller still has to account for.
* Accepts both spellings, rejects a repeat, and rejects a missing or
* flag-shaped value.
*/
function parseValueOption(
args: readonly string[],
flag: string,
requirement: string
): { value?: string; remaining: string[] } {
let value: string | undefined
const remaining: string[] = []
const prefix = `${flag}=`
for (let index = 0; index < args.length; index += 1) {
const arg = args[index]
if (arg !== flag && !arg.startsWith(prefix)) {
remaining.push(arg)
continue
}
if (value !== undefined) fail(`${flag} may only be provided once`)
const candidate = arg === flag ? args[++index] : arg.slice(prefix.length)
if (!candidate || candidate.startsWith('-')) fail(requirement)
value = candidate
}
return { value, remaining }
}
function parseMode(args: readonly string[]): { mode?: WizardMode; remaining: string[] } {
const { value, remaining } = parseValueOption(args, '--mode', '--mode requires a value')
if (value !== undefined && value !== 'compose' && value !== 'dev' && value !== 'k8s') {
fail(`invalid --mode "${value}" — expected compose, dev, or k8s`)
}
return { mode: value as WizardMode | undefined, remaining }
}
function expectNoArguments(command: string, args: readonly string[]): void {
if (args.length > 0) fail(`${command} does not accept: ${args.join(' ')}`)
}
function parseCore(
args: readonly string[],
helpRequested: boolean
): Exclude<SetupInvocation, { kind: 'help' | 'version' }> {
const command = args[0]
if (!command || command.startsWith('-')) {
const quick = oneFlag(args, '--quick')
const withoutQuick = args.filter((arg) => arg !== '--quick')
const { mode, remaining } = parseMode(withoutQuick)
if (remaining.length > 0) fail(`Unknown setup option: ${remaining[0]}`)
return { kind: 'wizard', quick, ...(mode ? { mode } : {}) }
}
const commandArgs = args.slice(1)
if (command === 'config') {
expectNoArguments(command, commandArgs)
return { kind: 'config' }
}
if (command === 'add') {
const feature = commandArgs[0]
if (!feature) {
if (helpRequested) return { kind: 'add', feature: '', args: [] }
fail('add requires a feature')
}
if (feature.startsWith('-')) fail(`Unknown add option: ${feature}`)
const featureArgs = commandArgs.slice(1)
if (feature === 'integration') {
if (featureArgs.length === 0 && helpRequested) {
return { kind: 'add', feature, args: [] }
}
if (featureArgs.length !== 1 || featureArgs[0].startsWith('-')) {
fail('add integration requires exactly one integration slug')
}
} else if (featureArgs.length > 0) {
fail(`add ${feature} does not accept: ${featureArgs.join(' ')}`)
}
return { kind: 'add', feature, args: featureArgs }
}
if (command === 'desktop') {
const noOpen = oneFlag(commandArgs, '--no-open')
const { value: url, remaining } = parseValueOption(
commandArgs.filter((arg) => arg !== '--no-open'),
'--url',
'--url requires a deployment URL'
)
if (remaining.length > 0) fail(`Unknown desktop option: ${remaining[0]}`)
return { kind: 'desktop', noOpen, ...(url ? { url } : {}) }
}
if (command === 'doctor') {
const fix = oneFlag(commandArgs, '--fix')
const json = oneFlag(commandArgs, '--json')
const remaining = commandArgs.filter((arg) => arg !== '--fix' && arg !== '--json')
if (remaining.length > 0) fail(`Unknown doctor option: ${remaining[0]}`)
return { kind: 'doctor', fix, json }
}
if (isLifecycleCommand(command)) {
expectNoArguments(command, commandArgs)
return { kind: 'lifecycle', command }
}
fail(`Unknown command: ${command}`)
}
/** Parses and validates the complete public command surface before any setup work begins. */
export function parseSetupArguments(rawArgs: readonly string[]): SetupInvocation {
const args = stripDirectoryOption(rawArgs)
const helpCount = args.filter((arg) => arg === '--help' || arg === '-h').length
if (helpCount > 1) fail('--help may only be provided once')
const versionCount = args.filter((arg) => arg === '--version' || arg === '-V').length
if (versionCount > 1) fail('--version may only be provided once')
if (helpCount > 0 && versionCount > 0) fail('--help and --version cannot be combined')
if (versionCount === 1) {
const remaining = args.filter((arg) => arg !== '--version' && arg !== '-V')
expectNoArguments('--version', remaining)
return { kind: 'version' }
}
const helpRequested = helpCount === 1
const withoutHelp = args.filter((arg) => arg !== '--help' && arg !== '-h')
const invocation = parseCore(withoutHelp, helpRequested)
return helpRequested ? { kind: 'help' } : invocation
}