@geekmidas/cli 0.0.25 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/FUNCTION_CRON_SUPPORT.md +266 -0
  2. package/README.md +21 -4
  3. package/dist/CronGenerator-ClbRcmz_.mjs +53 -0
  4. package/dist/CronGenerator-ClbRcmz_.mjs.map +1 -0
  5. package/dist/CronGenerator-Ctl4USy4.cjs +59 -0
  6. package/dist/CronGenerator-Ctl4USy4.cjs.map +1 -0
  7. package/dist/EndpointGenerator-Dj7AumHi.cjs +164 -0
  8. package/dist/EndpointGenerator-Dj7AumHi.cjs.map +1 -0
  9. package/dist/EndpointGenerator-uBA1ixUw.mjs +158 -0
  10. package/dist/EndpointGenerator-uBA1ixUw.mjs.map +1 -0
  11. package/dist/FunctionGenerator-DN681IUn.cjs +58 -0
  12. package/dist/FunctionGenerator-DN681IUn.cjs.map +1 -0
  13. package/dist/FunctionGenerator-crAa-JC7.mjs +52 -0
  14. package/dist/FunctionGenerator-crAa-JC7.mjs.map +1 -0
  15. package/dist/Generator-C3tYSTQY.cjs +47 -0
  16. package/dist/Generator-C3tYSTQY.cjs.map +1 -0
  17. package/dist/Generator-CDt4pB3W.mjs +41 -0
  18. package/dist/Generator-CDt4pB3W.mjs.map +1 -0
  19. package/dist/__tests__/config.spec.cjs +98 -0
  20. package/dist/__tests__/config.spec.cjs.map +1 -0
  21. package/dist/__tests__/config.spec.mjs +97 -0
  22. package/dist/__tests__/config.spec.mjs.map +1 -0
  23. package/dist/__tests__/test-helpers.cjs +14 -0
  24. package/dist/__tests__/test-helpers.mjs +4 -0
  25. package/dist/build/__tests__/index-new.spec.cjs +286 -0
  26. package/dist/build/__tests__/index-new.spec.cjs.map +1 -0
  27. package/dist/build/__tests__/index-new.spec.mjs +285 -0
  28. package/dist/build/__tests__/index-new.spec.mjs.map +1 -0
  29. package/dist/build/index.cjs +11 -0
  30. package/dist/build/index.mjs +11 -0
  31. package/dist/build/manifests.cjs +3 -0
  32. package/dist/build/manifests.mjs +3 -0
  33. package/dist/build/providerResolver.cjs +5 -0
  34. package/dist/build/providerResolver.mjs +3 -0
  35. package/dist/build/types.cjs +0 -0
  36. package/dist/build/types.mjs +0 -0
  37. package/dist/build-BZdwxCLW.mjs +64 -0
  38. package/dist/build-BZdwxCLW.mjs.map +1 -0
  39. package/dist/build-BfQFnU5-.cjs +70 -0
  40. package/dist/build-BfQFnU5-.cjs.map +1 -0
  41. package/dist/{chunk-CUT6urMc.cjs → chunk-CsX-DzYB.cjs} +12 -0
  42. package/dist/config-CXxYmz_o.mjs +30 -0
  43. package/dist/config-CXxYmz_o.mjs.map +1 -0
  44. package/dist/config-RcNESK0T.cjs +36 -0
  45. package/dist/config-RcNESK0T.cjs.map +1 -0
  46. package/dist/config.cjs +1 -1
  47. package/dist/config.mjs +1 -1
  48. package/dist/esm-9eeZntth.mjs +3777 -0
  49. package/dist/esm-9eeZntth.mjs.map +1 -0
  50. package/dist/esm-Crmo4h9t.cjs +4392 -0
  51. package/dist/esm-Crmo4h9t.cjs.map +1 -0
  52. package/dist/esm-CsJbr7gi.mjs +3 -0
  53. package/dist/esm-w09tAC4l.cjs +8 -0
  54. package/dist/generators/CronGenerator.cjs +4 -0
  55. package/dist/generators/CronGenerator.mjs +4 -0
  56. package/dist/generators/EndpointGenerator.cjs +4 -0
  57. package/dist/generators/EndpointGenerator.mjs +4 -0
  58. package/dist/generators/FunctionGenerator.cjs +4 -0
  59. package/dist/generators/FunctionGenerator.mjs +4 -0
  60. package/dist/generators/Generator.cjs +3 -0
  61. package/dist/generators/Generator.mjs +3 -0
  62. package/dist/generators/__tests__/CronGenerator.spec.cjs +216 -0
  63. package/dist/generators/__tests__/CronGenerator.spec.cjs.map +1 -0
  64. package/dist/generators/__tests__/CronGenerator.spec.mjs +215 -0
  65. package/dist/generators/__tests__/CronGenerator.spec.mjs.map +1 -0
  66. package/dist/generators/__tests__/EndpointGenerator.spec.cjs +182 -0
  67. package/dist/generators/__tests__/EndpointGenerator.spec.cjs.map +1 -0
  68. package/dist/generators/__tests__/EndpointGenerator.spec.mjs +181 -0
  69. package/dist/generators/__tests__/EndpointGenerator.spec.mjs.map +1 -0
  70. package/dist/generators/__tests__/FunctionGenerator.spec.cjs +152 -0
  71. package/dist/generators/__tests__/FunctionGenerator.spec.cjs.map +1 -0
  72. package/dist/generators/__tests__/FunctionGenerator.spec.mjs +151 -0
  73. package/dist/generators/__tests__/FunctionGenerator.spec.mjs.map +1 -0
  74. package/dist/generators/index.cjs +10 -0
  75. package/dist/generators/index.mjs +7 -0
  76. package/dist/generators-CsLujGXs.mjs +0 -0
  77. package/dist/generators-_pY7sHy1.cjs +0 -0
  78. package/dist/index.cjs +68 -26
  79. package/dist/index.cjs.map +1 -0
  80. package/dist/index.mjs +67 -25
  81. package/dist/index.mjs.map +1 -0
  82. package/dist/manifests-BTtfDMX8.cjs +26 -0
  83. package/dist/manifests-BTtfDMX8.cjs.map +1 -0
  84. package/dist/manifests-HX4z4kkz.mjs +20 -0
  85. package/dist/manifests-HX4z4kkz.mjs.map +1 -0
  86. package/dist/{openapi-CksVdkh2.mjs → openapi-BivnatiC.mjs} +8 -6
  87. package/dist/openapi-BivnatiC.mjs.map +1 -0
  88. package/dist/{openapi-D4QQJUPY.cjs → openapi-DW-qF3oW.cjs} +9 -7
  89. package/dist/openapi-DW-qF3oW.cjs.map +1 -0
  90. package/dist/{openapi-react-query-C1JLYUOs.cjs → openapi-react-query-J0BzBHhN.cjs} +4 -3
  91. package/dist/openapi-react-query-J0BzBHhN.cjs.map +1 -0
  92. package/dist/{openapi-react-query-DpT3XHFC.mjs → openapi-react-query-lgS7AVEz.mjs} +3 -2
  93. package/dist/openapi-react-query-lgS7AVEz.mjs.map +1 -0
  94. package/dist/openapi-react-query.cjs +1 -1
  95. package/dist/openapi-react-query.mjs +1 -1
  96. package/dist/openapi.cjs +4 -3
  97. package/dist/openapi.mjs +4 -3
  98. package/dist/providerResolver-B_TjNF0_.mjs +96 -0
  99. package/dist/providerResolver-B_TjNF0_.mjs.map +1 -0
  100. package/dist/providerResolver-Cs-0YCaP.cjs +114 -0
  101. package/dist/providerResolver-Cs-0YCaP.cjs.map +1 -0
  102. package/dist/test-helpers-ARd8GDgx.cjs +199 -0
  103. package/dist/test-helpers-ARd8GDgx.cjs.map +1 -0
  104. package/dist/test-helpers-DdVBk23F.mjs +133 -0
  105. package/dist/test-helpers-DdVBk23F.mjs.map +1 -0
  106. package/examples/cron-example.ts +45 -0
  107. package/examples/function-example.ts +40 -0
  108. package/examples/gkm.config.json +22 -0
  109. package/examples/gkm.minimal.config.json +7 -0
  110. package/examples/gkm.production.config.json +27 -0
  111. package/package.json +35 -14
  112. package/src/__tests__/config.spec.ts +110 -0
  113. package/src/__tests__/test-helpers.ts +178 -0
  114. package/src/build/__tests__/index-new.spec.ts +578 -0
  115. package/src/build/index.ts +136 -0
  116. package/src/build/manifests.ts +32 -0
  117. package/src/build/providerResolver.ts +184 -0
  118. package/src/build/types.ts +37 -0
  119. package/src/config.ts +14 -6
  120. package/src/generators/CronGenerator.ts +97 -0
  121. package/src/generators/EndpointGenerator.ts +290 -0
  122. package/src/generators/FunctionGenerator.ts +96 -0
  123. package/src/generators/Generator.ts +95 -0
  124. package/src/generators/__tests__/CronGenerator.spec.ts +445 -0
  125. package/src/generators/__tests__/EndpointGenerator.spec.ts +372 -0
  126. package/src/generators/__tests__/FunctionGenerator.spec.ts +257 -0
  127. package/src/generators/index.ts +8 -0
  128. package/src/index.ts +57 -22
  129. package/src/openapi.ts +4 -3
  130. package/src/types.ts +73 -2
  131. package/dist/build-BTggTCYL.cjs +0 -176
  132. package/dist/build-Ca4P6_lY.mjs +0 -170
  133. package/dist/build.cjs +0 -5
  134. package/dist/build.mjs +0 -5
  135. package/dist/config-BNqUMsvc.cjs +0 -24
  136. package/dist/config-BciAdY6_.mjs +0 -18
  137. package/dist/loadEndpoints-BBIavB9h.cjs +0 -37
  138. package/dist/loadEndpoints-DAZ53Og2.mjs +0 -31
  139. package/dist/loadEndpoints.cjs +0 -3
  140. package/dist/loadEndpoints.mjs +0 -3
  141. package/src/build.ts +0 -305
  142. package/src/loadEndpoints.ts +0 -48
