-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathspring.ts
324 lines (309 loc) · 8.03 KB
/
spring.ts
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
type Data = {
dependencies: Options<DependencyGroup>;
packaging: Options<Option>;
javaVersion: Options<Option>;
language: Options<Option>;
bootVersion: Options<Option>;
};
type Options<T> = {
type: string;
values: T[];
};
type Option = {
id: string;
name: string;
};
type DependencyGroup = {
name: string;
values: Dependency[];
};
type Dependency = Option & {
description: string;
versionRange?: string;
};
const memoizedFetchData = () => {
let data: Data | undefined;
return async (
executeShellCommand: Fig.ExecuteCommandFunction
): Promise<Data | undefined> => {
if (data) {
return data;
}
try {
const { stdout } = await executeShellCommand({
command: "curl",
args: [
"-sfL",
"-H",
"Accept: application/json",
"https://start.spring.io/metadata/client",
],
});
data = JSON.parse(stdout);
return data;
} catch (error) {
return undefined;
}
};
};
const fetchData = memoizedFetchData();
const cache: Fig.Generator["cache"] = {
ttl: 1000 * 60 * 60 * 24, // 24 hours, in milliseconds
};
const delimiter = ",";
const dependencyGenerator: Fig.Generator = {
cache,
getQueryTerm: (token) =>
token.slice(token.lastIndexOf(delimiter) + delimiter.length),
trigger: (newToken, oldToken) =>
newToken.lastIndexOf(delimiter) !== oldToken.lastIndexOf(delimiter),
custom: async (tokens, executeShellCommand) => {
const data = await fetchData(executeShellCommand);
if (!data) return [];
const seen = new Set(tokens[tokens.length - 1]?.split(delimiter));
return data.dependencies.values
.flatMap(({ values }) => values)
.filter(({ id }) => !seen.has(id))
.sort((a, b) => a.name.localeCompare(b.name))
.map((dependency) => ({
name: dependency.id,
displayName: dependency.name,
description: dependency.description,
}));
},
};
const versionGenerator: Fig.Generator = {
cache,
custom: async (_, executeShellCommand) => {
const data = await fetchData(executeShellCommand);
if (!data) return [];
return data.bootVersion.values.map((version) => ({
name: version.id,
displayName: version.name,
}));
},
};
const javaVersionGenerator: Fig.Generator = {
cache,
custom: async (_, executeShellCommand) => {
const data = await fetchData(executeShellCommand);
if (!data) return [];
return data.javaVersion.values.map((version) => ({
name: version.id,
displayName: version.name,
}));
},
};
const completionSpec: Fig.Spec = {
name: "spring",
description:
"The Spring Boot CLI is a command line tool that you can use to bootstrap a new project from start.spring.io or encode a password",
icon: "https://start.spring.io/images/icon-48x48.png",
subcommands: [
{
name: "init",
description: "Initialize a new project using Spring Initializr",
options: [
{
name: ["-a", "---artifact-id"],
description: "Project coordinates",
args: {
name: "Name",
},
},
{
name: ["-b", "--boot-version"],
description: "Spring Boot version",
args: {
name: "Version",
generators: versionGenerator,
debounce: true,
},
},
{
name: "--build",
description: "Build system to use",
args: {
name: "System",
suggestions: ["maven", "gradle"],
default: "maven",
},
},
{
name: ["-d", "--dependencies"],
description:
"Comma-separated list of dependency identifiers to include in the generated project",
args: {
name: "Dependencies",
generators: dependencyGenerator,
isVariadic: true,
debounce: true,
},
},
{
name: "--description",
description: "Project description",
insertValue: "--description '{cursor}'",
args: {
name: "description",
},
},
{
name: ["-f", "--force"],
description: "Force overwrite of existing files",
isDangerous: true,
},
{
name: "--format",
description: "Format of the generated content",
args: {
name: "Format",
suggestions: ["build", "project"],
default: "build",
},
},
{
name: ["-g", "--group-id"],
description: "Project coordinates",
args: {
name: "Group ID",
},
},
{
name: ["-j", "--java-version"],
description: "Language level",
args: {
name: "Version",
generators: javaVersionGenerator,
debounce: true,
},
},
{
name: "--list",
description: "List the capabilities of the service",
},
{
name: ["-n", "--name"],
description: "Project name",
args: {
name: "Name",
},
},
{
name: ["-p", "--packaging"],
description: "Project packaging",
args: {
name: "Packaging",
suggestions: ["jar", "war"],
},
},
{
name: "--package-name",
description: "Package name",
args: {
name: "Name",
},
},
{
name: ["-t", "--type"],
description: "Project type",
args: {
name: "Type",
suggestions: [
{
name: "gradle-build",
description: "Generate a Gradle build file",
},
{
name: "gradle-project",
description:
"Generate a Gradle based project archive using the Groovy DSL",
},
{
name: "gradle-project-kotlin",
description:
"Generate a Gradle based project archive using the Kotlin DSL",
},
{
name: "maven-build",
description: "Generate a Maven pom.xml",
},
{
name: "maven-project",
description: "Generate a Maven based project archive",
},
],
default: "gradle-project",
},
},
{
name: "--target",
description: "URL of the service to use",
args: {
name: "URL",
default: "https://start.spring.io",
},
},
{
name: ["-v", "--version"],
description: "Project version",
args: {
name: "Version",
},
},
{
name: ["-x", "--extract"],
description:
"Extract the project archive. Inferred if a location is specified without an extension",
},
],
},
{
name: "encodepassword",
description: "Encode a password for use with Spring Security",
options: [
{
name: ["-a", "--algorithm"],
description: "The algorithm to use",
args: {
name: "Algorithm",
suggestions: ["default", "bcrypt", "pbkdf2"],
default: "default",
},
},
],
args: {
name: "Password",
},
},
{
name: "shell",
description: "Start a nested shell",
},
{
name: "help",
description: "Show help for other commands",
args: {
name: "Command",
template: "help",
},
},
],
options: [
{
name: "--help",
description: "Show help for spring",
},
{
name: "--version",
description: "Get spring CLI version",
},
{
name: "--debug",
description:
"Print additional status information for the command you are running",
isPersistent: true,
},
],
};
export default completionSpec;