@geekmidas/cli 0.9.0 → 0.12.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 (146) hide show
  1. package/README.md +525 -0
  2. package/dist/bundler-DRXCw_YR.mjs +70 -0
  3. package/dist/bundler-DRXCw_YR.mjs.map +1 -0
  4. package/dist/bundler-WsEvH_b2.cjs +71 -0
  5. package/dist/bundler-WsEvH_b2.cjs.map +1 -0
  6. package/dist/{config-CFls09Ey.cjs → config-AmInkU7k.cjs} +10 -8
  7. package/dist/config-AmInkU7k.cjs.map +1 -0
  8. package/dist/{config-Bq72aj8e.mjs → config-DYULeEv8.mjs} +6 -4
  9. package/dist/config-DYULeEv8.mjs.map +1 -0
  10. package/dist/config.cjs +1 -1
  11. package/dist/config.d.cts +2 -1
  12. package/dist/config.d.cts.map +1 -0
  13. package/dist/config.d.mts +2 -1
  14. package/dist/config.d.mts.map +1 -0
  15. package/dist/config.mjs +1 -1
  16. package/dist/encryption-C8H-38Yy.mjs +42 -0
  17. package/dist/encryption-C8H-38Yy.mjs.map +1 -0
  18. package/dist/encryption-Dyf_r1h-.cjs +44 -0
  19. package/dist/encryption-Dyf_r1h-.cjs.map +1 -0
  20. package/dist/index.cjs +2125 -184
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.mjs +2143 -197
  23. package/dist/index.mjs.map +1 -1
  24. package/dist/{openapi--vOy9mo4.mjs → openapi-BfFlOBCG.mjs} +812 -49
  25. package/dist/openapi-BfFlOBCG.mjs.map +1 -0
  26. package/dist/{openapi-CHhTPief.cjs → openapi-Bt_1FDpT.cjs} +805 -42
  27. package/dist/openapi-Bt_1FDpT.cjs.map +1 -0
  28. package/dist/{openapi-react-query-o5iMi8tz.cjs → openapi-react-query-B-sNWHFU.cjs} +5 -5
  29. package/dist/openapi-react-query-B-sNWHFU.cjs.map +1 -0
  30. package/dist/{openapi-react-query-CcciaVu5.mjs → openapi-react-query-B6XTeGqS.mjs} +5 -5
  31. package/dist/openapi-react-query-B6XTeGqS.mjs.map +1 -0
  32. package/dist/openapi-react-query.cjs +1 -1
  33. package/dist/openapi-react-query.d.cts.map +1 -0
  34. package/dist/openapi-react-query.d.mts.map +1 -0
  35. package/dist/openapi-react-query.mjs +1 -1
  36. package/dist/openapi.cjs +2 -2
  37. package/dist/openapi.d.cts +1 -1
  38. package/dist/openapi.d.cts.map +1 -0
  39. package/dist/openapi.d.mts +1 -1
  40. package/dist/openapi.d.mts.map +1 -0
  41. package/dist/openapi.mjs +2 -2
  42. package/dist/storage-BUYQJgz7.cjs +4 -0
  43. package/dist/storage-BXoJvmv2.cjs +149 -0
  44. package/dist/storage-BXoJvmv2.cjs.map +1 -0
  45. package/dist/storage-C9PU_30f.mjs +101 -0
  46. package/dist/storage-C9PU_30f.mjs.map +1 -0
  47. package/dist/storage-DLJAYxzJ.mjs +3 -0
  48. package/dist/{types-b-vwGpqc.d.cts → types-BR0M2v_c.d.mts} +100 -1
  49. package/dist/types-BR0M2v_c.d.mts.map +1 -0
  50. package/dist/{types-DXgiA1sF.d.mts → types-BhkZc-vm.d.cts} +100 -1
  51. package/dist/types-BhkZc-vm.d.cts.map +1 -0
  52. package/examples/cron-example.ts +27 -27
  53. package/examples/env.ts +27 -27
  54. package/examples/function-example.ts +31 -31
  55. package/examples/gkm.config.json +20 -20
  56. package/examples/gkm.config.ts +8 -8
  57. package/examples/gkm.minimal.config.json +5 -5
  58. package/examples/gkm.production.config.json +25 -25
  59. package/examples/logger.ts +2 -2
  60. package/package.json +6 -6
  61. package/src/__tests__/EndpointGenerator.hooks.spec.ts +191 -191
  62. package/src/__tests__/config.spec.ts +55 -55
  63. package/src/__tests__/loadEnvFiles.spec.ts +93 -93
  64. package/src/__tests__/normalizeHooksConfig.spec.ts +58 -58
  65. package/src/__tests__/openapi-react-query.spec.ts +497 -497
  66. package/src/__tests__/openapi.spec.ts +428 -428
  67. package/src/__tests__/test-helpers.ts +77 -76
  68. package/src/auth/__tests__/credentials.spec.ts +204 -0
  69. package/src/auth/__tests__/index.spec.ts +168 -0
  70. package/src/auth/credentials.ts +187 -0
  71. package/src/auth/index.ts +226 -0
  72. package/src/build/__tests__/index-new.spec.ts +474 -474
  73. package/src/build/__tests__/manifests.spec.ts +333 -333
  74. package/src/build/bundler.ts +141 -0
  75. package/src/build/endpoint-analyzer.ts +236 -0
  76. package/src/build/handler-templates.ts +1253 -0
  77. package/src/build/index.ts +250 -179
  78. package/src/build/manifests.ts +52 -52
  79. package/src/build/providerResolver.ts +145 -145
  80. package/src/build/types.ts +64 -43
  81. package/src/config.ts +39 -37
  82. package/src/deploy/__tests__/docker.spec.ts +111 -0
  83. package/src/deploy/__tests__/dokploy.spec.ts +245 -0
  84. package/src/deploy/__tests__/init.spec.ts +662 -0
  85. package/src/deploy/docker.ts +128 -0
  86. package/src/deploy/dokploy.ts +204 -0
  87. package/src/deploy/index.ts +136 -0
  88. package/src/deploy/init.ts +484 -0
  89. package/src/deploy/types.ts +48 -0
  90. package/src/dev/__tests__/index.spec.ts +266 -266
  91. package/src/dev/index.ts +647 -593
  92. package/src/docker/__tests__/compose.spec.ts +531 -0
  93. package/src/docker/__tests__/templates.spec.ts +280 -0
  94. package/src/docker/compose.ts +273 -0
  95. package/src/docker/index.ts +230 -0
  96. package/src/docker/templates.ts +446 -0
  97. package/src/generators/CronGenerator.ts +72 -72
  98. package/src/generators/EndpointGenerator.ts +699 -398
  99. package/src/generators/FunctionGenerator.ts +84 -84
  100. package/src/generators/Generator.ts +72 -72
  101. package/src/generators/OpenApiTsGenerator.ts +589 -589
  102. package/src/generators/SubscriberGenerator.ts +124 -124
  103. package/src/generators/__tests__/CronGenerator.spec.ts +433 -433
  104. package/src/generators/__tests__/EndpointGenerator.spec.ts +532 -382
  105. package/src/generators/__tests__/FunctionGenerator.spec.ts +244 -244
  106. package/src/generators/__tests__/SubscriberGenerator.spec.ts +397 -382
  107. package/src/generators/index.ts +4 -4
  108. package/src/index.ts +628 -206
  109. package/src/init/__tests__/generators.spec.ts +334 -334
  110. package/src/init/__tests__/init.spec.ts +332 -332
  111. package/src/init/__tests__/utils.spec.ts +89 -89
  112. package/src/init/generators/config.ts +175 -175
  113. package/src/init/generators/docker.ts +41 -41
  114. package/src/init/generators/env.ts +72 -72
  115. package/src/init/generators/index.ts +1 -1
  116. package/src/init/generators/models.ts +64 -64
  117. package/src/init/generators/monorepo.ts +161 -161
  118. package/src/init/generators/package.ts +71 -71
  119. package/src/init/generators/source.ts +6 -6
  120. package/src/init/index.ts +203 -208
  121. package/src/init/templates/api.ts +115 -115
  122. package/src/init/templates/index.ts +75 -75
  123. package/src/init/templates/minimal.ts +98 -98
  124. package/src/init/templates/serverless.ts +89 -89
  125. package/src/init/templates/worker.ts +98 -98
  126. package/src/init/utils.ts +54 -56
  127. package/src/openapi-react-query.ts +194 -194
  128. package/src/openapi.ts +63 -63
  129. package/src/secrets/__tests__/encryption.spec.ts +226 -0
  130. package/src/secrets/__tests__/generator.spec.ts +319 -0
  131. package/src/secrets/__tests__/index.spec.ts +91 -0
  132. package/src/secrets/__tests__/storage.spec.ts +403 -0
  133. package/src/secrets/encryption.ts +91 -0
  134. package/src/secrets/generator.ts +164 -0
  135. package/src/secrets/index.ts +383 -0
  136. package/src/secrets/storage.ts +134 -0
  137. package/src/secrets/types.ts +53 -0
  138. package/src/types.ts +295 -176
  139. package/tsconfig.json +9 -0
  140. package/tsdown.config.ts +11 -8
  141. package/dist/config-Bq72aj8e.mjs.map +0 -1
  142. package/dist/config-CFls09Ey.cjs.map +0 -1
  143. package/dist/openapi--vOy9mo4.mjs.map +0 -1
  144. package/dist/openapi-CHhTPief.cjs.map +0 -1
  145. package/dist/openapi-react-query-CcciaVu5.mjs.map +0 -1
  146. package/dist/openapi-react-query-o5iMi8tz.cjs.map +0 -1
