@constructive-io/graphql-codegen 4.0.2 → 4.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 (93) hide show
  1. package/cli/handler.d.ts +13 -0
  2. package/cli/handler.js +74 -0
  3. package/cli/index.js +11 -57
  4. package/core/codegen/barrel.d.ts +1 -0
  5. package/core/codegen/barrel.js +5 -2
  6. package/core/codegen/cli/arg-mapper.d.ts +4 -0
  7. package/core/codegen/cli/arg-mapper.js +117 -0
  8. package/core/codegen/cli/command-map-generator.d.ts +16 -0
  9. package/core/codegen/cli/command-map-generator.js +338 -0
  10. package/core/codegen/cli/custom-command-generator.d.ts +8 -0
  11. package/core/codegen/cli/custom-command-generator.js +155 -0
  12. package/core/codegen/cli/docs-generator.d.ts +26 -0
  13. package/core/codegen/cli/docs-generator.js +1399 -0
  14. package/core/codegen/cli/executor-generator.d.ts +11 -0
  15. package/core/codegen/cli/executor-generator.js +217 -0
  16. package/core/codegen/cli/index.d.ts +53 -0
  17. package/core/codegen/cli/index.js +153 -0
  18. package/core/codegen/cli/infra-generator.d.ts +9 -0
  19. package/core/codegen/cli/infra-generator.js +1195 -0
  20. package/core/codegen/cli/table-command-generator.d.ts +7 -0
  21. package/core/codegen/cli/table-command-generator.js +323 -0
  22. package/core/codegen/docs-utils.d.ts +30 -0
  23. package/core/codegen/docs-utils.js +122 -0
  24. package/core/codegen/hooks-docs-generator.d.ts +6 -0
  25. package/core/codegen/hooks-docs-generator.js +468 -0
  26. package/core/codegen/orm/docs-generator.d.ts +6 -0
  27. package/core/codegen/orm/docs-generator.js +416 -0
  28. package/core/codegen/target-docs-generator.d.ts +20 -0
  29. package/core/codegen/target-docs-generator.js +110 -0
  30. package/core/database/index.d.ts +0 -12
  31. package/core/database/index.js +2 -19
  32. package/core/generate.d.ts +34 -2
  33. package/core/generate.js +453 -12
  34. package/core/index.d.ts +0 -2
  35. package/core/index.js +0 -2
  36. package/core/introspect/source/database.js +2 -2
  37. package/core/introspect/source/pgpm-module.js +2 -2
  38. package/core/output/index.d.ts +1 -1
  39. package/core/output/index.js +1 -2
  40. package/core/output/writer.d.ts +0 -10
  41. package/core/output/writer.js +0 -31
  42. package/esm/cli/handler.d.ts +13 -0
  43. package/esm/cli/handler.js +71 -0
  44. package/esm/cli/index.js +11 -57
  45. package/esm/core/codegen/barrel.d.ts +1 -0
  46. package/esm/core/codegen/barrel.js +5 -2
  47. package/esm/core/codegen/cli/arg-mapper.d.ts +4 -0
  48. package/esm/core/codegen/cli/arg-mapper.js +80 -0
  49. package/esm/core/codegen/cli/command-map-generator.d.ts +16 -0
  50. package/esm/core/codegen/cli/command-map-generator.js +301 -0
  51. package/esm/core/codegen/cli/custom-command-generator.d.ts +8 -0
  52. package/esm/core/codegen/cli/custom-command-generator.js +119 -0
  53. package/esm/core/codegen/cli/docs-generator.d.ts +26 -0
  54. package/esm/core/codegen/cli/docs-generator.js +1387 -0
  55. package/esm/core/codegen/cli/executor-generator.d.ts +11 -0
  56. package/esm/core/codegen/cli/executor-generator.js +180 -0
  57. package/esm/core/codegen/cli/index.d.ts +53 -0
  58. package/esm/core/codegen/cli/index.js +128 -0
  59. package/esm/core/codegen/cli/infra-generator.d.ts +9 -0
  60. package/esm/core/codegen/cli/infra-generator.js +1156 -0
  61. package/esm/core/codegen/cli/table-command-generator.d.ts +7 -0
  62. package/esm/core/codegen/cli/table-command-generator.js +287 -0
  63. package/esm/core/codegen/docs-utils.d.ts +30 -0
  64. package/esm/core/codegen/docs-utils.js +112 -0
  65. package/esm/core/codegen/hooks-docs-generator.d.ts +6 -0
  66. package/esm/core/codegen/hooks-docs-generator.js +462 -0
  67. package/esm/core/codegen/orm/docs-generator.d.ts +6 -0
  68. package/esm/core/codegen/orm/docs-generator.js +410 -0
  69. package/esm/core/codegen/target-docs-generator.d.ts +20 -0
  70. package/esm/core/codegen/target-docs-generator.js +105 -0
  71. package/esm/core/database/index.d.ts +0 -12
  72. package/esm/core/database/index.js +1 -17
  73. package/esm/core/generate.d.ts +34 -2
  74. package/esm/core/generate.js +417 -12
  75. package/esm/core/index.d.ts +0 -2
  76. package/esm/core/index.js +0 -2
  77. package/esm/core/introspect/source/database.js +2 -2
  78. package/esm/core/introspect/source/pgpm-module.js +2 -2
  79. package/esm/core/output/index.d.ts +1 -1
  80. package/esm/core/output/index.js +1 -1
  81. package/esm/core/output/writer.d.ts +0 -10
  82. package/esm/core/output/writer.js +0 -30
  83. package/esm/generators/index.d.ts +0 -3
  84. package/esm/generators/index.js +0 -3
  85. package/esm/index.d.ts +4 -3
  86. package/esm/index.js +4 -2
  87. package/esm/types/config.d.ts +78 -0
  88. package/generators/index.d.ts +0 -3
  89. package/generators/index.js +0 -3
  90. package/index.d.ts +4 -3
  91. package/index.js +7 -2
  92. package/package.json +8 -7
  93. package/types/config.d.ts +78 -0
