@geekmidas/cli 0.0.26 → 0.2.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.
- package/FUNCTION_CRON_SUPPORT.md +266 -0
- package/README.md +84 -17
- package/dist/CronGenerator-1PflEYe2.cjs +60 -0
- package/dist/CronGenerator-1PflEYe2.cjs.map +1 -0
- package/dist/CronGenerator-DXRfHQcV.mjs +54 -0
- package/dist/CronGenerator-DXRfHQcV.mjs.map +1 -0
- package/dist/EndpointGenerator-BbGrDiCP.cjs +264 -0
- package/dist/EndpointGenerator-BbGrDiCP.cjs.map +1 -0
- package/dist/EndpointGenerator-BmZ9BxbO.mjs +258 -0
- package/dist/EndpointGenerator-BmZ9BxbO.mjs.map +1 -0
- package/dist/FunctionGenerator-Clw64SwQ.cjs +59 -0
- package/dist/FunctionGenerator-Clw64SwQ.cjs.map +1 -0
- package/dist/FunctionGenerator-DOEB_yPh.mjs +53 -0
- package/dist/FunctionGenerator-DOEB_yPh.mjs.map +1 -0
- package/dist/Generator-CDoEXCDg.cjs +47 -0
- package/dist/Generator-CDoEXCDg.cjs.map +1 -0
- package/dist/Generator-UanJW0_V.mjs +41 -0
- package/dist/Generator-UanJW0_V.mjs.map +1 -0
- package/dist/SubscriberGenerator-BfMZCVNy.cjs +204 -0
- package/dist/SubscriberGenerator-BfMZCVNy.cjs.map +1 -0
- package/dist/SubscriberGenerator-D2u00NI3.mjs +198 -0
- package/dist/SubscriberGenerator-D2u00NI3.mjs.map +1 -0
- package/dist/build/index.cjs +12 -0
- package/dist/build/index.mjs +12 -0
- package/dist/build/manifests.cjs +3 -0
- package/dist/build/manifests.mjs +3 -0
- package/dist/build/providerResolver.cjs +5 -0
- package/dist/build/providerResolver.mjs +3 -0
- package/dist/build/types.cjs +0 -0
- package/dist/build/types.mjs +0 -0
- package/dist/build-BBhlEjf5.cjs +89 -0
- package/dist/build-BBhlEjf5.cjs.map +1 -0
- package/dist/build-kY-lG30Q.mjs +83 -0
- package/dist/build-kY-lG30Q.mjs.map +1 -0
- package/dist/config-D1EpSGk6.cjs +36 -0
- package/dist/config-D1EpSGk6.cjs.map +1 -0
- package/dist/config-U-mdW-7Y.mjs +30 -0
- package/dist/config-U-mdW-7Y.mjs.map +1 -0
- package/dist/config.cjs +1 -1
- package/dist/config.mjs +1 -1
- package/dist/generators/CronGenerator.cjs +4 -0
- package/dist/generators/CronGenerator.mjs +4 -0
- package/dist/generators/EndpointGenerator.cjs +4 -0
- package/dist/generators/EndpointGenerator.mjs +4 -0
- package/dist/generators/FunctionGenerator.cjs +4 -0
- package/dist/generators/FunctionGenerator.mjs +4 -0
- package/dist/generators/Generator.cjs +3 -0
- package/dist/generators/Generator.mjs +3 -0
- package/dist/generators/SubscriberGenerator.cjs +4 -0
- package/dist/generators/SubscriberGenerator.mjs +4 -0
- package/dist/generators/index.cjs +12 -0
- package/dist/generators/index.mjs +8 -0
- package/dist/generators-CEKtVh81.cjs +0 -0
- package/dist/generators-CsLujGXs.mjs +0 -0
- package/dist/index.cjs +71 -25
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +71 -25
- package/dist/index.mjs.map +1 -0
- package/dist/manifests-BrJXpHrf.mjs +21 -0
- package/dist/manifests-BrJXpHrf.mjs.map +1 -0
- package/dist/manifests-D0saShvH.cjs +27 -0
- package/dist/manifests-D0saShvH.cjs.map +1 -0
- package/dist/{openapi-CksVdkh2.mjs → openapi-BQx3_JbM.mjs} +8 -6
- package/dist/openapi-BQx3_JbM.mjs.map +1 -0
- package/dist/{openapi-D4QQJUPY.cjs → openapi-CMLr04cz.cjs} +9 -7
- package/dist/openapi-CMLr04cz.cjs.map +1 -0
- package/dist/{openapi-react-query-DpT3XHFC.mjs → openapi-react-query-DbrWwQzb.mjs} +5 -3
- package/dist/openapi-react-query-DbrWwQzb.mjs.map +1 -0
- package/dist/{openapi-react-query-C1JLYUOs.cjs → openapi-react-query-Dvjqx_Eo.cjs} +5 -3
- package/dist/openapi-react-query-Dvjqx_Eo.cjs.map +1 -0
- package/dist/openapi-react-query.cjs +1 -1
- package/dist/openapi-react-query.mjs +1 -1
- package/dist/openapi.cjs +4 -3
- package/dist/openapi.mjs +4 -3
- package/dist/providerResolver-B_TjNF0_.mjs +96 -0
- package/dist/providerResolver-B_TjNF0_.mjs.map +1 -0
- package/dist/providerResolver-DgvzNfP4.cjs +114 -0
- package/dist/providerResolver-DgvzNfP4.cjs.map +1 -0
- package/examples/cron-example.ts +45 -0
- package/examples/function-example.ts +40 -0
- package/examples/gkm.config.json +22 -0
- package/examples/gkm.minimal.config.json +7 -0
- package/examples/gkm.production.config.json +27 -0
- package/examples/logger.ts +1 -1
- package/package.json +38 -14
- package/src/__tests__/config.spec.ts +110 -0
- package/src/__tests__/openapi-react-query.spec.ts +506 -0
- package/src/__tests__/openapi.spec.ts +362 -0
- package/src/__tests__/test-helpers.ts +180 -0
- package/src/build/__tests__/index-new.spec.ts +577 -0
- package/src/build/index.ts +197 -0
- package/src/build/manifests.ts +35 -0
- package/src/build/providerResolver.ts +184 -0
- package/src/build/types.ts +37 -0
- package/src/config.ts +14 -6
- package/src/generators/CronGenerator.ts +98 -0
- package/src/generators/EndpointGenerator.ts +389 -0
- package/src/generators/FunctionGenerator.ts +97 -0
- package/src/generators/Generator.ts +95 -0
- package/src/generators/SubscriberGenerator.ts +271 -0
- package/src/generators/__tests__/CronGenerator.spec.ts +445 -0
- package/src/generators/__tests__/EndpointGenerator.spec.ts +394 -0
- package/src/generators/__tests__/FunctionGenerator.spec.ts +256 -0
- package/src/generators/__tests__/SubscriberGenerator.spec.ts +341 -0
- package/src/generators/index.ts +9 -0
- package/src/index.ts +57 -22
- package/src/openapi-react-query.ts +2 -1
- package/src/openapi.ts +5 -4
- package/src/types.ts +91 -2
- package/dist/build-BTggTCYL.cjs +0 -176
- package/dist/build-Ca4P6_lY.mjs +0 -170
- package/dist/build.cjs +0 -5
- package/dist/build.mjs +0 -5
- package/dist/config-BNqUMsvc.cjs +0 -24
- package/dist/config-BciAdY6_.mjs +0 -18
- package/dist/loadEndpoints-BBIavB9h.cjs +0 -37
- package/dist/loadEndpoints-DAZ53Og2.mjs +0 -31
- package/dist/loadEndpoints.cjs +0 -3
- package/dist/loadEndpoints.mjs +0 -3
- package/src/build.ts +0 -305
- package/src/loadEndpoints.ts +0 -48
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join, relative } from 'node:path';
|
|
3
|
+
import { Endpoint } from '@geekmidas/constructs/endpoints';
|
|
4
|
+
import type { BuildContext } from '../build/types';
|
|
5
|
+
import type { LegacyProvider, RouteInfo } from '../types';
|
|
6
|
+
import {
|
|
7
|
+
ConstructGenerator,
|
|
8
|
+
type GeneratedConstruct,
|
|
9
|
+
type GeneratorOptions,
|
|
10
|
+
} from './Generator';
|
|
11
|
+
|
|
12
|
+
export class EndpointGenerator extends ConstructGenerator<
|
|
13
|
+
Endpoint<any, any, any, any, any, any>,
|
|
14
|
+
RouteInfo[]
|
|
15
|
+
> {
|
|
16
|
+
isConstruct(value: any): value is Endpoint<any, any, any, any, any, any> {
|
|
17
|
+
return Endpoint.isEndpoint(value);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async build(
|
|
21
|
+
context: BuildContext,
|
|
22
|
+
constructs: GeneratedConstruct<Endpoint<any, any, any, any, any, any>>[],
|
|
23
|
+
outputDir: string,
|
|
24
|
+
options?: GeneratorOptions,
|
|
25
|
+
): Promise<RouteInfo[]> {
|
|
26
|
+
const provider = options?.provider || 'aws-apigatewayv2';
|
|
27
|
+
const enableOpenApi = options?.enableOpenApi || false;
|
|
28
|
+
const logger = console;
|
|
29
|
+
const routes: RouteInfo[] = [];
|
|
30
|
+
|
|
31
|
+
if (constructs.length === 0) {
|
|
32
|
+
return routes;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (provider === 'server') {
|
|
36
|
+
// Generate endpoints.ts and app.ts
|
|
37
|
+
await this.generateEndpointsFile(outputDir, constructs, context);
|
|
38
|
+
const appFile = await this.generateAppFile(outputDir, context);
|
|
39
|
+
|
|
40
|
+
routes.push({
|
|
41
|
+
path: '*',
|
|
42
|
+
method: 'ALL',
|
|
43
|
+
handler: relative(process.cwd(), appFile),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
logger.log(
|
|
47
|
+
`Generated server with ${constructs.length} endpoints${enableOpenApi ? ' (OpenAPI enabled)' : ''}`,
|
|
48
|
+
);
|
|
49
|
+
} else if (provider === 'aws-lambda') {
|
|
50
|
+
// For aws-lambda, create routes subdirectory
|
|
51
|
+
const routesDir = join(outputDir, 'routes');
|
|
52
|
+
await mkdir(routesDir, { recursive: true });
|
|
53
|
+
|
|
54
|
+
// Generate individual handlers for API Gateway routes
|
|
55
|
+
for (const { key, construct, path } of constructs) {
|
|
56
|
+
const handlerFile = await this.generateHandlerFile(
|
|
57
|
+
routesDir,
|
|
58
|
+
path.relative,
|
|
59
|
+
key,
|
|
60
|
+
'aws-apigatewayv2',
|
|
61
|
+
construct,
|
|
62
|
+
context,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const routeInfo: RouteInfo = {
|
|
66
|
+
path: construct._path,
|
|
67
|
+
method: construct.method,
|
|
68
|
+
handler: relative(process.cwd(), handlerFile).replace(
|
|
69
|
+
/\.ts$/,
|
|
70
|
+
'.handler',
|
|
71
|
+
),
|
|
72
|
+
environment: await construct.getEnvironment(),
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
routes.push(routeInfo);
|
|
76
|
+
logger.log(
|
|
77
|
+
`Generated handler for ${routeInfo.method} ${routeInfo.path}`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
// Generate individual handler files for AWS API Gateway providers
|
|
82
|
+
for (const { key, construct, path } of constructs) {
|
|
83
|
+
const handlerFile = await this.generateHandlerFile(
|
|
84
|
+
outputDir,
|
|
85
|
+
path.relative,
|
|
86
|
+
key,
|
|
87
|
+
provider,
|
|
88
|
+
construct,
|
|
89
|
+
context,
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const routeInfo: RouteInfo = {
|
|
93
|
+
path: construct._path,
|
|
94
|
+
method: construct.method,
|
|
95
|
+
handler: relative(process.cwd(), handlerFile).replace(
|
|
96
|
+
/\.ts$/,
|
|
97
|
+
'.handler',
|
|
98
|
+
),
|
|
99
|
+
environment: await construct.getEnvironment(),
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
routes.push(routeInfo);
|
|
103
|
+
logger.log(
|
|
104
|
+
`Generated handler for ${routeInfo.method} ${routeInfo.path}`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return routes;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private async generateHandlerFile(
|
|
113
|
+
outputDir: string,
|
|
114
|
+
sourceFile: string,
|
|
115
|
+
exportName: string,
|
|
116
|
+
provider: LegacyProvider,
|
|
117
|
+
_endpoint: Endpoint<any, any, any, any, any, any>,
|
|
118
|
+
context: BuildContext,
|
|
119
|
+
): Promise<string> {
|
|
120
|
+
const handlerFileName = `${exportName}.ts`;
|
|
121
|
+
const handlerPath = join(outputDir, handlerFileName);
|
|
122
|
+
|
|
123
|
+
const relativePath = relative(dirname(handlerPath), sourceFile);
|
|
124
|
+
const importPath = relativePath.replace(/\.ts$/, '.js');
|
|
125
|
+
|
|
126
|
+
const relativeEnvParserPath = relative(
|
|
127
|
+
dirname(handlerPath),
|
|
128
|
+
context.envParserPath,
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
let content: string;
|
|
132
|
+
|
|
133
|
+
switch (provider) {
|
|
134
|
+
case 'aws-apigatewayv1':
|
|
135
|
+
content = this.generateAWSApiGatewayV1Handler(
|
|
136
|
+
importPath,
|
|
137
|
+
exportName,
|
|
138
|
+
relativeEnvParserPath,
|
|
139
|
+
context.envParserImportPattern,
|
|
140
|
+
);
|
|
141
|
+
break;
|
|
142
|
+
case 'aws-apigatewayv2':
|
|
143
|
+
content = this.generateAWSApiGatewayV2Handler(
|
|
144
|
+
importPath,
|
|
145
|
+
exportName,
|
|
146
|
+
relativeEnvParserPath,
|
|
147
|
+
context.envParserImportPattern,
|
|
148
|
+
);
|
|
149
|
+
break;
|
|
150
|
+
case 'server':
|
|
151
|
+
content = this.generateServerHandler(importPath, exportName);
|
|
152
|
+
break;
|
|
153
|
+
default:
|
|
154
|
+
throw new Error(`Unsupported provider: ${provider}`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
await writeFile(handlerPath, content);
|
|
158
|
+
return handlerPath;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private async generateEndpointsFile(
|
|
162
|
+
outputDir: string,
|
|
163
|
+
endpoints: GeneratedConstruct<Endpoint<any, any, any, any, any, any>>[],
|
|
164
|
+
context: BuildContext,
|
|
165
|
+
): Promise<string> {
|
|
166
|
+
const endpointsFileName = 'endpoints.ts';
|
|
167
|
+
const endpointsPath = join(outputDir, endpointsFileName);
|
|
168
|
+
|
|
169
|
+
// Group imports by file
|
|
170
|
+
const importsByFile = new Map<string, string[]>();
|
|
171
|
+
|
|
172
|
+
for (const { path, key } of endpoints) {
|
|
173
|
+
const relativePath = relative(dirname(endpointsPath), path.relative);
|
|
174
|
+
const importPath = relativePath.replace(/\.ts$/, '.js');
|
|
175
|
+
|
|
176
|
+
if (!importsByFile.has(importPath)) {
|
|
177
|
+
importsByFile.set(importPath, []);
|
|
178
|
+
}
|
|
179
|
+
importsByFile.get(importPath)!.push(key);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Generate import statements
|
|
183
|
+
const imports = Array.from(importsByFile.entries())
|
|
184
|
+
.map(
|
|
185
|
+
([importPath, exports]) =>
|
|
186
|
+
`import { ${exports.join(', ')} } from '${importPath}';`,
|
|
187
|
+
)
|
|
188
|
+
.join('\n');
|
|
189
|
+
|
|
190
|
+
const allExportNames = endpoints.map(({ key }) => key);
|
|
191
|
+
|
|
192
|
+
const content = `import type { EnvironmentParser } from '@geekmidas/envkit';
|
|
193
|
+
import type { Logger } from '@geekmidas/logger';
|
|
194
|
+
import { HonoEndpoint } from '@geekmidas/constructs/hono';
|
|
195
|
+
import { Endpoint } from '@geekmidas/constructs/endpoints';
|
|
196
|
+
import { ServiceDiscovery } from '@geekmidas/services';
|
|
197
|
+
import type { Hono } from 'hono';
|
|
198
|
+
${imports}
|
|
199
|
+
|
|
200
|
+
const endpoints: Endpoint<any, any, any, any, any, any, any, any>[] = [
|
|
201
|
+
${allExportNames.join(',\n ')}
|
|
202
|
+
];
|
|
203
|
+
|
|
204
|
+
export function setupEndpoints(
|
|
205
|
+
app: Hono,
|
|
206
|
+
envParser: EnvironmentParser<any>,
|
|
207
|
+
logger: Logger,
|
|
208
|
+
enableOpenApi: boolean = true,
|
|
209
|
+
): void {
|
|
210
|
+
const serviceDiscovery = ServiceDiscovery.getInstance(
|
|
211
|
+
logger,
|
|
212
|
+
envParser
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
// Configure OpenAPI options based on enableOpenApi flag
|
|
216
|
+
const openApiOptions: any = enableOpenApi ? {
|
|
217
|
+
docsPath: '/docs',
|
|
218
|
+
openApiOptions: {
|
|
219
|
+
title: 'API Documentation',
|
|
220
|
+
version: '1.0.0',
|
|
221
|
+
description: 'Generated API documentation'
|
|
222
|
+
}
|
|
223
|
+
} : { docsPath: false };
|
|
224
|
+
|
|
225
|
+
HonoEndpoint.addRoutes(endpoints, serviceDiscovery, app, openApiOptions);
|
|
226
|
+
}
|
|
227
|
+
`;
|
|
228
|
+
|
|
229
|
+
await writeFile(endpointsPath, content);
|
|
230
|
+
|
|
231
|
+
return endpointsPath;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private async generateAppFile(
|
|
235
|
+
outputDir: string,
|
|
236
|
+
context: BuildContext,
|
|
237
|
+
): Promise<string> {
|
|
238
|
+
const appFileName = 'app.ts';
|
|
239
|
+
const appPath = join(outputDir, appFileName);
|
|
240
|
+
|
|
241
|
+
const relativeLoggerPath = relative(dirname(appPath), context.loggerPath);
|
|
242
|
+
|
|
243
|
+
const relativeEnvParserPath = relative(
|
|
244
|
+
dirname(appPath),
|
|
245
|
+
context.envParserPath,
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
const content = `/**
|
|
249
|
+
* Generated server application
|
|
250
|
+
*
|
|
251
|
+
* ⚠️ WARNING: This is for LOCAL DEVELOPMENT ONLY
|
|
252
|
+
* The subscriber polling mechanism is not production-ready.
|
|
253
|
+
* For production, use AWS Lambda with SQS/SNS event sources.
|
|
254
|
+
*/
|
|
255
|
+
import { Hono } from 'hono';
|
|
256
|
+
import type { Hono as HonoType } from 'hono';
|
|
257
|
+
import { setupEndpoints } from './endpoints.js';
|
|
258
|
+
import { setupSubscribers } from './subscribers.js';
|
|
259
|
+
import ${context.envParserImportPattern} from '${relativeEnvParserPath}';
|
|
260
|
+
import ${context.loggerImportPattern} from '${relativeLoggerPath}';
|
|
261
|
+
|
|
262
|
+
export interface ServerApp {
|
|
263
|
+
app: HonoType;
|
|
264
|
+
start: (options?: {
|
|
265
|
+
port?: number;
|
|
266
|
+
serve: (app: HonoType, port: number) => void | Promise<void>;
|
|
267
|
+
}) => Promise<void>;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Create and configure the Hono application
|
|
272
|
+
*
|
|
273
|
+
* @param app - Optional Hono app instance to configure (creates new one if not provided)
|
|
274
|
+
* @param enableOpenApi - Enable OpenAPI documentation (default: true)
|
|
275
|
+
* @returns Server app with configured Hono app and start function
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* // With Bun
|
|
279
|
+
* import { createApp } from './.gkm/server/app.js';
|
|
280
|
+
*
|
|
281
|
+
* const { app, start } = createApp();
|
|
282
|
+
*
|
|
283
|
+
* await start({
|
|
284
|
+
* port: 3000,
|
|
285
|
+
* serve: (app, port) => {
|
|
286
|
+
* Bun.serve({ port, fetch: app.fetch });
|
|
287
|
+
* }
|
|
288
|
+
* });
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* // With Node.js (using @hono/node-server)
|
|
292
|
+
* import { serve } from '@hono/node-server';
|
|
293
|
+
* import { createApp } from './.gkm/server/app.js';
|
|
294
|
+
*
|
|
295
|
+
* const { app, start } = createApp();
|
|
296
|
+
*
|
|
297
|
+
* await start({
|
|
298
|
+
* port: 3000,
|
|
299
|
+
* serve: (app, port) => {
|
|
300
|
+
* serve({ fetch: app.fetch, port });
|
|
301
|
+
* }
|
|
302
|
+
* });
|
|
303
|
+
*/
|
|
304
|
+
export function createApp(app?: HonoType, enableOpenApi: boolean = true): ServerApp {
|
|
305
|
+
const honoApp = app || new Hono();
|
|
306
|
+
|
|
307
|
+
// Setup HTTP endpoints
|
|
308
|
+
setupEndpoints(honoApp, envParser, logger, enableOpenApi);
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
app: honoApp,
|
|
312
|
+
async start(options) {
|
|
313
|
+
if (!options?.serve) {
|
|
314
|
+
throw new Error(
|
|
315
|
+
'serve function is required. Pass a serve function for your runtime:\\n' +
|
|
316
|
+
' - Bun: (app, port) => Bun.serve({ port, fetch: app.fetch })\\n' +
|
|
317
|
+
' - Node: (app, port) => serve({ fetch: app.fetch, port })'
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const port = options.port ?? 3000;
|
|
322
|
+
|
|
323
|
+
// Start subscribers in background (non-blocking, local development only)
|
|
324
|
+
await setupSubscribers(envParser, logger).catch((error) => {
|
|
325
|
+
logger.error({ error }, 'Failed to start subscribers');
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
logger.info({ port }, 'Starting server');
|
|
329
|
+
|
|
330
|
+
// Start HTTP server using provided serve function
|
|
331
|
+
await options.serve(honoApp, port);
|
|
332
|
+
|
|
333
|
+
logger.info({ port }, 'Server started');
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Default export for convenience
|
|
339
|
+
export default createApp;
|
|
340
|
+
`;
|
|
341
|
+
|
|
342
|
+
await writeFile(appPath, content);
|
|
343
|
+
|
|
344
|
+
return appPath;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
private generateAWSApiGatewayV1Handler(
|
|
348
|
+
importPath: string,
|
|
349
|
+
exportName: string,
|
|
350
|
+
envParserPath: string,
|
|
351
|
+
envParserImportPattern: string,
|
|
352
|
+
): string {
|
|
353
|
+
return `import { AmazonApiGatewayV1Endpoint } from '@geekmidas/constructs/aws';
|
|
354
|
+
import { ${exportName} } from '${importPath}';
|
|
355
|
+
import ${envParserImportPattern} from '${envParserPath}';
|
|
356
|
+
|
|
357
|
+
const adapter = new AmazonApiGatewayV1Endpoint(envParser, ${exportName});
|
|
358
|
+
|
|
359
|
+
export const handler = adapter.handler;
|
|
360
|
+
`;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private generateAWSApiGatewayV2Handler(
|
|
364
|
+
importPath: string,
|
|
365
|
+
exportName: string,
|
|
366
|
+
envParserPath: string,
|
|
367
|
+
envParserImportPattern: string,
|
|
368
|
+
): string {
|
|
369
|
+
return `import { AmazonApiGatewayV2Endpoint } from '@geekmidas/constructs/aws';
|
|
370
|
+
import { ${exportName} } from '${importPath}';
|
|
371
|
+
import ${envParserImportPattern} from '${envParserPath}';
|
|
372
|
+
|
|
373
|
+
const adapter = new AmazonApiGatewayV2Endpoint(envParser, ${exportName});
|
|
374
|
+
|
|
375
|
+
export const handler = adapter.handler;
|
|
376
|
+
`;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
private generateServerHandler(
|
|
380
|
+
importPath: string,
|
|
381
|
+
exportName: string,
|
|
382
|
+
): string {
|
|
383
|
+
return `import { ${exportName} } from '${importPath}';
|
|
384
|
+
|
|
385
|
+
// Server handler - implement based on your server framework
|
|
386
|
+
export const handler = ${exportName};
|
|
387
|
+
`;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join, relative } from 'node:path';
|
|
3
|
+
import { Function } from '@geekmidas/constructs/functions';
|
|
4
|
+
import type { BuildContext } from '../build/types';
|
|
5
|
+
import type { FunctionInfo } from '../types';
|
|
6
|
+
import {
|
|
7
|
+
ConstructGenerator,
|
|
8
|
+
type GeneratedConstruct,
|
|
9
|
+
type GeneratorOptions,
|
|
10
|
+
} from './Generator';
|
|
11
|
+
|
|
12
|
+
export class FunctionGenerator extends ConstructGenerator<
|
|
13
|
+
Function<any, any, any, any>,
|
|
14
|
+
FunctionInfo[]
|
|
15
|
+
> {
|
|
16
|
+
isConstruct(value: any): value is Function<any, any, any, any> {
|
|
17
|
+
return Function.isFunction(value);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async build(
|
|
21
|
+
context: BuildContext,
|
|
22
|
+
constructs: GeneratedConstruct<Function<any, any, any, any>>[],
|
|
23
|
+
outputDir: string,
|
|
24
|
+
options?: GeneratorOptions,
|
|
25
|
+
): Promise<FunctionInfo[]> {
|
|
26
|
+
const provider = options?.provider || 'aws-lambda';
|
|
27
|
+
const logger = console;
|
|
28
|
+
const functionInfos: FunctionInfo[] = [];
|
|
29
|
+
|
|
30
|
+
if (constructs.length === 0 || provider !== 'aws-lambda') {
|
|
31
|
+
return functionInfos;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Create functions subdirectory
|
|
35
|
+
const functionsDir = join(outputDir, 'functions');
|
|
36
|
+
await mkdir(functionsDir, { recursive: true });
|
|
37
|
+
|
|
38
|
+
// Generate function handlers
|
|
39
|
+
for (const { key, construct, path } of constructs) {
|
|
40
|
+
const handlerFile = await this.generateFunctionHandler(
|
|
41
|
+
functionsDir,
|
|
42
|
+
path.relative,
|
|
43
|
+
key,
|
|
44
|
+
context,
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
functionInfos.push({
|
|
48
|
+
name: key,
|
|
49
|
+
handler: relative(process.cwd(), handlerFile).replace(
|
|
50
|
+
/\.ts$/,
|
|
51
|
+
'.handler',
|
|
52
|
+
),
|
|
53
|
+
timeout: construct.timeout,
|
|
54
|
+
environment: await construct.getEnvironment(),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
logger.log(`Generated function handler: ${key}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return functionInfos;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async generateFunctionHandler(
|
|
64
|
+
outputDir: string,
|
|
65
|
+
sourceFile: string,
|
|
66
|
+
exportName: string,
|
|
67
|
+
context: BuildContext,
|
|
68
|
+
): Promise<string> {
|
|
69
|
+
const handlerFileName = `${exportName}.ts`;
|
|
70
|
+
const handlerPath = join(outputDir, handlerFileName);
|
|
71
|
+
|
|
72
|
+
const relativePath = relative(dirname(handlerPath), sourceFile);
|
|
73
|
+
const importPath = relativePath.replace(/\.ts$/, '.js');
|
|
74
|
+
|
|
75
|
+
const relativeEnvParserPath = relative(
|
|
76
|
+
dirname(handlerPath),
|
|
77
|
+
context.envParserPath,
|
|
78
|
+
);
|
|
79
|
+
const relativeLoggerPath = relative(
|
|
80
|
+
dirname(handlerPath),
|
|
81
|
+
context.loggerPath,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const content = `import { AWSLambdaFunction } from '@geekmidas/constructs/functions';
|
|
85
|
+
import { ${exportName} } from '${importPath}';
|
|
86
|
+
import ${context.envParserImportPattern} from '${relativeEnvParserPath}';
|
|
87
|
+
import ${context.loggerImportPattern} from '${relativeLoggerPath}';
|
|
88
|
+
|
|
89
|
+
const adapter = new AWSLambdaFunction(envParser, ${exportName});
|
|
90
|
+
|
|
91
|
+
export const handler = adapter.handler;
|
|
92
|
+
`;
|
|
93
|
+
|
|
94
|
+
await writeFile(handlerPath, content);
|
|
95
|
+
return handlerPath;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { relative } from 'path';
|
|
2
|
+
import type { Construct } from '@geekmidas/constructs';
|
|
3
|
+
import fg from 'fast-glob';
|
|
4
|
+
import kebabCase from 'lodash.kebabcase';
|
|
5
|
+
import type { BuildContext } from '../build/types';
|
|
6
|
+
import type { LegacyProvider, Routes } from '../types';
|
|
7
|
+
|
|
8
|
+
export interface GeneratorOptions {
|
|
9
|
+
provider?: LegacyProvider;
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export abstract class ConstructGenerator<T extends Construct, R = void> {
|
|
14
|
+
abstract isConstruct(value: any): value is T;
|
|
15
|
+
|
|
16
|
+
static async build<T extends Construct, R = void>(
|
|
17
|
+
context: BuildContext,
|
|
18
|
+
outputDir: string,
|
|
19
|
+
generator: ConstructGenerator<T, R>,
|
|
20
|
+
patterns?: Routes,
|
|
21
|
+
options?: GeneratorOptions,
|
|
22
|
+
): Promise<R> {
|
|
23
|
+
const constructs = await generator.load(patterns);
|
|
24
|
+
return generator.build(context, constructs, outputDir, options);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
abstract build(
|
|
28
|
+
context: BuildContext,
|
|
29
|
+
constructs: GeneratedConstruct<T>[],
|
|
30
|
+
outputDir: string,
|
|
31
|
+
options?: GeneratorOptions,
|
|
32
|
+
): Promise<R>;
|
|
33
|
+
|
|
34
|
+
async load(
|
|
35
|
+
patterns?: Routes,
|
|
36
|
+
cwd = process.cwd(),
|
|
37
|
+
): Promise<GeneratedConstruct<T>[]> {
|
|
38
|
+
const logger = console;
|
|
39
|
+
|
|
40
|
+
// Normalize patterns to array
|
|
41
|
+
const globPatterns = Array.isArray(patterns)
|
|
42
|
+
? patterns
|
|
43
|
+
: patterns
|
|
44
|
+
? [patterns]
|
|
45
|
+
: [];
|
|
46
|
+
|
|
47
|
+
// Find all files
|
|
48
|
+
const files = fg.stream(globPatterns, {
|
|
49
|
+
cwd,
|
|
50
|
+
absolute: true,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Load constructs
|
|
54
|
+
const constructs: GeneratedConstruct<T>[] = [];
|
|
55
|
+
|
|
56
|
+
for await (const f of files) {
|
|
57
|
+
try {
|
|
58
|
+
const file = f.toString();
|
|
59
|
+
const module = await import(file);
|
|
60
|
+
|
|
61
|
+
// Check all exports for constructs
|
|
62
|
+
for (const [key, construct] of Object.entries(module)) {
|
|
63
|
+
if (this.isConstruct(construct)) {
|
|
64
|
+
constructs.push({
|
|
65
|
+
key,
|
|
66
|
+
name: kebabCase(key),
|
|
67
|
+
construct,
|
|
68
|
+
path: {
|
|
69
|
+
absolute: file,
|
|
70
|
+
relative: relative(process.cwd(), file),
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} catch (error) {
|
|
76
|
+
logger.warn(`Failed to load ${f}:`, (error as Error).message);
|
|
77
|
+
throw new Error(
|
|
78
|
+
'Failed to load constructs. Please check the logs for details.',
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return constructs;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface GeneratedConstruct<T extends Construct> {
|
|
88
|
+
key: string;
|
|
89
|
+
name: string;
|
|
90
|
+
construct: T;
|
|
91
|
+
path: {
|
|
92
|
+
absolute: string;
|
|
93
|
+
relative: string;
|
|
94
|
+
};
|
|
95
|
+
}
|