@@ -1,394 +1,544 @@
1
1
  import { mkdir, readFile } from 'node:fs/promises';
2
2
  import { join } from 'node:path';
3
+ import type { HttpMethod } from '@geekmidas/constructs';
3
4
  import type { Endpoint } from '@geekmidas/constructs/endpoints';
4
5
  import { itWithDir } from '@geekmidas/testkit/os';
5
6
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6
-
7
- import type { HttpMethod } from '@geekmidas/constructs';
8
7
  import {
9
- createMockBuildContext,
10
- createMockEndpointFile,
11
- createTestEndpoint,
8
+ createMockBuildContext,
9
+ createMockEndpointFile,
10
+ createTestEndpoint,
12
11
  } from '../../__tests__/test-helpers';
13
12
  import { EndpointGenerator } from '../EndpointGenerator';
14
13
  import type { GeneratedConstruct } from '../Generator';
15
14
 
16
15
  describe('EndpointGenerator', () => {
17
- let tempDir: string;
18
- let outputDir: string;
19
- let generator: EndpointGenerator;
20
- let context: ReturnType<typeof createMockBuildContext>;
21
-
22
- beforeEach(async () => {
23
- generator = new EndpointGenerator();
24
- context = createMockBuildContext();
25
- });
26
-
27
- afterEach(async () => {});
28
-
29
- it('should identify valid endpoints', () => {
30
- const testEndpoint = createTestEndpoint('/test', 'GET');
31
- expect(generator.isConstruct(testEndpoint)).toBe(true);
32
- });
33
-
34
- it('should reject invalid constructs', () => {
35
- expect(generator.isConstruct({})).toBe(false);
36
- expect(generator.isConstruct('string')).toBe(false);
37
- expect(generator.isConstruct(null)).toBe(false);
38
- });
39
-
40
- const createTestEndpointConstruct = (
41
- key: string,
42
- path: string,
43
- method: HttpMethod,
44
- dir: string,
45
- ): GeneratedConstruct<Endpoint<any, any, any, any, any, any>> => ({
46
- key,
47
- name: key.toLowerCase(),
48
- construct: createTestEndpoint(path, method),
49
- path: {
50
- absolute: join(dir, `${key}.ts`),
51
- relative: `./${key}.ts`,
52
- },
53
- });
54
-
55
- itWithDir('should generate a single server app file', async ({ dir }) => {
56
- const outputDir = join(dir, 'output');
57
- const routesDir = join(dir, 'routes');
58
- await mkdir(outputDir, { recursive: true });
59
-
60
- await Promise.all([
61
- createMockEndpointFile(
62
- routesDir,
63
- 'testEndpoint.ts',
64
- 'testEndpoint',
65
- '/test',
66
- 'GET',
67
- ),
68
- createMockEndpointFile(
69
- routesDir,
70
- 'anotherEndpoint.ts',
71
- 'anotherEndpoint',
72
- '/another',
73
- 'POST',
74
- ),
75
- ]);
76
-
77
- const constructs = await generator.load('**/routes/*.ts', dir);
78
-
79
- const routes = await generator.build(context, constructs, outputDir, {
80
- provider: 'server',
81
- enableOpenApi: true,
82
- });
83
-
84
- expect(routes).toHaveLength(1);
85
- expect(routes[0]).toMatchObject({
86
- path: '*',
87
- method: 'ALL',
88
- handler: expect.stringContaining('app.ts'),
89
- });
90
-
91
- // Check that the app.ts file was created
92
- const appPath = join(outputDir, 'app.ts');
93
- const appContent = await readFile(appPath, 'utf-8');
94
-
95
- expect(appContent).toContain('import { setupEndpoints }');
96
- expect(appContent).toContain('import { setupSubscribers }');
97
- expect(appContent).toContain('function createApp');
98
- expect(appContent).toContain('app?: HonoType');
99
- expect(appContent).toContain('enableOpenApi: boolean = true');
100
- expect(appContent).toContain('interface ServerApp');
101
- expect(appContent).toContain('async start(options');
102
- expect(appContent).toContain('serve: (app: HonoType, port: number)');
103
-
104
- // Check that the endpoints.ts file was created with endpoint logic
105
- const endpointsPath = join(outputDir, 'endpoints.ts');
106
- const endpointsContent = await readFile(endpointsPath, 'utf-8');
107
-
108
- expect(endpointsContent).toContain('import { HonoEndpoint }');
109
- expect(endpointsContent).toContain('import { testEndpoint }');
110
- expect(endpointsContent).toContain('import { anotherEndpoint }');
111
- // Function signature always defaults to true
112
- expect(endpointsContent).toContain('enableOpenApi: boolean = true');
113
- // OpenAPI options are configured based on the parameter
114
- expect(endpointsContent).toContain("docsPath: '/__docs'");
115
- });
116
-
117
- itWithDir(
118
- 'should generate server app without OpenAPI when disabled',
119
- async ({ dir }) => {
120
- const outputDir = join(dir, 'output');
121
- const routesDir = join(dir, 'routes');
122
- await mkdir(outputDir, { recursive: true });
123
-
124
- await createMockEndpointFile(
125
- routesDir,
126
- 'testEndpoint.ts',
127
- 'testEndpoint',
128
- '/test',
129
- 'GET',
130
- );
131
-
132
- const constructs = await generator.load('**/routes/*.ts', dir);
133
-
134
- await generator.build(context, constructs, outputDir, {
135
- provider: 'server',
136
- enableOpenApi: false,
137
- });
138
-
139
- const appPath = join(outputDir, 'app.ts');
140
- const appContent = await readFile(appPath, 'utf-8');
141
-
142
- expect(appContent).toContain('function createApp');
143
-
144
- // Check that the endpoints.ts file defaults to true (but can be overridden)
145
- const endpointsPath = join(outputDir, 'endpoints.ts');
146
- const endpointsContent = await readFile(endpointsPath, 'utf-8');
147
-
148
- // The function signature always defaults to true
149
- expect(endpointsContent).toContain('enableOpenApi: boolean = true');
150
- // But the OpenAPI options are configured dynamically based on the parameter
151
- expect(endpointsContent).toContain('docsPath: false');
152
- },
153
- );
154
-
155
- itWithDir(
156
- 'should generate individual handlers in routes subdirectory',
157
- async ({ dir }) => {
158
- const outputDir = join(dir, 'output');
159
- const routesDir = join(dir, 'routes');
160
- await mkdir(outputDir, { recursive: true });
161
-
162
- await Promise.all([
163
- createMockEndpointFile(
164
- routesDir,
165
- 'testEndpoint.ts',
166
- 'testEndpoint',
167
- '/test',
168
- 'GET',
169
- ),
170
- createMockEndpointFile(
171
- routesDir,
172
- 'anotherEndpoint.ts',
173
- 'anotherEndpoint',
174
- '/another',
175
- 'POST',
176
- ),
177
- ]);
178
-
179
- const constructs = await generator.load('**/routes/*.ts', dir);
180
-
181
- const routes = await generator.build(context, constructs, outputDir, {
182
- provider: 'aws-lambda',
183
- });
184
-
185
- expect(routes).toHaveLength(2);
186
-
187
- // Find routes by their path since order may vary
188
- const testRoute = routes.find((r) => r.path === '/test');
189
- const anotherRoute = routes.find((r) => r.path === '/another');
190
-
191
- expect(testRoute).toMatchObject({
192
- path: '/test',
193
- method: 'GET',
194
- handler: expect.stringContaining('routes/testEndpoint.handler'),
195
- });
196
- expect(anotherRoute).toMatchObject({
197
- path: '/another',
198
- method: 'POST',
199
- handler: expect.stringContaining('routes/anotherEndpoint.handler'),
200
- });
201
-
202
- // Check that handler files were created
203
- const testHandlerPath = join(outputDir, 'routes', 'testEndpoint.ts');
204
- const testHandlerContent = await readFile(testHandlerPath, 'utf-8');
205
- expect(testHandlerContent).toContain('AmazonApiGatewayV2Endpoint');
206
- expect(testHandlerContent).toContain('import { testEndpoint }');
207
-
208
- const anotherHandlerPath = join(
209
- outputDir,
210
- 'routes',
211
- 'anotherEndpoint.ts',
212
- );
213
- const anotherHandlerContent = await readFile(anotherHandlerPath, 'utf-8');
214
- expect(anotherHandlerContent).toContain('AmazonApiGatewayV2Endpoint');
215
- expect(anotherHandlerContent).toContain('import { anotherEndpoint }');
216
- },
217
- );
218
-
219
- itWithDir(
220
- 'should generate individual handlers with v1 adapter',
221
- async ({ dir }) => {
222
- const outputDir = join(dir, 'output');
223
- const routesDir = join(dir, 'routes');
224
- await mkdir(outputDir, { recursive: true });
225
-
226
- await createMockEndpointFile(
227
- routesDir,
228
- 'testEndpoint.ts',
229
- 'testEndpoint',
230
- '/test',
231
- 'GET',
232
- );
233
-
234
- const constructs = await generator.load('**/routes/*.ts', dir);
235
-
236
- const routes = await generator.build(context, constructs, outputDir, {
237
- provider: 'aws-apigatewayv1',
238
- });
239
-
240
- expect(routes).toHaveLength(1);
241
-
242
- const handlerPath = join(outputDir, 'testEndpoint.ts');
243
- const handlerContent = await readFile(handlerPath, 'utf-8');
244
- expect(handlerContent).toContain('AmazonApiGatewayV1Endpoint');
245
- expect(handlerContent).toContain('import { testEndpoint }');
246
- },
247
- );
248
-
249
- itWithDir(
250
- 'should generate individual handlers with v2 adapter',
251
- async ({ dir }) => {
252
- const outputDir = join(dir, 'output');
253
- const routesDir = join(dir, 'routes');
254
- await mkdir(outputDir, { recursive: true });
255
-
256
- await createMockEndpointFile(
257
- routesDir,
258
- 'testEndpoint.ts',
259
- 'testEndpoint',
260
- '/test',
261
- 'GET',
262
- );
263
-
264
- const constructs = await generator.load('**/routes/*.ts', dir);
265
-
266
- const routes = await generator.build(context, constructs, outputDir, {
267
- provider: 'aws-apigatewayv2',
268
- });
269
-
270
- expect(routes).toHaveLength(1);
271
-
272
- const handlerPath = join(outputDir, 'testEndpoint.ts');
273
- const handlerContent = await readFile(handlerPath, 'utf-8');
274
- expect(handlerContent).toContain('AmazonApiGatewayV2Endpoint');
275
- expect(handlerContent).toContain('import { testEndpoint }');
276
- },
277
- );
278
-
279
- itWithDir(
280
- 'should return empty array for empty constructs',
281
- async ({ dir }) => {
282
- const outputDir = join(dir, 'output');
283
- const routes = await generator.build(context, [], outputDir);
284
- expect(routes).toEqual([]);
285
- },
286
- );
287
-
288
- itWithDir(
289
- 'should use default provider when none specified',
290
- async ({ dir }) => {
291
- const outputDir = join(dir, 'output');
292
- const routesDir = join(dir, 'routes');
293
- await mkdir(outputDir, { recursive: true });
294
-
295
- await createMockEndpointFile(
296
- routesDir,
297
- 'testEndpoint.ts',
298
- 'testEndpoint',
299
- '/test',
300
- 'GET',
301
- );
302
-
303
- const constructs = await generator.load('**/routes/*.ts', dir);
304
-
305
- const routes = await generator.build(context, constructs, outputDir);
306
-
307
- expect(routes).toHaveLength(1);
308
-
309
- // Should use default aws-apigatewayv2
310
- const handlerPath = join(outputDir, 'testEndpoint.ts');
311
- const handlerContent = await readFile(handlerPath, 'utf-8');
312
- expect(handlerContent).toContain('AmazonApiGatewayV2Endpoint');
313
- },
314
- );
315
-
316
- itWithDir('should throw error for unsupported provider', async ({ dir }) => {
317
- const outputDir = join(dir, 'output');
318
- const constructs = [
319
- createTestEndpointConstruct('testEndpoint', '/test', 'GET', dir),
320
- ];
321
-
322
- await expect(
323
- generator.build(context, constructs, outputDir, {
324
- provider: 'unsupported' as any,
325
- }),
326
- ).rejects.toThrow('Unsupported provider: unsupported');
327
- });
328
-
329
- itWithDir(
330
- 'should generate correct import paths for nested files',
331
- async ({ dir }) => {
332
- const outputDir = join(dir, 'output');
333
- const nestedDir = join(dir, 'src', 'api', 'endpoints');
334
- await mkdir(outputDir, { recursive: true });
335
-
336
- await createMockEndpointFile(
337
- nestedDir,
338
- 'deepEndpoint.ts',
339
- 'deepEndpoint',
340
- '/deep',
341
- 'GET',
342
- );
343
-
344
- const constructs = await generator.load('**/src/api/endpoints/*.ts', dir);
345
-
346
- await generator.build(context, constructs, outputDir, {
347
- provider: 'aws-apigatewayv2',
348
- });
349
-
350
- const handlerPath = join(outputDir, 'deepEndpoint.ts');
351
- const handlerContent = await readFile(handlerPath, 'utf-8');
352
-
353
- // Check that relative imports are correct
354
- expect(handlerContent).toContain(
355
- "from '../src/api/endpoints/deepEndpoint.js'",
356
- );
357
- expect(handlerContent).toMatch(/from ['"]\.\.\/\.\.\/\.\.\/.*\/env['"]/);
358
- },
359
- );
360
-
361
- itWithDir('should log generation progress', async ({ dir }) => {
362
- const logSpy = vi.spyOn(console, 'log');
363
- const outputDir = join(dir, 'output');
364
- const routesDir = join(dir, 'routes');
365
- await mkdir(outputDir, { recursive: true });
366
-
367
- await Promise.all([
368
- createMockEndpointFile(
369
- routesDir,
370
- 'endpoint1.ts',
371
- 'endpoint1',
372
- '/test1',
373
- 'GET',
374
- ),
375
- createMockEndpointFile(
376
- routesDir,
377
- 'endpoint2.ts',
378
- 'endpoint2',
379
- '/test2',
380
- 'POST',
381
- ),
382
- ]);
383
-
384
- const constructs = await generator.load('**/routes/*.ts', dir);
385
-
386
- await generator.build(context, constructs, outputDir, {
387
- provider: 'server',
388
- });
389
-
390
- expect(logSpy).toHaveBeenCalledWith('Generated server with 2 endpoints');
391
-
392
- logSpy.mockRestore();
393
- });
16
+ let _tempDir: string;
17
+ let _outputDir: string;
18
+ let generator: EndpointGenerator;
19
+ let context: ReturnType<typeof createMockBuildContext>;
20
+
21
+ beforeEach(async () => {
22
+ generator = new EndpointGenerator();
23
+ context = createMockBuildContext();
24
+ });
25
+
26
+ afterEach(async () => {});
27
+
28
+ it('should identify valid endpoints', () => {
29
+ const testEndpoint = createTestEndpoint('/test', 'GET');
30
+ expect(generator.isConstruct(testEndpoint)).toBe(true);
31
+ });
32
+
33
+ it('should reject invalid constructs', () => {
34
+ expect(generator.isConstruct({})).toBe(false);
35
+ expect(generator.isConstruct('string')).toBe(false);
36
+ expect(generator.isConstruct(null)).toBe(false);
37
+ });
38
+
39
+ const createTestEndpointConstruct = (
40
+ key: string,
41
+ path: string,
42
+ method: HttpMethod,
43
+ dir: string,
44
+ ): GeneratedConstruct<Endpoint<any, any, any, any, any, any>> => ({
45
+ key,
46
+ name: key.toLowerCase(),
47
+ construct: createTestEndpoint(path, method),
48
+ path: {
49
+ absolute: join(dir, `${key}.ts`),
50
+ relative: `./${key}.ts`,
51
+ },
52
+ });
53
+
54
+ itWithDir('should generate a single server app file', async ({ dir }) => {
55
+ const outputDir = join(dir, 'output');
56
+ const routesDir = join(dir, 'routes');
57
+ await mkdir(outputDir, { recursive: true });
58
+
59
+ await Promise.all([
60
+ createMockEndpointFile(
61
+ routesDir,
62
+ 'testEndpoint.ts',
63
+ 'testEndpoint',
64
+ '/test',
65
+ 'GET',
66
+ ),
67
+ createMockEndpointFile(
68
+ routesDir,
69
+ 'anotherEndpoint.ts',
70
+ 'anotherEndpoint',
71
+ '/another',
72
+ 'POST',
73
+ ),
74
+ ]);
75
+
76
+ const constructs = await generator.load('**/routes/*.ts', dir);
77
+
78
+ const routes = await generator.build(context, constructs, outputDir, {
79
+ provider: 'server',
80
+ enableOpenApi: true,
81
+ });
82
+
83
+ expect(routes).toHaveLength(1);
84
+ expect(routes[0]).toMatchObject({
85
+ path: '*',
86
+ method: 'ALL',
87
+ handler: expect.stringContaining('app.ts'),
88
+ });
89
+
90
+ // Check that the app.ts file was created
91
+ const appPath = join(outputDir, 'app.ts');
92
+ const appContent = await readFile(appPath, 'utf-8');
93
+
94
+ expect(appContent).toContain('import { setupEndpoints }');
95
+ expect(appContent).toContain('import { setupSubscribers }');
96
+ expect(appContent).toContain('function createApp');
97
+ expect(appContent).toContain('app?: HonoType');
98
+ expect(appContent).toContain('enableOpenApi: boolean = true');
99
+ expect(appContent).toContain('interface ServerApp');
100
+ expect(appContent).toContain('async start(options');
101
+ expect(appContent).toContain('serve: (app: HonoType, port: number)');
102
+
103
+ // Check that the endpoints.ts file was created with endpoint logic
104
+ const endpointsPath = join(outputDir, 'endpoints.ts');
105
+ const endpointsContent = await readFile(endpointsPath, 'utf-8');
106
+
107
+ expect(endpointsContent).toContain('import { HonoEndpoint }');
108
+ expect(endpointsContent).toContain('import { testEndpoint }');
109
+ expect(endpointsContent).toContain('import { anotherEndpoint }');
110
+ // Function signature always defaults to true
111
+ expect(endpointsContent).toContain('enableOpenApi: boolean = true');
112
+ // OpenAPI options are configured based on the parameter
113
+ expect(endpointsContent).toContain("docsPath: '/__docs'");
114
+ });
115
+
116
+ itWithDir(
117
+ 'should generate server app without OpenAPI when disabled',
118
+ async ({ dir }) => {
119
+ const outputDir = join(dir, 'output');
120
+ const routesDir = join(dir, 'routes');
121
+ await mkdir(outputDir, { recursive: true });
122
+
123
+ await createMockEndpointFile(
124
+ routesDir,
125
+ 'testEndpoint.ts',
126
+ 'testEndpoint',
127
+ '/test',
128
+ 'GET',
129
+ );
130
+
131
+ const constructs = await generator.load('**/routes/*.ts', dir);
132
+
133
+ await generator.build(context, constructs, outputDir, {
134
+ provider: 'server',
135
+ enableOpenApi: false,
136
+ });
137
+
138
+ const appPath = join(outputDir, 'app.ts');
139
+ const appContent = await readFile(appPath, 'utf-8');
140
+
141
+ expect(appContent).toContain('function createApp');
142
+
143
+ // Check that the endpoints.ts file defaults to true (but can be overridden)
144
+ const endpointsPath = join(outputDir, 'endpoints.ts');
145
+ const endpointsContent = await readFile(endpointsPath, 'utf-8');
146
+
147
+ // The function signature always defaults to true
148
+ expect(endpointsContent).toContain('enableOpenApi: boolean = true');
149
+ // But the OpenAPI options are configured dynamically based on the parameter
150
+ expect(endpointsContent).toContain('docsPath: false');
151
+ },
152
+ );
153
+
154
+ itWithDir(
155
+ 'should generate individual handlers in routes subdirectory',
156
+ async ({ dir }) => {
157
+ const outputDir = join(dir, 'output');
158
+ const routesDir = join(dir, 'routes');
159
+ await mkdir(outputDir, { recursive: true });
160
+
161
+ await Promise.all([
162
+ createMockEndpointFile(
163
+ routesDir,
164
+ 'testEndpoint.ts',
165
+ 'testEndpoint',
166
+ '/test',
167
+ 'GET',
168
+ ),
169
+ createMockEndpointFile(
170
+ routesDir,
171
+ 'anotherEndpoint.ts',
172
+ 'anotherEndpoint',
173
+ '/another',
174
+ 'POST',
175
+ ),
176
+ ]);
177
+
178
+ const constructs = await generator.load('**/routes/*.ts', dir);
179
+
180
+ const routes = await generator.build(context, constructs, outputDir, {
181
+ provider: 'aws-lambda',
182
+ });
183
+
184
+ expect(routes).toHaveLength(2);
185
+
186
+ // Find routes by their path since order may vary
187
+ const testRoute = routes.find((r) => r.path === '/test');
188
+ const anotherRoute = routes.find((r) => r.path === '/another');
189
+
190
+ expect(testRoute).toMatchObject({
191
+ path: '/test',
192
+ method: 'GET',
193
+ handler: expect.stringContaining('routes/testEndpoint.handler'),
194
+ });
195
+ expect(anotherRoute).toMatchObject({
196
+ path: '/another',
197
+ method: 'POST',
198
+ handler: expect.stringContaining('routes/anotherEndpoint.handler'),
199
+ });
200
+
201
+ // Check that handler files were created
202
+ const testHandlerPath = join(outputDir, 'routes', 'testEndpoint.ts');
203
+ const testHandlerContent = await readFile(testHandlerPath, 'utf-8');
204
+ expect(testHandlerContent).toContain('AmazonApiGatewayV2Endpoint');
205
+ expect(testHandlerContent).toContain('import { testEndpoint }');
206
+
207
+ const anotherHandlerPath = join(
208
+ outputDir,
209
+ 'routes',
210
+ 'anotherEndpoint.ts',
211
+ );
212
+ const anotherHandlerContent = await readFile(anotherHandlerPath, 'utf-8');
213
+ expect(anotherHandlerContent).toContain('AmazonApiGatewayV2Endpoint');
214
+ expect(anotherHandlerContent).toContain('import { anotherEndpoint }');
215
+ },
216
+ );
217
+
218
+ itWithDir(
219
+ 'should generate individual handlers with v1 adapter',
220
+ async ({ dir }) => {
221
+ const outputDir = join(dir, 'output');
222
+ const routesDir = join(dir, 'routes');
223
+ await mkdir(outputDir, { recursive: true });
224
+
225
+ await createMockEndpointFile(
226
+ routesDir,
227
+ 'testEndpoint.ts',
228
+ 'testEndpoint',
229
+ '/test',
230
+ 'GET',
231
+ );
232
+
233
+ const constructs = await generator.load('**/routes/*.ts', dir);
234
+
235
+ const routes = await generator.build(context, constructs, outputDir, {
236
+ provider: 'aws-apigatewayv1',
237
+ });
238
+
239
+ expect(routes).toHaveLength(1);
240
+
241
+ const handlerPath = join(outputDir, 'testEndpoint.ts');
242
+ const handlerContent = await readFile(handlerPath, 'utf-8');
243
+ expect(handlerContent).toContain('AmazonApiGatewayV1Endpoint');
244
+ expect(handlerContent).toContain('import { testEndpoint }');
245
+ },
246
+ );
247
+
248
+ itWithDir(
249
+ 'should generate individual handlers with v2 adapter',
250
+ async ({ dir }) => {
251
+ const outputDir = join(dir, 'output');
252
+ const routesDir = join(dir, 'routes');
253
+ await mkdir(outputDir, { recursive: true });
254
+
255
+ await createMockEndpointFile(
256
+ routesDir,
257
+ 'testEndpoint.ts',
258
+ 'testEndpoint',
259
+ '/test',
260
+ 'GET',
261
+ );
262
+
263
+ const constructs = await generator.load('**/routes/*.ts', dir);
264
+
265
+ const routes = await generator.build(context, constructs, outputDir, {
266
+ provider: 'aws-apigatewayv2',
267
+ });
268
+
269
+ expect(routes).toHaveLength(1);
270
+
271
+ const handlerPath = join(outputDir, 'testEndpoint.ts');
272
+ const handlerContent = await readFile(handlerPath, 'utf-8');
273
+ expect(handlerContent).toContain('AmazonApiGatewayV2Endpoint');
274
+ expect(handlerContent).toContain('import { testEndpoint }');
275
+ },
276
+ );
277
+
278
+ itWithDir(
279
+ 'should return empty array for empty constructs',
280
+ async ({ dir }) => {
281
+ const outputDir = join(dir, 'output');
282
+ const routes = await generator.build(context, [], outputDir);
283
+ expect(routes).toEqual([]);
284
+ },
285
+ );
286
+
287
+ itWithDir(
288
+ 'should use default provider when none specified',
289
+ async ({ dir }) => {
290
+ const outputDir = join(dir, 'output');
291
+ const routesDir = join(dir, 'routes');
292
+ await mkdir(outputDir, { recursive: true });
293
+
294
+ await createMockEndpointFile(
295
+ routesDir,
296
+ 'testEndpoint.ts',
297
+ 'testEndpoint',
298
+ '/test',
299
+ 'GET',
300
+ );
301
+
302
+ const constructs = await generator.load('**/routes/*.ts', dir);
303
+
304
+ const routes = await generator.build(context, constructs, outputDir);
305
+
306
+ expect(routes).toHaveLength(1);
307
+
308
+ // Should use default aws-apigatewayv2
309
+ const handlerPath = join(outputDir, 'testEndpoint.ts');
310
+ const handlerContent = await readFile(handlerPath, 'utf-8');
311
+ expect(handlerContent).toContain('AmazonApiGatewayV2Endpoint');
312
+ },
313
+ );
314
+
315
+ itWithDir('should throw error for unsupported provider', async ({ dir }) => {
316
+ const outputDir = join(dir, 'output');
317
+ const constructs = [
318
+ createTestEndpointConstruct('testEndpoint', '/test', 'GET', dir),
319
+ ];
320
+
321
+ await expect(
322
+ generator.build(context, constructs, outputDir, {
323
+ provider: 'unsupported' as any,
324
+ }),
325
+ ).rejects.toThrow('Unsupported provider: unsupported');
326
+ });
327
+
328
+ itWithDir(
329
+ 'should generate correct import paths for nested files',
330
+ async ({ dir }) => {
331
+ const outputDir = join(dir, 'output');
332
+ const nestedDir = join(dir, 'src', 'api', 'endpoints');
333
+ await mkdir(outputDir, { recursive: true });
334
+
335
+ await createMockEndpointFile(
336
+ nestedDir,
337
+ 'deepEndpoint.ts',
338
+ 'deepEndpoint',
339
+ '/deep',
340
+ 'GET',
341
+ );
342
+
343
+ const constructs = await generator.load('**/src/api/endpoints/*.ts', dir);
344
+
345
+ await generator.build(context, constructs, outputDir, {
346
+ provider: 'aws-apigatewayv2',
347
+ });
348
+
349
+ const handlerPath = join(outputDir, 'deepEndpoint.ts');
350
+ const handlerContent = await readFile(handlerPath, 'utf-8');
351
+
352
+ // Check that relative imports are correct
353
+ expect(handlerContent).toContain(
354
+ "from '../src/api/endpoints/deepEndpoint.js'",
355
+ );
356
+ expect(handlerContent).toMatch(/from ['"]\.\.\/\.\.\/\.\.\/.*\/env['"]/);
357
+ },
358
+ );
359
+
360
+ itWithDir('should log generation progress', async ({ dir }) => {
361
+ const logSpy = vi.spyOn(console, 'log');
362
+ const outputDir = join(dir, 'output');
363
+ const routesDir = join(dir, 'routes');
364
+ await mkdir(outputDir, { recursive: true });
365
+
366
+ await Promise.all([
367
+ createMockEndpointFile(
368
+ routesDir,
369
+ 'endpoint1.ts',
370
+ 'endpoint1',
371
+ '/test1',
372
+ 'GET',
373
+ ),
374
+ createMockEndpointFile(
375
+ routesDir,
376
+ 'endpoint2.ts',
377
+ 'endpoint2',
378
+ '/test2',
379
+ 'POST',
380
+ ),
381
+ ]);
382
+
383
+ const constructs = await generator.load('**/routes/*.ts', dir);
384
+
385
+ await generator.build(context, constructs, outputDir, {
386
+ provider: 'server',
387
+ });
388
+
389
+ expect(logSpy).toHaveBeenCalledWith('Generated server with 2 endpoints');
390
+
391
+ logSpy.mockRestore();
392
+ });
393
+
394
+ itWithDir(
395
+ 'should generate production app file when production mode is enabled',
396
+ async ({ dir }) => {
397
+ const outputDir = join(dir, 'output');
398
+ const routesDir = join(dir, 'routes');
399
+ await mkdir(outputDir, { recursive: true });
400
+
401
+ await createMockEndpointFile(
402
+ routesDir,
403
+ 'testEndpoint.ts',
404
+ 'testEndpoint',
405
+ '/test',
406
+ 'GET',
407
+ );
408
+
409
+ const constructs = await generator.load('**/routes/*.ts', dir);
410
+
411
+ const productionContext = {
412
+ ...context,
413
+ production: {
414
+ enabled: true,
415
+ healthCheck: '/health',
416
+ gracefulShutdown: true,
417
+ openapi: false,
418
+ subscribers: 'exclude' as const,
419
+ optimizedHandlers: false,
420
+ },
421
+ };
422
+
423
+ await generator.build(productionContext, constructs, outputDir, {
424
+ provider: 'server',
425
+ });
426
+
427
+ const appPath = join(outputDir, 'app.ts');
428
+ const appContent = await readFile(appPath, 'utf-8');
429
+
430
+ expect(appContent).toContain('Generated production server application');
431
+ expect(appContent).toContain("honoApp.get('/health'");
432
+ expect(appContent).toContain("honoApp.get('/ready'");
433
+ expect(appContent).toContain('Graceful shutdown initiated');
434
+
435
+ // Check server.ts entry point was generated
436
+ const serverPath = join(outputDir, 'server.ts');
437
+ const serverContent = await readFile(serverPath, 'utf-8');
438
+ expect(serverContent).toContain('Production server entry point');
439
+ expect(serverContent).toContain(
440
+ "import { serve } from '@hono/node-server'",
441
+ );
442
+ },
443
+ );
444
+
445
+ itWithDir(
446
+ 'should generate production app with subscribers when include option is set',
447
+ async ({ dir }) => {
448
+ const outputDir = join(dir, 'output');
449
+ const routesDir = join(dir, 'routes');
450
+ await mkdir(outputDir, { recursive: true });
451
+
452
+ await createMockEndpointFile(
453
+ routesDir,
454
+ 'testEndpoint.ts',
455
+ 'testEndpoint',
456
+ '/test',
457
+ 'GET',
458
+ );
459
+
460
+ const constructs = await generator.load('**/routes/*.ts', dir);
461
+
462
+ const productionContext = {
463
+ ...context,
464
+ production: {
465
+ enabled: true,
466
+ healthCheck: '/health',
467
+ gracefulShutdown: false,
468
+ openapi: true,
469
+ subscribers: 'include' as const,
470
+ optimizedHandlers: false,
471
+ },
472
+ };
473
+
474
+ await generator.build(productionContext, constructs, outputDir, {
475
+ provider: 'server',
476
+ });
477
+
478
+ const appPath = join(outputDir, 'app.ts');
479
+ const appContent = await readFile(appPath, 'utf-8');
480
+
481
+ expect(appContent).toContain('import { setupSubscribers }');
482
+ expect(appContent).toContain('Start subscribers in background');
483
+ expect(appContent).toContain(
484
+ 'setupEndpoints(honoApp, envParser, logger, true)',
485
+ );
486
+ },
487
+ );
488
+
489
+ itWithDir(
490
+ 'should log aws-lambda handler generation for each route',
491
+ async ({ dir }) => {
492
+ const logSpy = vi.spyOn(console, 'log');
493
+ const outputDir = join(dir, 'output');
494
+ const routesDir = join(dir, 'routes');
495
+ await mkdir(outputDir, { recursive: true });
496
+
497
+ await createMockEndpointFile(
498
+ routesDir,
499
+ 'testEndpoint.ts',
500
+ 'testEndpoint',
501
+ '/test',
502
+ 'GET',
503
+ );
504
+
505
+ const constructs = await generator.load('**/routes/*.ts', dir);
506
+
507
+ await generator.build(context, constructs, outputDir, {
508
+ provider: 'aws-lambda',
509
+ });
510
+
511
+ expect(logSpy).toHaveBeenCalledWith('Generated handler for GET /test');
512
+ logSpy.mockRestore();
513
+ },
514
+ );
515
+
516
+ itWithDir(
517
+ 'should log apigatewayv2 handler generation for each route',
518
+ async ({ dir }) => {
519
+ const logSpy = vi.spyOn(console, 'log');
520
+ const outputDir = join(dir, 'output');
521
+ const routesDir = join(dir, 'routes');
522
+ await mkdir(outputDir, { recursive: true });
523
+
524
+ await createMockEndpointFile(
525
+ routesDir,
526
+ 'testEndpoint.ts',
527
+ 'testEndpoint',
528
+ '/api/users',
529
+ 'POST',
530
+ );
531
+
532
+ const constructs = await generator.load('**/routes/*.ts', dir);
533
+
534
+ await generator.build(context, constructs, outputDir, {
535
+ provider: 'aws-apigatewayv2',
536
+ });
537
+
538
+ expect(logSpy).toHaveBeenCalledWith(
539
+ 'Generated handler for POST /api/users',
540
+ );
541
+ logSpy.mockRestore();
542
+ },
543
+ );
394
544
  });