@@ -0,0 +1,1156 @@
1
+ import * as t from '@babel/types';
2
+ import { generateCode } from '../babel-ast';
3
+ import { getGeneratedFileHeader } from '../utils';
4
+ function createImportDeclaration(moduleSpecifier, namedImports, typeOnly = false) {
5
+ const specifiers = namedImports.map((name) => t.importSpecifier(t.identifier(name), t.identifier(name)));
6
+ const decl = t.importDeclaration(specifiers, t.stringLiteral(moduleSpecifier));
7
+ decl.importKind = typeOnly ? 'type' : 'value';
8
+ return decl;
9
+ }
10
+ function buildSwitchCase(testValue, handlerName, args) {
11
+ return t.switchCase(t.stringLiteral(testValue), [
12
+ t.returnStatement(t.callExpression(t.identifier(handlerName), args)),
13
+ ]);
14
+ }
15
+ function buildDefaultSwitchCase(usageVarName) {
16
+ return t.switchCase(null, [
17
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.identifier(usageVarName)])),
18
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('process'), t.identifier('exit')), [t.numericLiteral(1)])),
19
+ ]);
20
+ }
21
+ function buildCommandHandler(params, body, isAsync = true) {
22
+ const func = t.functionDeclaration(null, params, t.blockStatement(body), false, isAsync);
23
+ return func;
24
+ }
25
+ export function generateContextCommand(toolName) {
26
+ const statements = [];
27
+ statements.push(createImportDeclaration('inquirerer', [
28
+ 'CLIOptions',
29
+ 'Inquirerer',
30
+ 'extractFirst',
31
+ ]));
32
+ statements.push(createImportDeclaration('../executor', ['getStore']));
33
+ const usageStr = `
34
+ ${toolName} context <command>
35
+
36
+ Commands:
37
+ create <name> Create a new context
38
+ list List all contexts
39
+ use <name> Set the active context
40
+ current Show current context
41
+ delete <name> Delete a context
42
+
43
+ Create Options:
44
+ --endpoint <url> GraphQL endpoint URL
45
+
46
+ --help, -h Show this help message
47
+ `;
48
+ statements.push(t.variableDeclaration('const', [
49
+ t.variableDeclarator(t.identifier('usage'), t.stringLiteral(usageStr)),
50
+ ]));
51
+ const argvParam = t.identifier('argv');
52
+ argvParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
53
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
54
+ t.tsStringKeyword(),
55
+ t.tsUnknownKeyword(),
56
+ ])),
57
+ ])));
58
+ const prompterParam = t.identifier('prompter');
59
+ prompterParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
60
+ const optionsParam = t.identifier('_options');
61
+ optionsParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CLIOptions')));
62
+ const mainBody = [
63
+ t.ifStatement(t.logicalExpression('||', t.memberExpression(t.identifier('argv'), t.identifier('help')), t.memberExpression(t.identifier('argv'), t.identifier('h'))), t.blockStatement([
64
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.identifier('usage')])),
65
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('process'), t.identifier('exit')), [t.numericLiteral(0)])),
66
+ ])),
67
+ t.variableDeclaration('const', [
68
+ t.variableDeclarator(t.identifier('store'), t.callExpression(t.identifier('getStore'), [])),
69
+ ]),
70
+ t.variableDeclaration('const', [
71
+ t.variableDeclarator(t.objectPattern([
72
+ t.objectProperty(t.identifier('first'), t.identifier('subcommand')),
73
+ t.objectProperty(t.identifier('newArgv'), t.identifier('newArgv'), false, true),
74
+ ]), t.callExpression(t.identifier('extractFirst'), [
75
+ t.identifier('argv'),
76
+ ])),
77
+ ]),
78
+ t.ifStatement(t.unaryExpression('!', t.identifier('subcommand')), t.blockStatement([
79
+ t.variableDeclaration('const', [
80
+ t.variableDeclarator(t.identifier('answer'), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('prompter'), t.identifier('prompt')), [
81
+ t.identifier('argv'),
82
+ t.arrayExpression([
83
+ t.objectExpression([
84
+ t.objectProperty(t.identifier('type'), t.stringLiteral('autocomplete')),
85
+ t.objectProperty(t.identifier('name'), t.stringLiteral('subcommand')),
86
+ t.objectProperty(t.identifier('message'), t.stringLiteral('What do you want to do?')),
87
+ t.objectProperty(t.identifier('options'), t.arrayExpression([
88
+ t.stringLiteral('create'),
89
+ t.stringLiteral('list'),
90
+ t.stringLiteral('use'),
91
+ t.stringLiteral('current'),
92
+ t.stringLiteral('delete'),
93
+ ])),
94
+ ]),
95
+ ]),
96
+ ]))),
97
+ ]),
98
+ t.returnStatement(t.callExpression(t.identifier('handleSubcommand'), [
99
+ t.memberExpression(t.identifier('answer'), t.identifier('subcommand')),
100
+ t.identifier('newArgv'),
101
+ t.identifier('prompter'),
102
+ t.identifier('store'),
103
+ ])),
104
+ ])),
105
+ t.returnStatement(t.callExpression(t.identifier('handleSubcommand'), [
106
+ t.identifier('subcommand'),
107
+ t.identifier('newArgv'),
108
+ t.identifier('prompter'),
109
+ t.identifier('store'),
110
+ ])),
111
+ ];
112
+ const mainExport = t.exportDefaultDeclaration(t.arrowFunctionExpression([argvParam, prompterParam, optionsParam], t.blockStatement(mainBody), true));
113
+ statements.push(mainExport);
114
+ const subcmdParam = t.identifier('subcommand');
115
+ subcmdParam.typeAnnotation = t.tsTypeAnnotation(t.tsStringKeyword());
116
+ const argvParam2 = t.identifier('argv');
117
+ argvParam2.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
118
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
119
+ t.tsStringKeyword(),
120
+ t.tsUnknownKeyword(),
121
+ ])),
122
+ ])));
123
+ const prompterParam2 = t.identifier('prompter');
124
+ prompterParam2.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
125
+ const storeParam = t.identifier('store');
126
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
127
+ t.tsTypeQuery(t.identifier('getStore')),
128
+ ])));
129
+ const handleSubcommandFunc = t.functionDeclaration(t.identifier('handleSubcommand'), [subcmdParam, argvParam2, prompterParam2, storeParam], t.blockStatement([
130
+ t.switchStatement(t.identifier('subcommand'), [
131
+ buildSwitchCase('create', 'handleCreate', [
132
+ t.identifier('argv'),
133
+ t.identifier('prompter'),
134
+ t.identifier('store'),
135
+ ]),
136
+ buildSwitchCase('list', 'handleList', [t.identifier('store')]),
137
+ buildSwitchCase('use', 'handleUse', [
138
+ t.identifier('argv'),
139
+ t.identifier('prompter'),
140
+ t.identifier('store'),
141
+ ]),
142
+ buildSwitchCase('current', 'handleCurrent', [t.identifier('store')]),
143
+ buildSwitchCase('delete', 'handleDelete', [
144
+ t.identifier('argv'),
145
+ t.identifier('prompter'),
146
+ t.identifier('store'),
147
+ ]),
148
+ buildDefaultSwitchCase('usage'),
149
+ ]),
150
+ ]), false, true);
151
+ statements.push(handleSubcommandFunc);
152
+ statements.push(buildCreateHandler());
153
+ statements.push(buildListHandler());
154
+ statements.push(buildUseHandler());
155
+ statements.push(buildCurrentHandler());
156
+ statements.push(buildDeleteHandler());
157
+ const header = getGeneratedFileHeader('Context management commands');
158
+ const code = generateCode(statements);
159
+ return {
160
+ fileName: 'commands/context.ts',
161
+ content: header + '\n' + code,
162
+ };
163
+ }
164
+ function buildCreateHandler() {
165
+ const argvParam = t.identifier('argv');
166
+ argvParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
167
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
168
+ t.tsStringKeyword(),
169
+ t.tsUnknownKeyword(),
170
+ ])),
171
+ ])));
172
+ const prompterParam = t.identifier('prompter');
173
+ prompterParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
174
+ const storeParam = t.identifier('store');
175
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
176
+ t.tsTypeQuery(t.identifier('getStore')),
177
+ ])));
178
+ const body = [
179
+ t.variableDeclaration('const', [
180
+ t.variableDeclarator(t.objectPattern([
181
+ t.objectProperty(t.identifier('first'), t.identifier('name')),
182
+ t.objectProperty(t.identifier('newArgv'), t.identifier('restArgv')),
183
+ ]), t.callExpression(t.identifier('extractFirst'), [
184
+ t.identifier('argv'),
185
+ ])),
186
+ ]),
187
+ t.variableDeclaration('const', [
188
+ t.variableDeclarator(t.identifier('answers'), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('prompter'), t.identifier('prompt')), [
189
+ t.objectExpression([
190
+ t.objectProperty(t.identifier('name'), t.identifier('name'), false, true),
191
+ t.spreadElement(t.identifier('restArgv')),
192
+ ]),
193
+ t.arrayExpression([
194
+ t.objectExpression([
195
+ t.objectProperty(t.identifier('type'), t.stringLiteral('text')),
196
+ t.objectProperty(t.identifier('name'), t.stringLiteral('name')),
197
+ t.objectProperty(t.identifier('message'), t.stringLiteral('Context name')),
198
+ t.objectProperty(t.identifier('required'), t.booleanLiteral(true)),
199
+ ]),
200
+ t.objectExpression([
201
+ t.objectProperty(t.identifier('type'), t.stringLiteral('text')),
202
+ t.objectProperty(t.identifier('name'), t.stringLiteral('endpoint')),
203
+ t.objectProperty(t.identifier('message'), t.stringLiteral('GraphQL endpoint URL')),
204
+ t.objectProperty(t.identifier('required'), t.booleanLiteral(true)),
205
+ ]),
206
+ ]),
207
+ ]))),
208
+ ]),
209
+ t.variableDeclaration('const', [
210
+ t.variableDeclarator(t.identifier('contextName'), t.memberExpression(t.identifier('answers'), t.identifier('name'))),
211
+ ]),
212
+ t.variableDeclaration('const', [
213
+ t.variableDeclarator(t.identifier('endpoint'), t.memberExpression(t.identifier('answers'), t.identifier('endpoint'))),
214
+ ]),
215
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('createContext')), [
216
+ t.identifier('contextName'),
217
+ t.objectExpression([
218
+ t.objectProperty(t.identifier('endpoint'), t.identifier('endpoint')),
219
+ ]),
220
+ ])),
221
+ t.variableDeclaration('const', [
222
+ t.variableDeclarator(t.identifier('settings'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('loadSettings')), [])),
223
+ ]),
224
+ t.ifStatement(t.unaryExpression('!', t.memberExpression(t.identifier('settings'), t.identifier('currentContext'))), t.blockStatement([
225
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('setCurrentContext')), [t.identifier('contextName')])),
226
+ ])),
227
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
228
+ t.templateLiteral([
229
+ t.templateElement({ raw: 'Created context: ', cooked: 'Created context: ' }),
230
+ t.templateElement({ raw: '', cooked: '' }, true),
231
+ ], [t.identifier('contextName')]),
232
+ ])),
233
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
234
+ t.templateLiteral([
235
+ t.templateElement({ raw: ' Endpoint: ', cooked: ' Endpoint: ' }),
236
+ t.templateElement({ raw: '', cooked: '' }, true),
237
+ ], [t.identifier('endpoint')]),
238
+ ])),
239
+ ];
240
+ const func = t.functionDeclaration(t.identifier('handleCreate'), [argvParam, prompterParam, storeParam], t.blockStatement(body), false, true);
241
+ return func;
242
+ }
243
+ function buildListHandler() {
244
+ const storeParam = t.identifier('store');
245
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
246
+ t.tsTypeQuery(t.identifier('getStore')),
247
+ ])));
248
+ const body = [
249
+ t.variableDeclaration('const', [
250
+ t.variableDeclarator(t.identifier('contexts'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('listContexts')), [])),
251
+ ]),
252
+ t.variableDeclaration('const', [
253
+ t.variableDeclarator(t.identifier('settings'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('loadSettings')), [])),
254
+ ]),
255
+ t.ifStatement(t.binaryExpression('===', t.memberExpression(t.identifier('contexts'), t.identifier('length')), t.numericLiteral(0)), t.blockStatement([
256
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.stringLiteral('No contexts configured.')])),
257
+ t.returnStatement(),
258
+ ])),
259
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.stringLiteral('Contexts:')])),
260
+ t.forOfStatement(t.variableDeclaration('const', [
261
+ t.variableDeclarator(t.identifier('ctx')),
262
+ ]), t.identifier('contexts'), t.blockStatement([
263
+ t.variableDeclaration('const', [
264
+ t.variableDeclarator(t.identifier('marker'), t.conditionalExpression(t.binaryExpression('===', t.memberExpression(t.identifier('ctx'), t.identifier('name')), t.memberExpression(t.identifier('settings'), t.identifier('currentContext'))), t.stringLiteral('* '), t.stringLiteral(' '))),
265
+ ]),
266
+ t.variableDeclaration('const', [
267
+ t.variableDeclarator(t.identifier('authStatus'), t.conditionalExpression(t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('hasValidCredentials')), [
268
+ t.memberExpression(t.identifier('ctx'), t.identifier('name')),
269
+ ]), t.stringLiteral('[authenticated]'), t.stringLiteral('[no token]'))),
270
+ ]),
271
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
272
+ t.templateLiteral([
273
+ t.templateElement({ raw: '', cooked: '' }),
274
+ t.templateElement({ raw: '', cooked: '' }),
275
+ t.templateElement({ raw: ' ', cooked: ' ' }),
276
+ t.templateElement({ raw: '', cooked: '' }, true),
277
+ ], [
278
+ t.identifier('marker'),
279
+ t.memberExpression(t.identifier('ctx'), t.identifier('name')),
280
+ t.identifier('authStatus'),
281
+ ]),
282
+ ])),
283
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
284
+ t.templateLiteral([
285
+ t.templateElement({ raw: ' Endpoint: ', cooked: ' Endpoint: ' }),
286
+ t.templateElement({ raw: '', cooked: '' }, true),
287
+ ], [
288
+ t.memberExpression(t.identifier('ctx'), t.identifier('endpoint')),
289
+ ]),
290
+ ])),
291
+ ])),
292
+ ];
293
+ const func = t.functionDeclaration(t.identifier('handleList'), [storeParam], t.blockStatement(body));
294
+ return func;
295
+ }
296
+ function buildUseHandler() {
297
+ const argvParam = t.identifier('argv');
298
+ argvParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
299
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
300
+ t.tsStringKeyword(),
301
+ t.tsUnknownKeyword(),
302
+ ])),
303
+ ])));
304
+ const prompterParam = t.identifier('prompter');
305
+ prompterParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
306
+ const storeParam = t.identifier('store');
307
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
308
+ t.tsTypeQuery(t.identifier('getStore')),
309
+ ])));
310
+ const body = [
311
+ t.variableDeclaration('const', [
312
+ t.variableDeclarator(t.objectPattern([
313
+ t.objectProperty(t.identifier('first'), t.identifier('name')),
314
+ ]), t.callExpression(t.identifier('extractFirst'), [
315
+ t.identifier('argv'),
316
+ ])),
317
+ ]),
318
+ t.variableDeclaration('const', [
319
+ t.variableDeclarator(t.identifier('contexts'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('listContexts')), [])),
320
+ ]),
321
+ t.ifStatement(t.binaryExpression('===', t.memberExpression(t.identifier('contexts'), t.identifier('length')), t.numericLiteral(0)), t.blockStatement([
322
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.stringLiteral('No contexts configured.')])),
323
+ t.returnStatement(),
324
+ ])),
325
+ t.variableDeclaration('let', [
326
+ t.variableDeclarator(t.identifier('contextName'), t.identifier('name')),
327
+ ]),
328
+ t.ifStatement(t.unaryExpression('!', t.identifier('contextName')), t.blockStatement([
329
+ t.variableDeclaration('const', [
330
+ t.variableDeclarator(t.identifier('answer'), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('prompter'), t.identifier('prompt')), [
331
+ t.identifier('argv'),
332
+ t.arrayExpression([
333
+ t.objectExpression([
334
+ t.objectProperty(t.identifier('type'), t.stringLiteral('autocomplete')),
335
+ t.objectProperty(t.identifier('name'), t.stringLiteral('name')),
336
+ t.objectProperty(t.identifier('message'), t.stringLiteral('Select context')),
337
+ t.objectProperty(t.identifier('options'), t.callExpression(t.memberExpression(t.identifier('contexts'), t.identifier('map')), [
338
+ t.arrowFunctionExpression([t.identifier('c')], t.memberExpression(t.identifier('c'), t.identifier('name'))),
339
+ ])),
340
+ ]),
341
+ ]),
342
+ ]))),
343
+ ]),
344
+ t.expressionStatement(t.assignmentExpression('=', t.identifier('contextName'), t.memberExpression(t.identifier('answer'), t.identifier('name')))),
345
+ ])),
346
+ t.ifStatement(t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('setCurrentContext')), [t.identifier('contextName')]), t.blockStatement([
347
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
348
+ t.templateLiteral([
349
+ t.templateElement({ raw: 'Switched to context: ', cooked: 'Switched to context: ' }),
350
+ t.templateElement({ raw: '', cooked: '' }, true),
351
+ ], [t.identifier('contextName')]),
352
+ ])),
353
+ ]), t.blockStatement([
354
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('error')), [
355
+ t.templateLiteral([
356
+ t.templateElement({ raw: 'Context "', cooked: 'Context "' }),
357
+ t.templateElement({ raw: '" not found.', cooked: '" not found.' }, true),
358
+ ], [t.identifier('contextName')]),
359
+ ])),
360
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('process'), t.identifier('exit')), [t.numericLiteral(1)])),
361
+ ])),
362
+ ];
363
+ const func = t.functionDeclaration(t.identifier('handleUse'), [argvParam, prompterParam, storeParam], t.blockStatement(body), false, true);
364
+ return func;
365
+ }
366
+ function buildCurrentHandler() {
367
+ const storeParam = t.identifier('store');
368
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
369
+ t.tsTypeQuery(t.identifier('getStore')),
370
+ ])));
371
+ const body = [
372
+ t.variableDeclaration('const', [
373
+ t.variableDeclarator(t.identifier('current'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('getCurrentContext')), [])),
374
+ ]),
375
+ t.ifStatement(t.unaryExpression('!', t.identifier('current')), t.blockStatement([
376
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.stringLiteral('No current context set.')])),
377
+ t.returnStatement(),
378
+ ])),
379
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
380
+ t.templateLiteral([
381
+ t.templateElement({ raw: 'Current context: ', cooked: 'Current context: ' }),
382
+ t.templateElement({ raw: '', cooked: '' }, true),
383
+ ], [
384
+ t.memberExpression(t.identifier('current'), t.identifier('name')),
385
+ ]),
386
+ ])),
387
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
388
+ t.templateLiteral([
389
+ t.templateElement({ raw: ' Endpoint: ', cooked: ' Endpoint: ' }),
390
+ t.templateElement({ raw: '', cooked: '' }, true),
391
+ ], [
392
+ t.memberExpression(t.identifier('current'), t.identifier('endpoint')),
393
+ ]),
394
+ ])),
395
+ t.variableDeclaration('const', [
396
+ t.variableDeclarator(t.identifier('hasAuth'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('hasValidCredentials')), [
397
+ t.memberExpression(t.identifier('current'), t.identifier('name')),
398
+ ])),
399
+ ]),
400
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
401
+ t.templateLiteral([
402
+ t.templateElement({ raw: ' Auth: ', cooked: ' Auth: ' }),
403
+ t.templateElement({ raw: '', cooked: '' }, true),
404
+ ], [
405
+ t.conditionalExpression(t.identifier('hasAuth'), t.stringLiteral('authenticated'), t.stringLiteral('not authenticated')),
406
+ ]),
407
+ ])),
408
+ ];
409
+ const func = t.functionDeclaration(t.identifier('handleCurrent'), [storeParam], t.blockStatement(body));
410
+ return func;
411
+ }
412
+ function buildDeleteHandler() {
413
+ const argvParam = t.identifier('argv');
414
+ argvParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
415
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
416
+ t.tsStringKeyword(),
417
+ t.tsUnknownKeyword(),
418
+ ])),
419
+ ])));
420
+ const prompterParam = t.identifier('prompter');
421
+ prompterParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
422
+ const storeParam = t.identifier('store');
423
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
424
+ t.tsTypeQuery(t.identifier('getStore')),
425
+ ])));
426
+ const body = [
427
+ t.variableDeclaration('const', [
428
+ t.variableDeclarator(t.objectPattern([
429
+ t.objectProperty(t.identifier('first'), t.identifier('name')),
430
+ ]), t.callExpression(t.identifier('extractFirst'), [
431
+ t.identifier('argv'),
432
+ ])),
433
+ ]),
434
+ t.variableDeclaration('const', [
435
+ t.variableDeclarator(t.identifier('contexts'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('listContexts')), [])),
436
+ ]),
437
+ t.ifStatement(t.binaryExpression('===', t.memberExpression(t.identifier('contexts'), t.identifier('length')), t.numericLiteral(0)), t.blockStatement([
438
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.stringLiteral('No contexts configured.')])),
439
+ t.returnStatement(),
440
+ ])),
441
+ t.variableDeclaration('let', [
442
+ t.variableDeclarator(t.identifier('contextName'), t.identifier('name')),
443
+ ]),
444
+ t.ifStatement(t.unaryExpression('!', t.identifier('contextName')), t.blockStatement([
445
+ t.variableDeclaration('const', [
446
+ t.variableDeclarator(t.identifier('answer'), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('prompter'), t.identifier('prompt')), [
447
+ t.identifier('argv'),
448
+ t.arrayExpression([
449
+ t.objectExpression([
450
+ t.objectProperty(t.identifier('type'), t.stringLiteral('autocomplete')),
451
+ t.objectProperty(t.identifier('name'), t.stringLiteral('name')),
452
+ t.objectProperty(t.identifier('message'), t.stringLiteral('Select context to delete')),
453
+ t.objectProperty(t.identifier('options'), t.callExpression(t.memberExpression(t.identifier('contexts'), t.identifier('map')), [
454
+ t.arrowFunctionExpression([t.identifier('c')], t.memberExpression(t.identifier('c'), t.identifier('name'))),
455
+ ])),
456
+ ]),
457
+ ]),
458
+ ]))),
459
+ ]),
460
+ t.expressionStatement(t.assignmentExpression('=', t.identifier('contextName'), t.memberExpression(t.identifier('answer'), t.identifier('name')))),
461
+ ])),
462
+ t.ifStatement(t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('deleteContext')), [t.identifier('contextName')]), t.blockStatement([
463
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
464
+ t.templateLiteral([
465
+ t.templateElement({ raw: 'Deleted context: ', cooked: 'Deleted context: ' }),
466
+ t.templateElement({ raw: '', cooked: '' }, true),
467
+ ], [t.identifier('contextName')]),
468
+ ])),
469
+ ]), t.blockStatement([
470
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('error')), [
471
+ t.templateLiteral([
472
+ t.templateElement({ raw: 'Context "', cooked: 'Context "' }),
473
+ t.templateElement({ raw: '" not found.', cooked: '" not found.' }, true),
474
+ ], [t.identifier('contextName')]),
475
+ ])),
476
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('process'), t.identifier('exit')), [t.numericLiteral(1)])),
477
+ ])),
478
+ ];
479
+ const func = t.functionDeclaration(t.identifier('handleDelete'), [argvParam, prompterParam, storeParam], t.blockStatement(body), false, true);
480
+ return func;
481
+ }
482
+ export function generateAuthCommand(toolName) {
483
+ const statements = [];
484
+ statements.push(createImportDeclaration('inquirerer', [
485
+ 'CLIOptions',
486
+ 'Inquirerer',
487
+ 'extractFirst',
488
+ ]));
489
+ statements.push(createImportDeclaration('../executor', ['getStore']));
490
+ const usageStr = `
491
+ ${toolName} auth <command>
492
+
493
+ Commands:
494
+ set-token <token> Set API token for the current context
495
+ status Show authentication status
496
+ logout Remove credentials for the current context
497
+
498
+ Options:
499
+ --context <name> Specify context (defaults to current context)
500
+
501
+ --help, -h Show this help message
502
+ `;
503
+ statements.push(t.variableDeclaration('const', [
504
+ t.variableDeclarator(t.identifier('usage'), t.stringLiteral(usageStr)),
505
+ ]));
506
+ const argvParam = t.identifier('argv');
507
+ argvParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
508
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
509
+ t.tsStringKeyword(),
510
+ t.tsUnknownKeyword(),
511
+ ])),
512
+ ])));
513
+ const prompterParam = t.identifier('prompter');
514
+ prompterParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
515
+ const optionsParam = t.identifier('_options');
516
+ optionsParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CLIOptions')));
517
+ const mainBody = [
518
+ t.ifStatement(t.logicalExpression('||', t.memberExpression(t.identifier('argv'), t.identifier('help')), t.memberExpression(t.identifier('argv'), t.identifier('h'))), t.blockStatement([
519
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.identifier('usage')])),
520
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('process'), t.identifier('exit')), [t.numericLiteral(0)])),
521
+ ])),
522
+ t.variableDeclaration('const', [
523
+ t.variableDeclarator(t.identifier('store'), t.callExpression(t.identifier('getStore'), [])),
524
+ ]),
525
+ t.variableDeclaration('const', [
526
+ t.variableDeclarator(t.objectPattern([
527
+ t.objectProperty(t.identifier('first'), t.identifier('subcommand')),
528
+ t.objectProperty(t.identifier('newArgv'), t.identifier('newArgv'), false, true),
529
+ ]), t.callExpression(t.identifier('extractFirst'), [
530
+ t.identifier('argv'),
531
+ ])),
532
+ ]),
533
+ t.ifStatement(t.unaryExpression('!', t.identifier('subcommand')), t.blockStatement([
534
+ t.variableDeclaration('const', [
535
+ t.variableDeclarator(t.identifier('answer'), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('prompter'), t.identifier('prompt')), [
536
+ t.identifier('argv'),
537
+ t.arrayExpression([
538
+ t.objectExpression([
539
+ t.objectProperty(t.identifier('type'), t.stringLiteral('autocomplete')),
540
+ t.objectProperty(t.identifier('name'), t.stringLiteral('subcommand')),
541
+ t.objectProperty(t.identifier('message'), t.stringLiteral('What do you want to do?')),
542
+ t.objectProperty(t.identifier('options'), t.arrayExpression([
543
+ t.stringLiteral('set-token'),
544
+ t.stringLiteral('status'),
545
+ t.stringLiteral('logout'),
546
+ ])),
547
+ ]),
548
+ ]),
549
+ ]))),
550
+ ]),
551
+ t.returnStatement(t.callExpression(t.identifier('handleAuthSubcommand'), [
552
+ t.memberExpression(t.identifier('answer'), t.identifier('subcommand')),
553
+ t.identifier('newArgv'),
554
+ t.identifier('prompter'),
555
+ t.identifier('store'),
556
+ ])),
557
+ ])),
558
+ t.returnStatement(t.callExpression(t.identifier('handleAuthSubcommand'), [
559
+ t.identifier('subcommand'),
560
+ t.identifier('newArgv'),
561
+ t.identifier('prompter'),
562
+ t.identifier('store'),
563
+ ])),
564
+ ];
565
+ statements.push(t.exportDefaultDeclaration(t.arrowFunctionExpression([argvParam, prompterParam, optionsParam], t.blockStatement(mainBody), true)));
566
+ const subcmdParam = t.identifier('subcommand');
567
+ subcmdParam.typeAnnotation = t.tsTypeAnnotation(t.tsStringKeyword());
568
+ const argvParam2 = t.identifier('argv');
569
+ argvParam2.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
570
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
571
+ t.tsStringKeyword(),
572
+ t.tsUnknownKeyword(),
573
+ ])),
574
+ ])));
575
+ const prompterParam2 = t.identifier('prompter');
576
+ prompterParam2.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
577
+ const storeParam = t.identifier('store');
578
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
579
+ t.tsTypeQuery(t.identifier('getStore')),
580
+ ])));
581
+ const handleAuthSubcommandFunc = t.functionDeclaration(t.identifier('handleAuthSubcommand'), [subcmdParam, argvParam2, prompterParam2, storeParam], t.blockStatement([
582
+ t.switchStatement(t.identifier('subcommand'), [
583
+ buildSwitchCase('set-token', 'handleSetToken', [
584
+ t.identifier('argv'),
585
+ t.identifier('prompter'),
586
+ t.identifier('store'),
587
+ ]),
588
+ buildSwitchCase('status', 'handleStatus', [t.identifier('store')]),
589
+ buildSwitchCase('logout', 'handleLogout', [
590
+ t.identifier('argv'),
591
+ t.identifier('prompter'),
592
+ t.identifier('store'),
593
+ ]),
594
+ buildDefaultSwitchCase('usage'),
595
+ ]),
596
+ ]), false, true);
597
+ statements.push(handleAuthSubcommandFunc);
598
+ statements.push(buildSetTokenHandler());
599
+ statements.push(buildStatusHandler());
600
+ statements.push(buildLogoutHandler());
601
+ const header = getGeneratedFileHeader('Authentication commands');
602
+ const code = generateCode(statements);
603
+ return {
604
+ fileName: 'commands/auth.ts',
605
+ content: header + '\n' + code,
606
+ };
607
+ }
608
+ function buildSetTokenHandler() {
609
+ const argvParam = t.identifier('argv');
610
+ argvParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
611
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
612
+ t.tsStringKeyword(),
613
+ t.tsUnknownKeyword(),
614
+ ])),
615
+ ])));
616
+ const prompterParam = t.identifier('prompter');
617
+ prompterParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
618
+ const storeParam = t.identifier('store');
619
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
620
+ t.tsTypeQuery(t.identifier('getStore')),
621
+ ])));
622
+ const body = [
623
+ t.variableDeclaration('const', [
624
+ t.variableDeclarator(t.identifier('current'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('getCurrentContext')), [])),
625
+ ]),
626
+ t.ifStatement(t.unaryExpression('!', t.identifier('current')), t.blockStatement([
627
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('error')), [t.stringLiteral('No active context. Run "context create" first.')])),
628
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('process'), t.identifier('exit')), [t.numericLiteral(1)])),
629
+ ])),
630
+ t.variableDeclaration('const', [
631
+ t.variableDeclarator(t.objectPattern([
632
+ t.objectProperty(t.identifier('first'), t.identifier('token')),
633
+ ]), t.callExpression(t.identifier('extractFirst'), [
634
+ t.identifier('argv'),
635
+ ])),
636
+ ]),
637
+ t.variableDeclaration('let', [
638
+ t.variableDeclarator(t.identifier('tokenValue'), t.identifier('token')),
639
+ ]),
640
+ t.ifStatement(t.unaryExpression('!', t.identifier('tokenValue')), t.blockStatement([
641
+ t.variableDeclaration('const', [
642
+ t.variableDeclarator(t.identifier('answer'), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('prompter'), t.identifier('prompt')), [
643
+ t.identifier('argv'),
644
+ t.arrayExpression([
645
+ t.objectExpression([
646
+ t.objectProperty(t.identifier('type'), t.stringLiteral('password')),
647
+ t.objectProperty(t.identifier('name'), t.stringLiteral('token')),
648
+ t.objectProperty(t.identifier('message'), t.stringLiteral('API Token')),
649
+ t.objectProperty(t.identifier('required'), t.booleanLiteral(true)),
650
+ ]),
651
+ ]),
652
+ ]))),
653
+ ]),
654
+ t.expressionStatement(t.assignmentExpression('=', t.identifier('tokenValue'), t.memberExpression(t.identifier('answer'), t.identifier('token')))),
655
+ ])),
656
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('setCredentials')), [
657
+ t.memberExpression(t.identifier('current'), t.identifier('name')),
658
+ t.objectExpression([
659
+ t.objectProperty(t.identifier('token'), t.callExpression(t.memberExpression(t.callExpression(t.memberExpression(t.identifier('String'), t.identifier('call')), [
660
+ t.logicalExpression('||', t.identifier('tokenValue'), t.stringLiteral('')),
661
+ ]), t.identifier('trim')), [])),
662
+ ]),
663
+ ])),
664
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
665
+ t.templateLiteral([
666
+ t.templateElement({ raw: 'Token saved for context: ', cooked: 'Token saved for context: ' }),
667
+ t.templateElement({ raw: '', cooked: '' }, true),
668
+ ], [
669
+ t.memberExpression(t.identifier('current'), t.identifier('name')),
670
+ ]),
671
+ ])),
672
+ ];
673
+ const func = t.functionDeclaration(t.identifier('handleSetToken'), [argvParam, prompterParam, storeParam], t.blockStatement(body), false, true);
674
+ return func;
675
+ }
676
+ function buildStatusHandler() {
677
+ const storeParam = t.identifier('store');
678
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
679
+ t.tsTypeQuery(t.identifier('getStore')),
680
+ ])));
681
+ const body = [
682
+ t.variableDeclaration('const', [
683
+ t.variableDeclarator(t.identifier('contexts'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('listContexts')), [])),
684
+ ]),
685
+ t.variableDeclaration('const', [
686
+ t.variableDeclarator(t.identifier('settings'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('loadSettings')), [])),
687
+ ]),
688
+ t.ifStatement(t.binaryExpression('===', t.memberExpression(t.identifier('contexts'), t.identifier('length')), t.numericLiteral(0)), t.blockStatement([
689
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.stringLiteral('No contexts configured.')])),
690
+ t.returnStatement(),
691
+ ])),
692
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.stringLiteral('Authentication Status:')])),
693
+ t.forOfStatement(t.variableDeclaration('const', [
694
+ t.variableDeclarator(t.identifier('ctx')),
695
+ ]), t.identifier('contexts'), t.blockStatement([
696
+ t.variableDeclaration('const', [
697
+ t.variableDeclarator(t.identifier('isCurrent'), t.binaryExpression('===', t.memberExpression(t.identifier('ctx'), t.identifier('name')), t.memberExpression(t.identifier('settings'), t.identifier('currentContext')))),
698
+ ]),
699
+ t.variableDeclaration('const', [
700
+ t.variableDeclarator(t.identifier('hasAuth'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('hasValidCredentials')), [
701
+ t.memberExpression(t.identifier('ctx'), t.identifier('name')),
702
+ ])),
703
+ ]),
704
+ t.variableDeclaration('const', [
705
+ t.variableDeclarator(t.identifier('marker'), t.conditionalExpression(t.identifier('isCurrent'), t.stringLiteral('* '), t.stringLiteral(' '))),
706
+ ]),
707
+ t.variableDeclaration('const', [
708
+ t.variableDeclarator(t.identifier('status'), t.conditionalExpression(t.identifier('hasAuth'), t.stringLiteral('authenticated'), t.stringLiteral('no token'))),
709
+ ]),
710
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
711
+ t.templateLiteral([
712
+ t.templateElement({ raw: '', cooked: '' }),
713
+ t.templateElement({ raw: '', cooked: '' }),
714
+ t.templateElement({ raw: ' [', cooked: ' [' }),
715
+ t.templateElement({ raw: ']', cooked: ']' }, true),
716
+ ], [
717
+ t.identifier('marker'),
718
+ t.memberExpression(t.identifier('ctx'), t.identifier('name')),
719
+ t.identifier('status'),
720
+ ]),
721
+ ])),
722
+ ])),
723
+ ];
724
+ const func = t.functionDeclaration(t.identifier('handleStatus'), [storeParam], t.blockStatement(body));
725
+ return func;
726
+ }
727
+ function buildLogoutHandler() {
728
+ const argvParam = t.identifier('argv');
729
+ argvParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
730
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
731
+ t.tsStringKeyword(),
732
+ t.tsUnknownKeyword(),
733
+ ])),
734
+ ])));
735
+ const prompterParam = t.identifier('prompter');
736
+ prompterParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
737
+ const storeParam = t.identifier('store');
738
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
739
+ t.tsTypeQuery(t.identifier('getStore')),
740
+ ])));
741
+ const body = [
742
+ t.variableDeclaration('const', [
743
+ t.variableDeclarator(t.identifier('current'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('getCurrentContext')), [])),
744
+ ]),
745
+ t.ifStatement(t.unaryExpression('!', t.identifier('current')), t.blockStatement([
746
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.stringLiteral('No active context.')])),
747
+ t.returnStatement(),
748
+ ])),
749
+ t.variableDeclaration('const', [
750
+ t.variableDeclarator(t.identifier('confirm'), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('prompter'), t.identifier('prompt')), [
751
+ t.identifier('argv'),
752
+ t.arrayExpression([
753
+ t.objectExpression([
754
+ t.objectProperty(t.identifier('type'), t.stringLiteral('confirm')),
755
+ t.objectProperty(t.identifier('name'), t.stringLiteral('confirm')),
756
+ t.objectProperty(t.identifier('message'), t.templateLiteral([
757
+ t.templateElement({
758
+ raw: 'Remove credentials for "',
759
+ cooked: 'Remove credentials for "',
760
+ }),
761
+ t.templateElement({
762
+ raw: '"?',
763
+ cooked: '"?',
764
+ }, true),
765
+ ], [
766
+ t.memberExpression(t.identifier('current'), t.identifier('name')),
767
+ ])),
768
+ t.objectProperty(t.identifier('default'), t.booleanLiteral(false)),
769
+ ]),
770
+ ]),
771
+ ]))),
772
+ ]),
773
+ t.ifStatement(t.unaryExpression('!', t.memberExpression(t.identifier('confirm'), t.identifier('confirm'))), t.blockStatement([t.returnStatement()])),
774
+ t.ifStatement(t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('removeCredentials')), [
775
+ t.memberExpression(t.identifier('current'), t.identifier('name')),
776
+ ]), t.blockStatement([
777
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
778
+ t.templateLiteral([
779
+ t.templateElement({ raw: 'Credentials removed for: ', cooked: 'Credentials removed for: ' }),
780
+ t.templateElement({ raw: '', cooked: '' }, true),
781
+ ], [
782
+ t.memberExpression(t.identifier('current'), t.identifier('name')),
783
+ ]),
784
+ ])),
785
+ ]), t.blockStatement([
786
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
787
+ t.templateLiteral([
788
+ t.templateElement({ raw: 'No credentials found for: ', cooked: 'No credentials found for: ' }),
789
+ t.templateElement({ raw: '', cooked: '' }, true),
790
+ ], [
791
+ t.memberExpression(t.identifier('current'), t.identifier('name')),
792
+ ]),
793
+ ])),
794
+ ])),
795
+ ];
796
+ const func = t.functionDeclaration(t.identifier('handleLogout'), [argvParam, prompterParam, storeParam], t.blockStatement(body), false, true);
797
+ return func;
798
+ }
799
+ export function generateMultiTargetContextCommand(toolName, commandName, targets) {
800
+ const statements = [];
801
+ statements.push(createImportDeclaration('inquirerer', [
802
+ 'CLIOptions',
803
+ 'Inquirerer',
804
+ 'extractFirst',
805
+ ]));
806
+ statements.push(createImportDeclaration('../executor', ['getStore']));
807
+ const usageStr = `
808
+ ${toolName} ${commandName} <command>
809
+
810
+ Commands:
811
+ create <name> Create a new context
812
+ list List all contexts
813
+ use <name> Set the active context
814
+ current Show current context
815
+ delete <name> Delete a context
816
+
817
+ Create Options:
818
+ ${targets.map((tgt) => ` --${tgt.name}-endpoint <url> ${tgt.name} endpoint (default: ${tgt.endpoint})`).join('\n')}
819
+
820
+ --help, -h Show this help message
821
+ `;
822
+ statements.push(t.variableDeclaration('const', [
823
+ t.variableDeclarator(t.identifier('usage'), t.stringLiteral(usageStr)),
824
+ ]));
825
+ const argvParam = t.identifier('argv');
826
+ argvParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
827
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
828
+ t.tsStringKeyword(),
829
+ t.tsUnknownKeyword(),
830
+ ])),
831
+ ])));
832
+ const prompterParam = t.identifier('prompter');
833
+ prompterParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
834
+ const optionsParam = t.identifier('_options');
835
+ optionsParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CLIOptions')));
836
+ const mainBody = [
837
+ t.ifStatement(t.logicalExpression('||', t.memberExpression(t.identifier('argv'), t.identifier('help')), t.memberExpression(t.identifier('argv'), t.identifier('h'))), t.blockStatement([
838
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.identifier('usage')])),
839
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('process'), t.identifier('exit')), [t.numericLiteral(0)])),
840
+ ])),
841
+ t.variableDeclaration('const', [
842
+ t.variableDeclarator(t.identifier('store'), t.callExpression(t.identifier('getStore'), [])),
843
+ ]),
844
+ t.variableDeclaration('const', [
845
+ t.variableDeclarator(t.objectPattern([
846
+ t.objectProperty(t.identifier('first'), t.identifier('subcommand')),
847
+ t.objectProperty(t.identifier('newArgv'), t.identifier('newArgv'), false, true),
848
+ ]), t.callExpression(t.identifier('extractFirst'), [
849
+ t.identifier('argv'),
850
+ ])),
851
+ ]),
852
+ t.ifStatement(t.unaryExpression('!', t.identifier('subcommand')), t.blockStatement([
853
+ t.variableDeclaration('const', [
854
+ t.variableDeclarator(t.identifier('answer'), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('prompter'), t.identifier('prompt')), [
855
+ t.identifier('argv'),
856
+ t.arrayExpression([
857
+ t.objectExpression([
858
+ t.objectProperty(t.identifier('type'), t.stringLiteral('autocomplete')),
859
+ t.objectProperty(t.identifier('name'), t.stringLiteral('subcommand')),
860
+ t.objectProperty(t.identifier('message'), t.stringLiteral('What do you want to do?')),
861
+ t.objectProperty(t.identifier('options'), t.arrayExpression([
862
+ t.stringLiteral('create'),
863
+ t.stringLiteral('list'),
864
+ t.stringLiteral('use'),
865
+ t.stringLiteral('current'),
866
+ t.stringLiteral('delete'),
867
+ ])),
868
+ ]),
869
+ ]),
870
+ ]))),
871
+ ]),
872
+ t.returnStatement(t.callExpression(t.identifier('handleSubcommand'), [
873
+ t.memberExpression(t.identifier('answer'), t.identifier('subcommand')),
874
+ t.identifier('newArgv'),
875
+ t.identifier('prompter'),
876
+ t.identifier('store'),
877
+ ])),
878
+ ])),
879
+ t.returnStatement(t.callExpression(t.identifier('handleSubcommand'), [
880
+ t.identifier('subcommand'),
881
+ t.identifier('newArgv'),
882
+ t.identifier('prompter'),
883
+ t.identifier('store'),
884
+ ])),
885
+ ];
886
+ const mainExport = t.exportDefaultDeclaration(t.arrowFunctionExpression([argvParam, prompterParam, optionsParam], t.blockStatement(mainBody), true));
887
+ statements.push(mainExport);
888
+ const subcmdParam = t.identifier('subcommand');
889
+ subcmdParam.typeAnnotation = t.tsTypeAnnotation(t.tsStringKeyword());
890
+ const argvParam2 = t.identifier('argv');
891
+ argvParam2.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
892
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
893
+ t.tsStringKeyword(),
894
+ t.tsUnknownKeyword(),
895
+ ])),
896
+ ])));
897
+ const prompterParam2 = t.identifier('prompter');
898
+ prompterParam2.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
899
+ const storeParam = t.identifier('store');
900
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
901
+ t.tsTypeQuery(t.identifier('getStore')),
902
+ ])));
903
+ const handleSubcommandFunc = t.functionDeclaration(t.identifier('handleSubcommand'), [subcmdParam, argvParam2, prompterParam2, storeParam], t.blockStatement([
904
+ t.switchStatement(t.identifier('subcommand'), [
905
+ buildSwitchCase('create', 'handleCreate', [
906
+ t.identifier('argv'),
907
+ t.identifier('prompter'),
908
+ t.identifier('store'),
909
+ ]),
910
+ buildSwitchCase('list', 'handleList', [t.identifier('store')]),
911
+ buildSwitchCase('use', 'handleUse', [
912
+ t.identifier('argv'),
913
+ t.identifier('prompter'),
914
+ t.identifier('store'),
915
+ ]),
916
+ buildSwitchCase('current', 'handleCurrent', [t.identifier('store')]),
917
+ buildSwitchCase('delete', 'handleDelete', [
918
+ t.identifier('argv'),
919
+ t.identifier('prompter'),
920
+ t.identifier('store'),
921
+ ]),
922
+ buildDefaultSwitchCase('usage'),
923
+ ]),
924
+ ]), false, true);
925
+ statements.push(handleSubcommandFunc);
926
+ statements.push(buildMultiTargetCreateHandler(targets));
927
+ statements.push(buildListHandler());
928
+ statements.push(buildUseHandler());
929
+ statements.push(buildCurrentHandler());
930
+ statements.push(buildDeleteHandler());
931
+ const header = getGeneratedFileHeader('Multi-target context management commands');
932
+ const code = generateCode(statements);
933
+ return {
934
+ fileName: `commands/${commandName}.ts`,
935
+ content: header + '\n' + code,
936
+ };
937
+ }
938
+ function buildMultiTargetCreateHandler(targets) {
939
+ const argvParam = t.identifier('argv');
940
+ argvParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
941
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
942
+ t.tsStringKeyword(),
943
+ t.tsUnknownKeyword(),
944
+ ])),
945
+ ])));
946
+ const prompterParam = t.identifier('prompter');
947
+ prompterParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
948
+ const storeParam = t.identifier('store');
949
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
950
+ t.tsTypeQuery(t.identifier('getStore')),
951
+ ])));
952
+ const promptQuestions = [
953
+ t.objectExpression([
954
+ t.objectProperty(t.identifier('type'), t.stringLiteral('text')),
955
+ t.objectProperty(t.identifier('name'), t.stringLiteral('name')),
956
+ t.objectProperty(t.identifier('message'), t.stringLiteral('Context name')),
957
+ t.objectProperty(t.identifier('required'), t.booleanLiteral(true)),
958
+ ]),
959
+ ];
960
+ for (const target of targets) {
961
+ const fieldName = `${target.name}Endpoint`;
962
+ promptQuestions.push(t.objectExpression([
963
+ t.objectProperty(t.identifier('type'), t.stringLiteral('text')),
964
+ t.objectProperty(t.identifier('name'), t.stringLiteral(fieldName)),
965
+ t.objectProperty(t.identifier('message'), t.stringLiteral(`${target.name} endpoint`)),
966
+ t.objectProperty(t.identifier('default'), t.stringLiteral(target.endpoint)),
967
+ ]));
968
+ }
969
+ const targetsObjProps = targets.map((target) => {
970
+ const fieldName = `${target.name}Endpoint`;
971
+ return t.objectProperty(t.stringLiteral(target.name), t.objectExpression([
972
+ t.objectProperty(t.identifier('endpoint'), t.memberExpression(t.identifier('answers'), t.identifier(fieldName))),
973
+ ]));
974
+ });
975
+ const body = [
976
+ t.variableDeclaration('const', [
977
+ t.variableDeclarator(t.objectPattern([
978
+ t.objectProperty(t.identifier('first'), t.identifier('name')),
979
+ t.objectProperty(t.identifier('newArgv'), t.identifier('restArgv')),
980
+ ]), t.callExpression(t.identifier('extractFirst'), [
981
+ t.identifier('argv'),
982
+ ])),
983
+ ]),
984
+ t.variableDeclaration('const', [
985
+ t.variableDeclarator(t.identifier('answers'), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('prompter'), t.identifier('prompt')), [
986
+ t.objectExpression([
987
+ t.objectProperty(t.identifier('name'), t.identifier('name'), false, true),
988
+ t.spreadElement(t.identifier('restArgv')),
989
+ ]),
990
+ t.arrayExpression(promptQuestions),
991
+ ]))),
992
+ ]),
993
+ t.variableDeclaration('const', [
994
+ t.variableDeclarator(t.identifier('contextName'), t.memberExpression(t.identifier('answers'), t.identifier('name'))),
995
+ ]),
996
+ t.variableDeclaration('const', [
997
+ t.variableDeclarator(t.identifier('targets'), t.objectExpression(targetsObjProps)),
998
+ ]),
999
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('createContext')), [
1000
+ t.identifier('contextName'),
1001
+ t.objectExpression([
1002
+ t.objectProperty(t.identifier('endpoint'), t.memberExpression(t.identifier('answers'), t.identifier(`${targets[0].name}Endpoint`))),
1003
+ t.objectProperty(t.identifier('targets'), t.identifier('targets')),
1004
+ ]),
1005
+ ])),
1006
+ t.variableDeclaration('const', [
1007
+ t.variableDeclarator(t.identifier('settings'), t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('loadSettings')), [])),
1008
+ ]),
1009
+ t.ifStatement(t.unaryExpression('!', t.memberExpression(t.identifier('settings'), t.identifier('currentContext'))), t.blockStatement([
1010
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('store'), t.identifier('setCurrentContext')), [t.identifier('contextName')])),
1011
+ ])),
1012
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
1013
+ t.templateLiteral([
1014
+ t.templateElement({ raw: 'Created context: ', cooked: 'Created context: ' }),
1015
+ t.templateElement({ raw: '', cooked: '' }, true),
1016
+ ], [t.identifier('contextName')]),
1017
+ ])),
1018
+ ];
1019
+ for (const target of targets) {
1020
+ const fieldName = `${target.name}Endpoint`;
1021
+ body.push(t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [
1022
+ t.templateLiteral([
1023
+ t.templateElement({ raw: ` ${target.name}: `, cooked: ` ${target.name}: ` }),
1024
+ t.templateElement({ raw: '', cooked: '' }, true),
1025
+ ], [t.memberExpression(t.identifier('answers'), t.identifier(fieldName))]),
1026
+ ])));
1027
+ }
1028
+ const func = t.functionDeclaration(t.identifier('handleCreate'), [argvParam, prompterParam, storeParam], t.blockStatement(body), false, true);
1029
+ return func;
1030
+ }
1031
+ export function generateAuthCommandWithName(toolName, commandName) {
1032
+ const statements = [];
1033
+ statements.push(createImportDeclaration('inquirerer', [
1034
+ 'CLIOptions',
1035
+ 'Inquirerer',
1036
+ 'extractFirst',
1037
+ ]));
1038
+ statements.push(createImportDeclaration('../executor', ['getStore']));
1039
+ const usageStr = `
1040
+ ${toolName} ${commandName} <command>
1041
+
1042
+ Commands:
1043
+ set-token <token> Set API token for the current context
1044
+ status Show authentication status
1045
+ logout Remove credentials for the current context
1046
+
1047
+ Options:
1048
+ --context <name> Specify context (defaults to current context)
1049
+
1050
+ --help, -h Show this help message
1051
+ `;
1052
+ statements.push(t.variableDeclaration('const', [
1053
+ t.variableDeclarator(t.identifier('usage'), t.stringLiteral(usageStr)),
1054
+ ]));
1055
+ const argvParam = t.identifier('argv');
1056
+ argvParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
1057
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
1058
+ t.tsStringKeyword(),
1059
+ t.tsUnknownKeyword(),
1060
+ ])),
1061
+ ])));
1062
+ const prompterParam = t.identifier('prompter');
1063
+ prompterParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
1064
+ const optionsParam = t.identifier('_options');
1065
+ optionsParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CLIOptions')));
1066
+ const mainBody = [
1067
+ t.ifStatement(t.logicalExpression('||', t.memberExpression(t.identifier('argv'), t.identifier('help')), t.memberExpression(t.identifier('argv'), t.identifier('h'))), t.blockStatement([
1068
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('console'), t.identifier('log')), [t.identifier('usage')])),
1069
+ t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('process'), t.identifier('exit')), [t.numericLiteral(0)])),
1070
+ ])),
1071
+ t.variableDeclaration('const', [
1072
+ t.variableDeclarator(t.identifier('store'), t.callExpression(t.identifier('getStore'), [])),
1073
+ ]),
1074
+ t.variableDeclaration('const', [
1075
+ t.variableDeclarator(t.objectPattern([
1076
+ t.objectProperty(t.identifier('first'), t.identifier('subcommand')),
1077
+ t.objectProperty(t.identifier('newArgv'), t.identifier('newArgv'), false, true),
1078
+ ]), t.callExpression(t.identifier('extractFirst'), [
1079
+ t.identifier('argv'),
1080
+ ])),
1081
+ ]),
1082
+ t.ifStatement(t.unaryExpression('!', t.identifier('subcommand')), t.blockStatement([
1083
+ t.variableDeclaration('const', [
1084
+ t.variableDeclarator(t.identifier('answer'), t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('prompter'), t.identifier('prompt')), [
1085
+ t.identifier('argv'),
1086
+ t.arrayExpression([
1087
+ t.objectExpression([
1088
+ t.objectProperty(t.identifier('type'), t.stringLiteral('autocomplete')),
1089
+ t.objectProperty(t.identifier('name'), t.stringLiteral('subcommand')),
1090
+ t.objectProperty(t.identifier('message'), t.stringLiteral('What do you want to do?')),
1091
+ t.objectProperty(t.identifier('options'), t.arrayExpression([
1092
+ t.stringLiteral('set-token'),
1093
+ t.stringLiteral('status'),
1094
+ t.stringLiteral('logout'),
1095
+ ])),
1096
+ ]),
1097
+ ]),
1098
+ ]))),
1099
+ ]),
1100
+ t.returnStatement(t.callExpression(t.identifier('handleAuthSubcommand'), [
1101
+ t.memberExpression(t.identifier('answer'), t.identifier('subcommand')),
1102
+ t.identifier('newArgv'),
1103
+ t.identifier('prompter'),
1104
+ t.identifier('store'),
1105
+ ])),
1106
+ ])),
1107
+ t.returnStatement(t.callExpression(t.identifier('handleAuthSubcommand'), [
1108
+ t.identifier('subcommand'),
1109
+ t.identifier('newArgv'),
1110
+ t.identifier('prompter'),
1111
+ t.identifier('store'),
1112
+ ])),
1113
+ ];
1114
+ statements.push(t.exportDefaultDeclaration(t.arrowFunctionExpression([argvParam, prompterParam, optionsParam], t.blockStatement(mainBody), true)));
1115
+ const subcmdParam = t.identifier('subcommand');
1116
+ subcmdParam.typeAnnotation = t.tsTypeAnnotation(t.tsStringKeyword());
1117
+ const argvParam2 = t.identifier('argv');
1118
+ argvParam2.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Partial'), t.tsTypeParameterInstantiation([
1119
+ t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
1120
+ t.tsStringKeyword(),
1121
+ t.tsUnknownKeyword(),
1122
+ ])),
1123
+ ])));
1124
+ const prompterParam2 = t.identifier('prompter');
1125
+ prompterParam2.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Inquirerer')));
1126
+ const storeParam = t.identifier('store');
1127
+ storeParam.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('ReturnType'), t.tsTypeParameterInstantiation([
1128
+ t.tsTypeQuery(t.identifier('getStore')),
1129
+ ])));
1130
+ const handleAuthSubcommandFunc = t.functionDeclaration(t.identifier('handleAuthSubcommand'), [subcmdParam, argvParam2, prompterParam2, storeParam], t.blockStatement([
1131
+ t.switchStatement(t.identifier('subcommand'), [
1132
+ buildSwitchCase('set-token', 'handleSetToken', [
1133
+ t.identifier('argv'),
1134
+ t.identifier('prompter'),
1135
+ t.identifier('store'),
1136
+ ]),
1137
+ buildSwitchCase('status', 'handleStatus', [t.identifier('store')]),
1138
+ buildSwitchCase('logout', 'handleLogout', [
1139
+ t.identifier('argv'),
1140
+ t.identifier('prompter'),
1141
+ t.identifier('store'),
1142
+ ]),
1143
+ buildDefaultSwitchCase('usage'),
1144
+ ]),
1145
+ ]), false, true);
1146
+ statements.push(handleAuthSubcommandFunc);
1147
+ statements.push(buildSetTokenHandler());
1148
+ statements.push(buildStatusHandler());
1149
+ statements.push(buildLogoutHandler());
1150
+ const header = getGeneratedFileHeader('Authentication commands');
1151
+ const code = generateCode(statements);
1152
+ return {
1153
+ fileName: `commands/${commandName}.ts`,
1154
+ content: header + '\n' + code,
1155
+ };
1156
+ }