@@ -0,0 +1,372 @@
1
+ import { mkdir, readFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import type { Endpoint } from '@geekmidas/api/server';
4
+ import { itWithDir } from '@geekmidas/testkit/os';
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6
+ import type { HttpMethod } from '../../../../api/src/constructs/types';
7
+ import {
8
+ createMockBuildContext,
9
+ createMockEndpointFile,
10
+ createTestEndpoint,
11
+ } from '../../__tests__/test-helpers';
12
+ import { EndpointGenerator } from '../EndpointGenerator';
13
+ import type { GeneratedConstruct } from '../Generator';
14
+
15
+ describe('EndpointGenerator', () => {
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 { HonoEndpoint }');
95
+ expect(appContent).toContain('import { testEndpoint }');
96
+ expect(appContent).toContain('import { anotherEndpoint }');
97
+ expect(appContent).toContain('enableOpenApi: boolean = true');
98
+ expect(appContent).toContain("docsPath: '/docs'");
99
+ });
100
+
101
+ itWithDir(
102
+ 'should generate server app without OpenAPI when disabled',
103
+ async ({ dir }) => {
104
+ const outputDir = join(dir, 'output');
105
+ const routesDir = join(dir, 'routes');
106
+ await mkdir(outputDir, { recursive: true });
107
+
108
+ await createMockEndpointFile(
109
+ routesDir,
110
+ 'testEndpoint.ts',
111
+ 'testEndpoint',
112
+ '/test',
113
+ 'GET',
114
+ );
115
+
116
+ const constructs = await generator.load('**/routes/*.ts', dir);
117
+
118
+ await generator.build(context, constructs, outputDir, {
119
+ provider: 'server',
120
+ enableOpenApi: false,
121
+ });
122
+
123
+ const appPath = join(outputDir, 'app.ts');
124
+ const appContent = await readFile(appPath, 'utf-8');
125
+
126
+ expect(appContent).toContain('enableOpenApi: boolean = false');
127
+ expect(appContent).toContain('docsPath: false');
128
+ },
129
+ );
130
+
131
+ itWithDir(
132
+ 'should generate individual handlers in routes subdirectory',
133
+ async ({ dir }) => {
134
+ const outputDir = join(dir, 'output');
135
+ const routesDir = join(dir, 'routes');
136
+ await mkdir(outputDir, { recursive: true });
137
+
138
+ await Promise.all([
139
+ createMockEndpointFile(
140
+ routesDir,
141
+ 'testEndpoint.ts',
142
+ 'testEndpoint',
143
+ '/test',
144
+ 'GET',
145
+ ),
146
+ createMockEndpointFile(
147
+ routesDir,
148
+ 'anotherEndpoint.ts',
149
+ 'anotherEndpoint',
150
+ '/another',
151
+ 'POST',
152
+ ),
153
+ ]);
154
+
155
+ const constructs = await generator.load('**/routes/*.ts', dir);
156
+
157
+ const routes = await generator.build(context, constructs, outputDir, {
158
+ provider: 'aws-lambda',
159
+ });
160
+
161
+ expect(routes).toHaveLength(2);
162
+
163
+ // Find routes by their path since order may vary
164
+ const testRoute = routes.find((r) => r.path === '/test');
165
+ const anotherRoute = routes.find((r) => r.path === '/another');
166
+
167
+ expect(testRoute).toMatchObject({
168
+ path: '/test',
169
+ method: 'GET',
170
+ handler: expect.stringContaining('routes/testEndpoint.handler'),
171
+ });
172
+ expect(anotherRoute).toMatchObject({
173
+ path: '/another',
174
+ method: 'POST',
175
+ handler: expect.stringContaining('routes/anotherEndpoint.handler'),
176
+ });
177
+
178
+ // Check that handler files were created
179
+ const testHandlerPath = join(outputDir, 'routes', 'testEndpoint.ts');
180
+ const testHandlerContent = await readFile(testHandlerPath, 'utf-8');
181
+ expect(testHandlerContent).toContain('AmazonApiGatewayV2Endpoint');
182
+ expect(testHandlerContent).toContain('import { testEndpoint }');
183
+
184
+ const anotherHandlerPath = join(
185
+ outputDir,
186
+ 'routes',
187
+ 'anotherEndpoint.ts',
188
+ );
189
+ const anotherHandlerContent = await readFile(anotherHandlerPath, 'utf-8');
190
+ expect(anotherHandlerContent).toContain('AmazonApiGatewayV2Endpoint');
191
+ expect(anotherHandlerContent).toContain('import { anotherEndpoint }');
192
+ },
193
+ );
194
+
195
+ itWithDir(
196
+ 'should generate individual handlers with v1 adapter',
197
+ async ({ dir }) => {
198
+ const outputDir = join(dir, 'output');
199
+ const routesDir = join(dir, 'routes');
200
+ await mkdir(outputDir, { recursive: true });
201
+
202
+ await createMockEndpointFile(
203
+ routesDir,
204
+ 'testEndpoint.ts',
205
+ 'testEndpoint',
206
+ '/test',
207
+ 'GET',
208
+ );
209
+
210
+ const constructs = await generator.load('**/routes/*.ts', dir);
211
+
212
+ const routes = await generator.build(context, constructs, outputDir, {
213
+ provider: 'aws-apigatewayv1',
214
+ });
215
+
216
+ expect(routes).toHaveLength(1);
217
+
218
+ const handlerPath = join(outputDir, 'testEndpoint.ts');
219
+ const handlerContent = await readFile(handlerPath, 'utf-8');
220
+ expect(handlerContent).toContain('AmazonApiGatewayV1Endpoint');
221
+ expect(handlerContent).toContain('import { testEndpoint }');
222
+ },
223
+ );
224
+
225
+ itWithDir(
226
+ 'should generate individual handlers with v2 adapter',
227
+ async ({ dir }) => {
228
+ const outputDir = join(dir, 'output');
229
+ const routesDir = join(dir, 'routes');
230
+ await mkdir(outputDir, { recursive: true });
231
+
232
+ await createMockEndpointFile(
233
+ routesDir,
234
+ 'testEndpoint.ts',
235
+ 'testEndpoint',
236
+ '/test',
237
+ 'GET',
238
+ );
239
+
240
+ const constructs = await generator.load('**/routes/*.ts', dir);
241
+
242
+ const routes = await generator.build(context, constructs, outputDir, {
243
+ provider: 'aws-apigatewayv2',
244
+ });
245
+
246
+ expect(routes).toHaveLength(1);
247
+
248
+ const handlerPath = join(outputDir, 'testEndpoint.ts');
249
+ const handlerContent = await readFile(handlerPath, 'utf-8');
250
+ expect(handlerContent).toContain('AmazonApiGatewayV2Endpoint');
251
+ expect(handlerContent).toContain('import { testEndpoint }');
252
+ },
253
+ );
254
+
255
+ itWithDir(
256
+ 'should return empty array for empty constructs',
257
+ async ({ dir }) => {
258
+ const outputDir = join(dir, 'output');
259
+ const routes = await generator.build(context, [], outputDir);
260
+ expect(routes).toEqual([]);
261
+ },
262
+ );
263
+
264
+ itWithDir(
265
+ 'should use default provider when none specified',
266
+ async ({ dir }) => {
267
+ const outputDir = join(dir, 'output');
268
+ const routesDir = join(dir, 'routes');
269
+ await mkdir(outputDir, { recursive: true });
270
+
271
+ await createMockEndpointFile(
272
+ routesDir,
273
+ 'testEndpoint.ts',
274
+ 'testEndpoint',
275
+ '/test',
276
+ 'GET',
277
+ );
278
+
279
+ const constructs = await generator.load('**/routes/*.ts', dir);
280
+
281
+ const routes = await generator.build(context, constructs, outputDir);
282
+
283
+ expect(routes).toHaveLength(1);
284
+
285
+ // Should use default aws-apigatewayv2
286
+ const handlerPath = join(outputDir, 'testEndpoint.ts');
287
+ const handlerContent = await readFile(handlerPath, 'utf-8');
288
+ expect(handlerContent).toContain('AmazonApiGatewayV2Endpoint');
289
+ },
290
+ );
291
+
292
+ itWithDir('should throw error for unsupported provider', async ({ dir }) => {
293
+ const outputDir = join(dir, 'output');
294
+ const constructs = [
295
+ createTestEndpointConstruct('testEndpoint', '/test', 'GET', dir),
296
+ ];
297
+
298
+ await expect(
299
+ generator.build(context, constructs, outputDir, {
300
+ provider: 'unsupported' as any,
301
+ }),
302
+ ).rejects.toThrow('Unsupported provider: unsupported');
303
+ });
304
+
305
+ itWithDir(
306
+ 'should generate correct import paths for nested files',
307
+ async ({ dir }) => {
308
+ const outputDir = join(dir, 'output');
309
+ const nestedDir = join(dir, 'src', 'api', 'endpoints');
310
+ await mkdir(outputDir, { recursive: true });
311
+
312
+ await createMockEndpointFile(
313
+ nestedDir,
314
+ 'deepEndpoint.ts',
315
+ 'deepEndpoint',
316
+ '/deep',
317
+ 'GET',
318
+ );
319
+
320
+ const constructs = await generator.load('**/src/api/endpoints/*.ts', dir);
321
+
322
+ await generator.build(context, constructs, outputDir, {
323
+ provider: 'aws-apigatewayv2',
324
+ });
325
+
326
+ const handlerPath = join(outputDir, 'deepEndpoint.ts');
327
+ const handlerContent = await readFile(handlerPath, 'utf-8');
328
+
329
+ // Check that relative imports are correct
330
+ expect(handlerContent).toContain(
331
+ "from '../src/api/endpoints/deepEndpoint.js'",
332
+ );
333
+ expect(handlerContent).toMatch(/from ['"]\.\.\/\.\.\/\.\.\/.*\/env['"]/);
334
+ },
335
+ );
336
+
337
+ itWithDir('should log generation progress', async ({ dir }) => {
338
+ const logSpy = vi.spyOn(console, 'log');
339
+ const outputDir = join(dir, 'output');
340
+ const routesDir = join(dir, 'routes');
341
+ await mkdir(outputDir, { recursive: true });
342
+
343
+ await Promise.all([
344
+ createMockEndpointFile(
345
+ routesDir,
346
+ 'endpoint1.ts',
347
+ 'endpoint1',
348
+ '/test1',
349
+ 'GET',
350
+ ),
351
+ createMockEndpointFile(
352
+ routesDir,
353
+ 'endpoint2.ts',
354
+ 'endpoint2',
355
+ '/test2',
356
+ 'POST',
357
+ ),
358
+ ]);
359
+
360
+ const constructs = await generator.load('**/routes/*.ts', dir);
361
+
362
+ await generator.build(context, constructs, outputDir, {
363
+ provider: 'server',
364
+ });
365
+
366
+ expect(logSpy).toHaveBeenCalledWith(
367
+ 'Generated server app with 2 endpoints',
368
+ );
369
+
370
+ logSpy.mockRestore();
371
+ });
372
+ });
@@ -0,0 +1,257 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { type Function, FunctionBuilder } from '@geekmidas/api/constructs';
4
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
5
+ import {
6
+ cleanupDir,
7
+ createMockBuildContext,
8
+ createTempDir,
9
+ } from '../../__tests__/test-helpers';
10
+ import { FunctionGenerator } from '../FunctionGenerator';
11
+ import type { GeneratedConstruct } from '../Generator';
12
+
13
+ describe('FunctionGenerator', () => {
14
+ let tempDir: string;
15
+ let outputDir: string;
16
+ let generator: FunctionGenerator;
17
+ let context: ReturnType<typeof createMockBuildContext>;
18
+
19
+ beforeEach(async () => {
20
+ tempDir = await createTempDir();
21
+ outputDir = join(tempDir, 'output');
22
+ generator = new FunctionGenerator();
23
+ context = createMockBuildContext();
24
+ });
25
+
26
+ afterEach(async () => {
27
+ await cleanupDir(tempDir);
28
+ });
29
+
30
+ describe('isConstruct', () => {
31
+ it('should identify valid functions', async () => {
32
+ // Import the actual FunctionBuilder to create a real Function instance
33
+
34
+ const { z } = await import('zod');
35
+
36
+ const testFunction = new FunctionBuilder()
37
+ .input(z.object({ name: z.string() }))
38
+ .output(z.object({ greeting: z.string() }))
39
+ .timeout(30)
40
+ .handle(async ({ input }: any) => ({
41
+ greeting: `Hello, ${input.name}!`,
42
+ }));
43
+
44
+ expect(generator.isConstruct(testFunction)).toBe(true);
45
+ });
46
+
47
+ it('should reject invalid constructs', () => {
48
+ expect(generator.isConstruct({})).toBe(false);
49
+ expect(generator.isConstruct('string')).toBe(false);
50
+ expect(generator.isConstruct(null)).toBe(false);
51
+ });
52
+ });
53
+
54
+ describe('build', () => {
55
+ const createTestFunctionConstruct = (
56
+ key: string,
57
+ timeout: number = 30,
58
+ ): GeneratedConstruct<Function<any, any, any, any>> => ({
59
+ key,
60
+ name: key.toLowerCase(),
61
+ construct: {
62
+ __IS_FUNCTION__: true,
63
+ type: 'dev.geekmidas.function.function',
64
+ timeout,
65
+ handle: async () => ({ greeting: 'Hello!' }),
66
+ } as any,
67
+ path: {
68
+ absolute: join(tempDir, `${key}.ts`),
69
+ relative: `${key}.ts`,
70
+ },
71
+ });
72
+
73
+ describe('aws-lambda provider', () => {
74
+ it('should generate function handlers', async () => {
75
+ const constructs = [
76
+ createTestFunctionConstruct('processData', 60),
77
+ createTestFunctionConstruct('sendEmail', 30),
78
+ ];
79
+
80
+ const functionInfos = await generator.build(
81
+ context,
82
+ constructs,
83
+ outputDir,
84
+ { provider: 'aws-lambda' },
85
+ );
86
+
87
+ expect(functionInfos).toHaveLength(2);
88
+ expect(functionInfos[0]).toMatchObject({
89
+ name: 'processData',
90
+ handler: expect.stringContaining('functions/processData.handler'),
91
+ timeout: 60,
92
+ });
93
+ expect(functionInfos[1]).toMatchObject({
94
+ name: 'sendEmail',
95
+ handler: expect.stringContaining('functions/sendEmail.handler'),
96
+ timeout: 30,
97
+ });
98
+
99
+ // Check that handler files were created
100
+ const processDataHandlerPath = join(
101
+ outputDir,
102
+ 'functions',
103
+ 'processData.ts',
104
+ );
105
+ const processDataContent = await readFile(
106
+ processDataHandlerPath,
107
+ 'utf-8',
108
+ );
109
+ expect(processDataContent).toContain('AWSLambdaFunction');
110
+ expect(processDataContent).toContain('import { processData }');
111
+ expect(processDataContent).toContain('import envParser');
112
+ expect(processDataContent).toContain('import logger');
113
+
114
+ const sendEmailHandlerPath = join(
115
+ outputDir,
116
+ 'functions',
117
+ 'sendEmail.ts',
118
+ );
119
+ const sendEmailContent = await readFile(sendEmailHandlerPath, 'utf-8');
120
+ expect(sendEmailContent).toContain('AWSLambdaFunction');
121
+ expect(sendEmailContent).toContain('import { sendEmail }');
122
+ });
123
+
124
+ it('should generate correct relative import paths', async () => {
125
+ const construct: GeneratedConstruct<Function<any, any, any, any>> = {
126
+ key: 'deepFunction',
127
+ name: 'deep-function',
128
+ construct: createTestFunctionConstruct('deepFunction', 45).construct,
129
+ path: {
130
+ absolute: join(tempDir, 'src/functions/deep/processor.ts'),
131
+ relative: 'src/functions/deep/processor.ts',
132
+ },
133
+ };
134
+
135
+ await generator.build(context, [construct], outputDir, {
136
+ provider: 'aws-lambda',
137
+ });
138
+
139
+ const handlerPath = join(outputDir, 'functions', 'deepFunction.ts');
140
+ const handlerContent = await readFile(handlerPath, 'utf-8');
141
+
142
+ // Check relative imports are correct - the path will be relative from outputDir
143
+ expect(handlerContent).toMatch(
144
+ /from ['"].*src\/functions\/deep\/processor\.js['"]/,
145
+ );
146
+ expect(handlerContent).toMatch(/from ['"].*\/env['"]/);
147
+ expect(handlerContent).toMatch(/from ['"].*\/logger['"]/);
148
+ });
149
+
150
+ it('should log generation progress', async () => {
151
+ const logSpy = vi.spyOn(console, 'log');
152
+
153
+ const constructs = [createTestFunctionConstruct('testFunction', 30)];
154
+
155
+ await generator.build(context, constructs, outputDir, {
156
+ provider: 'aws-lambda',
157
+ });
158
+
159
+ expect(logSpy).toHaveBeenCalledWith(
160
+ 'Generated function handler: testFunction',
161
+ );
162
+
163
+ logSpy.mockRestore();
164
+ });
165
+ });
166
+
167
+ describe('non aws-lambda provider', () => {
168
+ it('should return empty array for server provider', async () => {
169
+ const constructs = [createTestFunctionConstruct('testFunction', 30)];
170
+
171
+ const functionInfos = await generator.build(
172
+ context,
173
+ constructs,
174
+ outputDir,
175
+ { provider: 'server' },
176
+ );
177
+
178
+ expect(functionInfos).toEqual([]);
179
+ });
180
+
181
+ it('should return empty array for aws-apigatewayv1 provider', async () => {
182
+ const constructs = [createTestFunctionConstruct('testFunction', 30)];
183
+
184
+ const functionInfos = await generator.build(
185
+ context,
186
+ constructs,
187
+ outputDir,
188
+ { provider: 'aws-apigatewayv1' },
189
+ );
190
+
191
+ expect(functionInfos).toEqual([]);
192
+ });
193
+ });
194
+
195
+ it('should return empty array for empty constructs', async () => {
196
+ const functionInfos = await generator.build(context, [], outputDir, {
197
+ provider: 'aws-lambda',
198
+ });
199
+ expect(functionInfos).toEqual([]);
200
+ });
201
+
202
+ it('should use default provider when none specified', async () => {
203
+ const constructs = [createTestFunctionConstruct('defaultFunction', 30)];
204
+
205
+ const functionInfos = await generator.build(
206
+ context,
207
+ constructs,
208
+ outputDir,
209
+ );
210
+
211
+ expect(functionInfos).toHaveLength(1);
212
+ expect(functionInfos[0].name).toBe('defaultFunction');
213
+
214
+ // Check that handler was created (default is aws-lambda)
215
+ const handlerPath = join(outputDir, 'functions', 'defaultFunction.ts');
216
+ const handlerContent = await readFile(handlerPath, 'utf-8');
217
+ expect(handlerContent).toContain('AWSLambdaFunction');
218
+ });
219
+
220
+ it('should handle functions with different timeout values', async () => {
221
+ const constructs = [
222
+ createTestFunctionConstruct('quickFunction', 15),
223
+ createTestFunctionConstruct('slowFunction', 300),
224
+ ];
225
+
226
+ const functionInfos = await generator.build(
227
+ context,
228
+ constructs,
229
+ outputDir,
230
+ { provider: 'aws-lambda' },
231
+ );
232
+
233
+ expect(functionInfos[0].timeout).toBe(15);
234
+ expect(functionInfos[1].timeout).toBe(300);
235
+ });
236
+
237
+ it('should handle functions with custom environment parser patterns', async () => {
238
+ const customContext = {
239
+ ...context,
240
+ envParserImportPattern: '{ customParser as envParser }',
241
+ loggerImportPattern: '{ customLogger as logger }',
242
+ };
243
+
244
+ const constructs = [createTestFunctionConstruct('customFunction', 30)];
245
+
246
+ await generator.build(customContext, constructs, outputDir, {
247
+ provider: 'aws-lambda',
248
+ });
249
+
250
+ const handlerPath = join(outputDir, 'functions', 'customFunction.ts');
251
+ const handlerContent = await readFile(handlerPath, 'utf-8');
252
+
253
+ expect(handlerContent).toContain('import { customParser as envParser }');
254
+ expect(handlerContent).toContain('import { customLogger as logger }');
255
+ });
256
+ });
257
+ });
@@ -0,0 +1,8 @@
1
+ export { CronGenerator } from './CronGenerator';
2
+ export { EndpointGenerator } from './EndpointGenerator';
3
+ export { FunctionGenerator } from './FunctionGenerator';
4
+ export {
5
+ ConstructGenerator,
6
+ type GeneratedConstruct,
7
+ type GeneratorOptions,
8
+ } from './Generator';