@constructive-io/graphql-codegen 4.0.2 → 4.1.1

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