@usestatemachines/cli 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +34 -2
  3. package/dist/agents/bundled.d.ts +5 -0
  4. package/dist/agents/bundled.js +6 -0
  5. package/dist/agents/destinations.d.ts +13 -0
  6. package/dist/agents/destinations.js +16 -0
  7. package/dist/api-internal.d.ts +62 -0
  8. package/dist/api-internal.js +348 -0
  9. package/dist/bin.d.ts +2 -0
  10. package/dist/bin.js +4 -0
  11. package/dist/command.d.ts +119 -0
  12. package/dist/command.js +195 -0
  13. package/dist/commands/api/command.d.ts +15 -0
  14. package/dist/commands/api/command.js +38 -0
  15. package/dist/commands/api/run.d.ts +4 -0
  16. package/dist/commands/api/run.js +24 -0
  17. package/dist/commands/apps/command.d.ts +19 -0
  18. package/dist/commands/apps/command.js +29 -0
  19. package/dist/commands/apps/list.d.ts +8 -0
  20. package/dist/commands/apps/list.js +10 -0
  21. package/dist/commands/connect/command.d.ts +34 -0
  22. package/dist/commands/connect/command.js +70 -0
  23. package/dist/commands/connect/run.d.ts +10 -0
  24. package/dist/commands/connect/run.js +71 -0
  25. package/dist/commands/doctor/command.d.ts +29 -0
  26. package/dist/commands/doctor/command.js +36 -0
  27. package/dist/commands/doctor/run.d.ts +13 -0
  28. package/dist/commands/doctor/run.js +12 -0
  29. package/dist/commands/env/command.d.ts +5 -0
  30. package/dist/commands/env/command.js +19 -0
  31. package/dist/commands/env/pull.d.ts +6 -0
  32. package/dist/commands/env/pull.js +6 -0
  33. package/dist/commands/environments/command.d.ts +121 -0
  34. package/dist/commands/environments/command.js +170 -0
  35. package/dist/commands/environments/create.d.ts +9 -0
  36. package/dist/commands/environments/create.js +42 -0
  37. package/dist/commands/environments/delete.d.ts +7 -0
  38. package/dist/commands/environments/delete.js +48 -0
  39. package/dist/commands/environments/get.d.ts +39 -0
  40. package/dist/commands/environments/get.js +5 -0
  41. package/dist/commands/environments/list.d.ts +41 -0
  42. package/dist/commands/environments/list.js +18 -0
  43. package/dist/commands/environments/wait.d.ts +39 -0
  44. package/dist/commands/environments/wait.js +21 -0
  45. package/dist/commands/init/command.d.ts +68 -0
  46. package/dist/commands/init/command.js +90 -0
  47. package/dist/commands/init/run.d.ts +8 -0
  48. package/dist/commands/init/run.js +422 -0
  49. package/dist/commands/link/command.d.ts +13 -0
  50. package/dist/commands/link/command.js +32 -0
  51. package/dist/commands/link/run.d.ts +12 -0
  52. package/dist/commands/link/run.js +7 -0
  53. package/dist/commands/login/command.d.ts +1 -0
  54. package/dist/commands/login/command.js +43 -0
  55. package/dist/commands/login/run.d.ts +30 -0
  56. package/dist/commands/login/run.js +43 -0
  57. package/dist/commands/login/schema.d.ts +33 -0
  58. package/dist/commands/login/schema.js +33 -0
  59. package/dist/commands/logout/command.d.ts +1 -0
  60. package/dist/commands/logout/command.js +24 -0
  61. package/dist/commands/logout/run.d.ts +10 -0
  62. package/dist/commands/logout/run.js +19 -0
  63. package/dist/commands/logout/schema.d.ts +10 -0
  64. package/dist/commands/logout/schema.js +10 -0
  65. package/dist/commands/profiles/command.d.ts +2 -0
  66. package/dist/commands/profiles/command.js +29 -0
  67. package/dist/commands/profiles/list.d.ts +20 -0
  68. package/dist/commands/profiles/list.js +10 -0
  69. package/dist/commands/profiles/schema.d.ts +24 -0
  70. package/dist/commands/profiles/schema.js +19 -0
  71. package/dist/commands/profiles/use.d.ts +7 -0
  72. package/dist/commands/profiles/use.js +7 -0
  73. package/dist/commands/requests/command.d.ts +39 -0
  74. package/dist/commands/requests/command.js +65 -0
  75. package/dist/commands/requests/get.d.ts +32 -0
  76. package/dist/commands/requests/get.js +15 -0
  77. package/dist/commands/requests/list.d.ts +27 -0
  78. package/dist/commands/requests/list.js +25 -0
  79. package/dist/commands/snapshots/command.d.ts +48 -0
  80. package/dist/commands/snapshots/command.js +80 -0
  81. package/dist/commands/snapshots/create.d.ts +7 -0
  82. package/dist/commands/snapshots/create.js +26 -0
  83. package/dist/commands/snapshots/delete.d.ts +34 -0
  84. package/dist/commands/snapshots/delete.js +8 -0
  85. package/dist/commands/snapshots/get.d.ts +35 -0
  86. package/dist/commands/snapshots/get.js +5 -0
  87. package/dist/commands/snapshots/list.d.ts +37 -0
  88. package/dist/commands/snapshots/list.js +10 -0
  89. package/dist/commands/unlink/command.d.ts +1 -0
  90. package/dist/commands/unlink/command.js +15 -0
  91. package/dist/commands/unlink/run.d.ts +7 -0
  92. package/dist/commands/unlink/run.js +8 -0
  93. package/dist/commands/whoami/command.d.ts +28 -0
  94. package/dist/commands/whoami/command.js +44 -0
  95. package/dist/commands/whoami/run.d.ts +5 -0
  96. package/dist/commands/whoami/run.js +39 -0
  97. package/dist/commands/workspaces/command.d.ts +33 -0
  98. package/dist/commands/workspaces/command.js +73 -0
  99. package/dist/commands/workspaces/list.d.ts +5 -0
  100. package/dist/commands/workspaces/list.js +42 -0
  101. package/dist/commands/workspaces/use.d.ts +5 -0
  102. package/dist/commands/workspaces/use.js +18 -0
  103. package/dist/config/project.d.ts +21 -0
  104. package/dist/config/project.js +105 -0
  105. package/dist/config/schema.d.ts +616 -0
  106. package/dist/config/schema.js +282 -0
  107. package/dist/context-types.d.ts +89 -0
  108. package/dist/context-types.js +56 -0
  109. package/dist/context.d.ts +13 -0
  110. package/dist/context.js +290 -0
  111. package/dist/credentials/sealing.d.ts +10 -0
  112. package/dist/credentials/sealing.js +72 -0
  113. package/dist/credentials/store.d.ts +17 -0
  114. package/dist/credentials/store.js +156 -0
  115. package/dist/doctor.d.ts +4 -0
  116. package/dist/doctor.js +239 -0
  117. package/dist/env-file.d.ts +10 -0
  118. package/dist/env-file.js +254 -0
  119. package/dist/errors.d.ts +784 -0
  120. package/dist/errors.js +459 -0
  121. package/dist/flags.d.ts +22 -0
  122. package/dist/flags.js +56 -0
  123. package/dist/generated/internal.d.ts +138 -0
  124. package/dist/generated/internal.js +2627 -0
  125. package/dist/generated/retry.d.ts +1 -0
  126. package/dist/generated/retry.js +11 -0
  127. package/dist/help.d.ts +408 -0
  128. package/dist/help.js +220 -0
  129. package/dist/io-types.d.ts +127 -0
  130. package/dist/io-types.js +1 -0
  131. package/dist/io.d.ts +13 -0
  132. package/dist/io.js +1017 -0
  133. package/dist/keys.d.ts +50 -0
  134. package/dist/keys.js +691 -0
  135. package/dist/link.d.ts +6 -0
  136. package/dist/link.js +58 -0
  137. package/dist/login.d.ts +92 -0
  138. package/dist/login.js +965 -0
  139. package/dist/main.d.ts +2 -0
  140. package/dist/main.js +126 -0
  141. package/dist/native-helper.d.ts +1 -0
  142. package/dist/native-helper.js +2 -0
  143. package/dist/output.d.ts +8 -0
  144. package/dist/output.js +135 -0
  145. package/dist/parser.d.ts +28 -0
  146. package/dist/parser.js +250 -0
  147. package/dist/program-types.d.ts +67 -0
  148. package/dist/program-types.js +14 -0
  149. package/dist/programs.d.ts +2 -0
  150. package/dist/programs.js +44 -0
  151. package/dist/project-roots.d.ts +3 -0
  152. package/dist/project-roots.js +26 -0
  153. package/dist/prompt.d.ts +3 -0
  154. package/dist/prompt.js +61 -0
  155. package/dist/registry.d.ts +2 -0
  156. package/dist/registry.js +44 -0
  157. package/dist/resources/create.d.ts +9 -0
  158. package/dist/resources/create.js +55 -0
  159. package/dist/resources/read.d.ts +15 -0
  160. package/dist/resources/read.js +69 -0
  161. package/dist/resources/schema.d.ts +177 -0
  162. package/dist/resources/schema.js +158 -0
  163. package/dist/text.d.ts +3 -0
  164. package/dist/text.js +19 -0
  165. package/dist/update.d.ts +22 -0
  166. package/dist/update.js +126 -0
  167. package/dist/version.d.ts +2 -0
  168. package/dist/version.js +2 -0
  169. package/package.json +52 -3
@@ -0,0 +1 @@
1
+ export declare const retryPolicy: { readonly initialInterval: number; readonly maxInterval: number; readonly exponent: number; readonly statusCodes: readonly number[]; readonly retryConnectionErrors: boolean };
@@ -0,0 +1,11 @@
1
+ export const retryPolicy = {
2
+ "initialInterval": 1000,
3
+ "maxInterval": 5000,
4
+ "exponent": 1.5,
5
+ "statusCodes": [
6
+ 502,
7
+ 503,
8
+ 504
9
+ ],
10
+ "retryConnectionErrors": true
11
+ };
package/dist/help.d.ts ADDED
@@ -0,0 +1,408 @@
1
+ import { z } from 'zod';
2
+ import { type CommandHelp, type RegisteredCommand } from './command.js';
3
+ export declare const versionSchema: z.ZodObject<{
4
+ cli: z.ZodString;
5
+ sdk: z.ZodString;
6
+ node: z.ZodString;
7
+ }, z.core.$strict>;
8
+ export declare const utilityCommands: readonly CommandHelp[];
9
+ export declare function helpDocument(commands: readonly RegisteredCommand[], command: CommandHelp | null, group: string | null): {
10
+ commands: CommandHelp[];
11
+ globalFlags: ({
12
+ name: string;
13
+ key: string;
14
+ type: "string";
15
+ description: string;
16
+ short?: undefined;
17
+ negatedName?: undefined;
18
+ } | {
19
+ name: string;
20
+ key: string;
21
+ type: "boolean";
22
+ negatedName: null;
23
+ description: string;
24
+ short?: undefined;
25
+ } | {
26
+ name: string;
27
+ key: string;
28
+ short: string;
29
+ type: "boolean";
30
+ negatedName: null;
31
+ description: string;
32
+ })[];
33
+ exitCodes: ({
34
+ code: 0;
35
+ meaning: string;
36
+ } | {
37
+ code: 1;
38
+ meaning: string;
39
+ } | {
40
+ code: 2;
41
+ meaning: string;
42
+ } | {
43
+ code: 3;
44
+ meaning: string;
45
+ } | {
46
+ code: 130;
47
+ meaning: string;
48
+ })[];
49
+ errors: ({
50
+ message: string;
51
+ exitCode: 1;
52
+ retryable: false;
53
+ userActionRequired: false;
54
+ next: never[];
55
+ code: string;
56
+ } | {
57
+ message: string;
58
+ exitCode: 1;
59
+ retryable: false;
60
+ userActionRequired: false;
61
+ next: never[];
62
+ code: string;
63
+ } | {
64
+ message: string;
65
+ exitCode: 3;
66
+ retryable: false;
67
+ userActionRequired: true;
68
+ next: {
69
+ command: string;
70
+ reason: string;
71
+ }[];
72
+ code: string;
73
+ } | {
74
+ message: string;
75
+ exitCode: 2;
76
+ retryable: false;
77
+ userActionRequired: true;
78
+ next: never[];
79
+ code: string;
80
+ } | {
81
+ message: string;
82
+ exitCode: 2;
83
+ retryable: false;
84
+ userActionRequired: true;
85
+ next: never[];
86
+ code: string;
87
+ } | {
88
+ message: string;
89
+ exitCode: 1;
90
+ retryable: true;
91
+ userActionRequired: false;
92
+ next: never[];
93
+ code: string;
94
+ } | {
95
+ message: string;
96
+ exitCode: 1;
97
+ retryable: false;
98
+ userActionRequired: true;
99
+ next: never[];
100
+ code: string;
101
+ } | {
102
+ message: string;
103
+ exitCode: 1;
104
+ retryable: false;
105
+ userActionRequired: true;
106
+ next: never[];
107
+ code: string;
108
+ } | {
109
+ message: string;
110
+ exitCode: 1;
111
+ retryable: false;
112
+ userActionRequired: true;
113
+ next: never[];
114
+ code: string;
115
+ } | {
116
+ message: string;
117
+ exitCode: 3;
118
+ retryable: false;
119
+ userActionRequired: true;
120
+ next: {
121
+ command: string;
122
+ reason: string;
123
+ }[];
124
+ code: string;
125
+ } | {
126
+ message: string;
127
+ exitCode: 3;
128
+ retryable: false;
129
+ userActionRequired: true;
130
+ next: {
131
+ command: string;
132
+ reason: string;
133
+ }[];
134
+ code: string;
135
+ } | {
136
+ message: string;
137
+ exitCode: 3;
138
+ retryable: false;
139
+ userActionRequired: true;
140
+ next: {
141
+ command: string;
142
+ reason: string;
143
+ }[];
144
+ code: string;
145
+ } | {
146
+ message: string;
147
+ exitCode: 3;
148
+ retryable: false;
149
+ userActionRequired: true;
150
+ next: {
151
+ command: string;
152
+ reason: string;
153
+ }[];
154
+ code: string;
155
+ } | {
156
+ message: string;
157
+ exitCode: 3;
158
+ retryable: false;
159
+ userActionRequired: true;
160
+ next: {
161
+ command: string;
162
+ reason: string;
163
+ }[];
164
+ code: string;
165
+ } | {
166
+ message: string;
167
+ exitCode: 2;
168
+ retryable: false;
169
+ userActionRequired: true;
170
+ next: {
171
+ command: string;
172
+ reason: string;
173
+ }[];
174
+ code: string;
175
+ } | {
176
+ message: string;
177
+ exitCode: 2;
178
+ retryable: false;
179
+ userActionRequired: true;
180
+ next: {
181
+ command: string;
182
+ reason: string;
183
+ }[];
184
+ code: string;
185
+ } | {
186
+ message: string;
187
+ exitCode: 2;
188
+ retryable: false;
189
+ userActionRequired: true;
190
+ next: {
191
+ command: string;
192
+ reason: string;
193
+ }[];
194
+ code: string;
195
+ } | {
196
+ message: string;
197
+ exitCode: 3;
198
+ retryable: false;
199
+ userActionRequired: true;
200
+ next: {
201
+ command: string;
202
+ reason: string;
203
+ }[];
204
+ code: string;
205
+ } | {
206
+ message: string;
207
+ exitCode: 1;
208
+ retryable: false;
209
+ userActionRequired: true;
210
+ next: {
211
+ command: string;
212
+ reason: string;
213
+ }[];
214
+ code: string;
215
+ } | {
216
+ message: string;
217
+ exitCode: 1;
218
+ retryable: true;
219
+ userActionRequired: true;
220
+ next: {
221
+ command: string;
222
+ reason: string;
223
+ }[];
224
+ code: string;
225
+ } | {
226
+ message: string;
227
+ exitCode: 1;
228
+ retryable: false;
229
+ userActionRequired: true;
230
+ next: {
231
+ command: string;
232
+ reason: string;
233
+ }[];
234
+ code: string;
235
+ } | {
236
+ message: string;
237
+ exitCode: 1;
238
+ retryable: false;
239
+ userActionRequired: true;
240
+ next: {
241
+ command: string;
242
+ reason: string;
243
+ }[];
244
+ code: string;
245
+ } | {
246
+ message: string;
247
+ exitCode: 1;
248
+ retryable: false;
249
+ userActionRequired: true;
250
+ next: {
251
+ command: string;
252
+ reason: string;
253
+ }[];
254
+ code: string;
255
+ } | {
256
+ message: string;
257
+ exitCode: 2;
258
+ retryable: false;
259
+ userActionRequired: true;
260
+ next: never[];
261
+ code: string;
262
+ } | {
263
+ message: string;
264
+ exitCode: 2;
265
+ retryable: false;
266
+ userActionRequired: false;
267
+ next: {
268
+ command: string;
269
+ reason: string;
270
+ }[];
271
+ code: string;
272
+ } | {
273
+ message: string;
274
+ exitCode: 2;
275
+ retryable: false;
276
+ userActionRequired: false;
277
+ next: {
278
+ command: string;
279
+ reason: string;
280
+ }[];
281
+ code: string;
282
+ } | {
283
+ message: string;
284
+ exitCode: 1;
285
+ retryable: true;
286
+ userActionRequired: false;
287
+ next: never[];
288
+ code: string;
289
+ } | {
290
+ message: string;
291
+ exitCode: 1;
292
+ retryable: false;
293
+ userActionRequired: true;
294
+ next: {
295
+ command: string;
296
+ reason: string;
297
+ }[];
298
+ code: string;
299
+ } | {
300
+ message: string;
301
+ exitCode: 2;
302
+ retryable: false;
303
+ userActionRequired: true;
304
+ next: never[];
305
+ code: string;
306
+ } | {
307
+ message: string;
308
+ exitCode: 1;
309
+ retryable: false;
310
+ userActionRequired: true;
311
+ next: {
312
+ command: string;
313
+ reason: string;
314
+ }[];
315
+ code: string;
316
+ } | {
317
+ message: string;
318
+ exitCode: 1;
319
+ retryable: true;
320
+ userActionRequired: false;
321
+ next: never[];
322
+ code: string;
323
+ } | {
324
+ message: string;
325
+ exitCode: 1;
326
+ retryable: false;
327
+ userActionRequired: true;
328
+ next: {
329
+ command: string;
330
+ reason: string;
331
+ }[];
332
+ code: string;
333
+ } | {
334
+ message: string;
335
+ exitCode: 1;
336
+ retryable: false;
337
+ userActionRequired: true;
338
+ next: {
339
+ command: string;
340
+ reason: string;
341
+ }[];
342
+ code: string;
343
+ } | {
344
+ message: string;
345
+ exitCode: 1;
346
+ retryable: true;
347
+ userActionRequired: false;
348
+ next: never[];
349
+ code: string;
350
+ } | {
351
+ message: string;
352
+ exitCode: 1;
353
+ retryable: false;
354
+ userActionRequired: true;
355
+ next: {
356
+ command: string;
357
+ reason: string;
358
+ }[];
359
+ code: string;
360
+ } | {
361
+ message: string;
362
+ exitCode: 1;
363
+ retryable: true;
364
+ userActionRequired: true;
365
+ next: {
366
+ command: string;
367
+ reason: string;
368
+ }[];
369
+ code: string;
370
+ } | {
371
+ message: string;
372
+ exitCode: 1;
373
+ retryable: true;
374
+ userActionRequired: false;
375
+ next: never[];
376
+ code: string;
377
+ } | {
378
+ message: string;
379
+ exitCode: 1;
380
+ retryable: true;
381
+ userActionRequired: false;
382
+ next: never[];
383
+ code: string;
384
+ } | {
385
+ message: string;
386
+ exitCode: 1;
387
+ retryable: false;
388
+ userActionRequired: false;
389
+ next: never[];
390
+ code: string;
391
+ } | {
392
+ message: string;
393
+ exitCode: 130;
394
+ retryable: false;
395
+ userActionRequired: false;
396
+ next: never[];
397
+ code: string;
398
+ } | {
399
+ message: string;
400
+ exitCode: 1;
401
+ retryable: false;
402
+ userActionRequired: false;
403
+ next: never[];
404
+ code: string;
405
+ })[];
406
+ };
407
+ export declare function helpText(commands: readonly RegisteredCommand[], command: CommandHelp | null, group: string | null): string;
408
+ export declare function completionScript(commands: readonly RegisteredCommand[], shell: 'bash' | 'zsh' | 'fish'): string;
package/dist/help.js ADDED
@@ -0,0 +1,220 @@
1
+ import { z } from 'zod';
2
+ import { commandHelp, } from './command.js';
3
+ import { errorHelp, exitCodeHelp } from './errors.js';
4
+ import { globalFlags } from './flags.js';
5
+ export const versionSchema = z.strictObject({
6
+ cli: z.string(),
7
+ sdk: z.string(),
8
+ node: z.string(),
9
+ });
10
+ function utilityHelp(name, summary, schemas, raw = {}) {
11
+ return commandHelp({
12
+ name: [name],
13
+ summary,
14
+ need: 'public',
15
+ destructive: false,
16
+ confirmation: 'before-run',
17
+ streams: false,
18
+ acceptsWorkspace: false,
19
+ output: 'structured',
20
+ outputException: null,
21
+ arguments: [],
22
+ flags: [],
23
+ exitCodes: exitCodeHelp.map((entry) => entry.code),
24
+ ...raw,
25
+ }, {
26
+ input: schemas.input ??
27
+ (() => ({
28
+ type: 'object',
29
+ additionalProperties: false,
30
+ properties: {},
31
+ })),
32
+ result: schemas.result,
33
+ progress: () => false,
34
+ });
35
+ }
36
+ export const utilityCommands = [
37
+ utilityHelp('run', 'Run a command with a workspace key. Child streams, exit codes, and signals pass through unchanged. Use run -- <command> [args].', {
38
+ result: () => null,
39
+ input: () => ({
40
+ type: 'object',
41
+ required: ['command'],
42
+ properties: {
43
+ command: { type: 'string', minLength: 1 },
44
+ args: { type: 'array', items: { type: 'string' } },
45
+ },
46
+ additionalProperties: false,
47
+ }),
48
+ }, {
49
+ need: 'workspace',
50
+ acceptsWorkspace: true,
51
+ output: 'passthrough',
52
+ arguments: [
53
+ { name: 'command', key: 'command', required: true, many: false },
54
+ { name: 'args', key: 'args', required: false, many: true },
55
+ ],
56
+ }),
57
+ utilityHelp('help', 'Show commands, flags, result schemas, and errors.', {
58
+ result: () => z.json().parse(z.toJSONSchema(z.json())),
59
+ }),
60
+ utilityHelp('version', 'Show the CLI, SDK, and Node versions.', {
61
+ result: () => z.json().parse(z.toJSONSchema(versionSchema)),
62
+ }),
63
+ utilityHelp('completion', 'Print a shell completion script.', {
64
+ result: () => null,
65
+ input: () => z
66
+ .json()
67
+ .parse(z.toJSONSchema(z.strictObject({ shell: z.enum(['bash', 'zsh', 'fish']) }))),
68
+ }, {
69
+ output: 'raw',
70
+ arguments: [{ name: 'shell', key: 'shell', required: true, many: false }],
71
+ }),
72
+ ];
73
+ function listed(commands, group) {
74
+ return [...commands.map((entry) => entry.help), ...utilityCommands].filter((entry) => group === null || entry.name[0] === group);
75
+ }
76
+ export function helpDocument(commands, command, group) {
77
+ return {
78
+ commands: command === null ? listed(commands, group) : [command],
79
+ globalFlags,
80
+ exitCodes: exitCodeHelp,
81
+ errors: errorHelp(),
82
+ };
83
+ }
84
+ function acceptedFlags(command) {
85
+ return [
86
+ ...command.flags,
87
+ ...globalFlags.filter((flag) => (flag.name !== 'workspace' || command.acceptsWorkspace) &&
88
+ (flag.name !== 'stream' || command.streams)),
89
+ ];
90
+ }
91
+ export function helpText(commands, command, group) {
92
+ if (command === null) {
93
+ const prefix = group === null ? '' : `${group} `;
94
+ return [
95
+ `Usage: statemachines ${prefix}<command> [options]`,
96
+ '',
97
+ 'Commands:',
98
+ ...listed(commands, group).map((entry) => ` ${entry.name.join(' ')} ${entry.summary}`),
99
+ '',
100
+ `Run statemachines ${prefix}<command> --help for arguments and options.`,
101
+ ].join('\n');
102
+ }
103
+ const args = command.arguments
104
+ .map((arg) => `${arg.required ? '<' : '['}${arg.name}${arg.many ? '...' : ''}${arg.required ? '>' : ']'}`)
105
+ .join(' ');
106
+ const flags = acceptedFlags(command);
107
+ return [
108
+ `Usage: statemachines ${command.name.join(' ')}${args === '' ? '' : ` ${args}`} [options]`,
109
+ '',
110
+ command.summary,
111
+ '',
112
+ 'Options:',
113
+ ...flags.map((flag) => ` --${flag.name}${flag.type === 'boolean' ? (flag.negatedName === null ? '' : ` / --${flag.negatedName}`) : ' <value>'} ${flag.description}`),
114
+ ].join('\n');
115
+ }
116
+ function completionCases(commands) {
117
+ const entries = listed(commands, null);
118
+ const groups = new Map();
119
+ for (const { name } of entries) {
120
+ const [first, second] = name;
121
+ groups.set(first, [
122
+ ...(groups.get(first) ?? []),
123
+ ...(second === undefined ? [] : [second]),
124
+ ]);
125
+ }
126
+ return [
127
+ { path: '', exact: true, words: [...groups.keys()] },
128
+ ...[...groups]
129
+ .filter(([, subcommands]) => subcommands.length > 0)
130
+ .map(([group, subcommands]) => ({
131
+ path: group,
132
+ exact: true,
133
+ words: subcommands,
134
+ })),
135
+ ...entries.map((entry) => ({
136
+ path: entry.name.join(' '),
137
+ exact: false,
138
+ words: acceptedFlags(entry).flatMap((flag) => (flag.type === 'boolean' && flag.negatedName !== null
139
+ ? [flag.name, flag.negatedName]
140
+ : [flag.name]).map((name) => `--${name}`)),
141
+ })),
142
+ ];
143
+ }
144
+ export function completionScript(commands, shell) {
145
+ const cases = completionCases(commands);
146
+ const pattern = ({ path, exact }) => exact ? `'${path}'` : `'${path}'|'${path} '*`;
147
+ const valued = globalFlags
148
+ .filter((flag) => flag.type !== 'boolean')
149
+ .map((flag) => `--${flag.name}`);
150
+ const booleans = globalFlags.flatMap((flag) => flag.type === 'boolean'
151
+ ? [`--${flag.name}`, ...('short' in flag ? [`-${flag.short}`] : [])]
152
+ : []);
153
+ const alone = [...booleans, ...valued.map((name) => `${name}=*`)];
154
+ switch (shell) {
155
+ case 'bash':
156
+ return [
157
+ '_statemachines() {',
158
+ " local cur=\"${COMP_WORDS[COMP_CWORD]}\" line='' words='' skip='' word i",
159
+ ' for ((i = 1; i < COMP_CWORD; i++)); do',
160
+ ' word="${COMP_WORDS[i]}"',
161
+ ' if [ -n "$skip" ]; then skip=\'\'; continue; fi',
162
+ ' case "$word" in',
163
+ ` ${valued.join('|')}) skip=1 ;;`,
164
+ ` ${alone.join('|')}) ;;`,
165
+ ' *) line="${line:+$line }$word" ;;',
166
+ ' esac',
167
+ ' done',
168
+ ' case "$line" in',
169
+ ...cases.map((entry) => ` ${pattern(entry)}) words='${entry.words.join(' ')}' ;;`),
170
+ ' esac',
171
+ ' COMPREPLY=($(compgen -W "$words" -- "$cur"))',
172
+ '}',
173
+ 'complete -F _statemachines statemachines',
174
+ ].join('\n');
175
+ case 'zsh':
176
+ return [
177
+ '#compdef statemachines',
178
+ '_statemachines() {',
179
+ ' local -a candidates',
180
+ " local line='' skip='' word",
181
+ ' for word in "${(@)words[2,CURRENT-1]}"; do',
182
+ " if [[ -n $skip ]]; then skip=''; continue; fi",
183
+ ' case "$word" in',
184
+ ` (${valued.join('|')}) skip=1 ;;`,
185
+ ` (${alone.join('|')}) ;;`,
186
+ ' (*) line="${line:+$line }$word" ;;',
187
+ ' esac',
188
+ ' done',
189
+ ' case "$line" in',
190
+ ...cases.map((entry) => ` ${pattern(entry)}) candidates=(${entry.words.join(' ')}) ;;`),
191
+ ' esac',
192
+ ' compadd -- "${candidates[@]}"',
193
+ '}',
194
+ 'if [ "$funcstack[1]" = _statemachines ]; then',
195
+ ' _statemachines "$@"',
196
+ 'else',
197
+ ' compdef _statemachines statemachines',
198
+ 'fi',
199
+ ].join('\n');
200
+ case 'fish':
201
+ return [
202
+ 'function __statemachines_path',
203
+ ' set -l words',
204
+ ' set -l skip',
205
+ ' for word in (commandline -opc)[2..-1]',
206
+ ' if set -q skip[1]; set -e skip; continue; end',
207
+ ` if contains -- $word ${valued.join(' ')}`,
208
+ ' set skip 1',
209
+ ` else if not contains -- $word ${booleans.join(' ')}; and not string match -qr -- '^(${valued.join('|')})=' $word`,
210
+ ' set -a words $word',
211
+ ' end',
212
+ ' end',
213
+ " set -l path (string join ' ' -- $words)",
214
+ ' test "$path" = "$argv[1]"; or begin; test "$argv[2]" = prefix; and string match -q -- "$argv[1] *" "$path"; end',
215
+ 'end',
216
+ 'complete -c statemachines -f',
217
+ ...cases.map((entry) => `complete -c statemachines -n '__statemachines_path "${entry.path}"${entry.exact ? '' : ' prefix'}' -a '${entry.words.join(' ')}'`),
218
+ ].join('\n');
219
+ }
220
+ }