@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,271 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join, relative } from 'node:path';
|
|
3
|
+
import { Subscriber } from '@geekmidas/constructs/subscribers';
|
|
4
|
+
import type { BuildContext } from '../build/types';
|
|
5
|
+
import type { SubscriberInfo } from '../types';
|
|
6
|
+
import {
|
|
7
|
+
ConstructGenerator,
|
|
8
|
+
type GeneratedConstruct,
|
|
9
|
+
type GeneratorOptions,
|
|
10
|
+
} from './Generator';
|
|
11
|
+
|
|
12
|
+
export class SubscriberGenerator extends ConstructGenerator<
|
|
13
|
+
Subscriber<any, any, any, any, any, any>,
|
|
14
|
+
SubscriberInfo[]
|
|
15
|
+
> {
|
|
16
|
+
isConstruct(value: any): value is Subscriber<any, any, any, any, any, any> {
|
|
17
|
+
return Subscriber.isSubscriber(value);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async build(
|
|
21
|
+
context: BuildContext,
|
|
22
|
+
constructs: GeneratedConstruct<Subscriber<any, any, any, any, any, any>>[],
|
|
23
|
+
outputDir: string,
|
|
24
|
+
options?: GeneratorOptions,
|
|
25
|
+
): Promise<SubscriberInfo[]> {
|
|
26
|
+
const provider = options?.provider || 'aws-lambda';
|
|
27
|
+
const logger = console;
|
|
28
|
+
const subscriberInfos: SubscriberInfo[] = [];
|
|
29
|
+
|
|
30
|
+
if (constructs.length === 0) {
|
|
31
|
+
return subscriberInfos;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (provider === 'server') {
|
|
35
|
+
// Generate subscribers.ts for server-based polling
|
|
36
|
+
await this.generateServerSubscribersFile(outputDir, constructs);
|
|
37
|
+
|
|
38
|
+
logger.log(
|
|
39
|
+
`Generated server subscribers file with ${constructs.length} subscribers (polling mode)`,
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// Return empty array as server subscribers don't have individual handlers
|
|
43
|
+
return subscriberInfos;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (provider !== 'aws-lambda') {
|
|
47
|
+
return subscriberInfos;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Create subscribers subdirectory
|
|
51
|
+
const subscribersDir = join(outputDir, 'subscribers');
|
|
52
|
+
await mkdir(subscribersDir, { recursive: true });
|
|
53
|
+
|
|
54
|
+
// Generate subscriber handlers
|
|
55
|
+
for (const { key, construct, path } of constructs) {
|
|
56
|
+
const handlerFile = await this.generateSubscriberHandler(
|
|
57
|
+
subscribersDir,
|
|
58
|
+
path.relative,
|
|
59
|
+
key,
|
|
60
|
+
construct,
|
|
61
|
+
context,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
subscriberInfos.push({
|
|
65
|
+
name: key,
|
|
66
|
+
handler: relative(process.cwd(), handlerFile).replace(
|
|
67
|
+
/\.ts$/,
|
|
68
|
+
'.handler',
|
|
69
|
+
),
|
|
70
|
+
subscribedEvents: construct.subscribedEvents || [],
|
|
71
|
+
timeout: construct.timeout,
|
|
72
|
+
environment: await construct.getEnvironment(),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
logger.log(`Generated subscriber handler: ${key}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return subscriberInfos;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private async generateSubscriberHandler(
|
|
82
|
+
outputDir: string,
|
|
83
|
+
sourceFile: string,
|
|
84
|
+
exportName: string,
|
|
85
|
+
_subscriber: Subscriber<any, any, any, any, any, any>,
|
|
86
|
+
context: BuildContext,
|
|
87
|
+
): Promise<string> {
|
|
88
|
+
const handlerFileName = `${exportName}.ts`;
|
|
89
|
+
const handlerPath = join(outputDir, handlerFileName);
|
|
90
|
+
|
|
91
|
+
const relativePath = relative(dirname(handlerPath), sourceFile);
|
|
92
|
+
const importPath = relativePath.replace(/\.ts$/, '.js');
|
|
93
|
+
|
|
94
|
+
const relativeEnvParserPath = relative(
|
|
95
|
+
dirname(handlerPath),
|
|
96
|
+
context.envParserPath,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const content = `import { AWSLambdaSubscriber } from '@geekmidas/constructs/aws';
|
|
100
|
+
import { ${exportName} } from '${importPath}';
|
|
101
|
+
import ${context.envParserImportPattern} from '${relativeEnvParserPath}';
|
|
102
|
+
|
|
103
|
+
const adapter = new AWSLambdaSubscriber(envParser, ${exportName});
|
|
104
|
+
|
|
105
|
+
export const handler = adapter.handler;
|
|
106
|
+
`;
|
|
107
|
+
|
|
108
|
+
await writeFile(handlerPath, content);
|
|
109
|
+
return handlerPath;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private async generateServerSubscribersFile(
|
|
113
|
+
outputDir: string,
|
|
114
|
+
subscribers: GeneratedConstruct<Subscriber<any, any, any, any, any, any>>[],
|
|
115
|
+
): Promise<string> {
|
|
116
|
+
const subscribersFileName = 'subscribers.ts';
|
|
117
|
+
const subscribersPath = join(outputDir, subscribersFileName);
|
|
118
|
+
|
|
119
|
+
// Group imports by file
|
|
120
|
+
const importsByFile = new Map<string, string[]>();
|
|
121
|
+
|
|
122
|
+
for (const { path, key } of subscribers) {
|
|
123
|
+
const relativePath = relative(dirname(subscribersPath), path.relative);
|
|
124
|
+
const importPath = relativePath.replace(/\.ts$/, '.js');
|
|
125
|
+
|
|
126
|
+
if (!importsByFile.has(importPath)) {
|
|
127
|
+
importsByFile.set(importPath, []);
|
|
128
|
+
}
|
|
129
|
+
importsByFile.get(importPath)!.push(key);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Generate import statements
|
|
133
|
+
const imports = Array.from(importsByFile.entries())
|
|
134
|
+
.map(
|
|
135
|
+
([importPath, exports]) =>
|
|
136
|
+
`import { ${exports.join(', ')} } from '${importPath}';`,
|
|
137
|
+
)
|
|
138
|
+
.join('\n');
|
|
139
|
+
|
|
140
|
+
const allExportNames = subscribers.map(({ key }) => key);
|
|
141
|
+
|
|
142
|
+
const content = `/**
|
|
143
|
+
* Generated subscribers setup
|
|
144
|
+
*
|
|
145
|
+
* ⚠️ WARNING: This is for LOCAL DEVELOPMENT ONLY
|
|
146
|
+
* This uses event polling which is not suitable for production.
|
|
147
|
+
*
|
|
148
|
+
* For production, use AWS Lambda with SQS/SNS event source mappings.
|
|
149
|
+
* Lambda automatically:
|
|
150
|
+
* - Scales based on queue depth
|
|
151
|
+
* - Handles batch processing and retries
|
|
152
|
+
* - Manages dead letter queues
|
|
153
|
+
* - Provides better cost optimization
|
|
154
|
+
*
|
|
155
|
+
* This polling implementation is useful for:
|
|
156
|
+
* - Local development and testing
|
|
157
|
+
* - Understanding event flow without Lambda deployment
|
|
158
|
+
*
|
|
159
|
+
* Supported connection strings:
|
|
160
|
+
* - sqs://region/account-id/queue-name (SQS queue)
|
|
161
|
+
* - sns://region/account-id/topic-name (SNS topic)
|
|
162
|
+
* - rabbitmq://host:port/queue-name (RabbitMQ)
|
|
163
|
+
* - basic://in-memory (In-memory for testing)
|
|
164
|
+
*/
|
|
165
|
+
import type { EnvironmentParser } from '@geekmidas/envkit';
|
|
166
|
+
import type { Logger } from '@geekmidas/logger';
|
|
167
|
+
import { EventConnectionFactory, Subscriber } from '@geekmidas/events';
|
|
168
|
+
import type { EventConnection, EventSubscriber } from '@geekmidas/events';
|
|
169
|
+
import { ServiceDiscovery } from '@geekmidas/services';
|
|
170
|
+
${imports}
|
|
171
|
+
|
|
172
|
+
const subscribers = [
|
|
173
|
+
${allExportNames.join(',\n ')}
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
const activeSubscribers: EventSubscriber<any>[] = [];
|
|
177
|
+
|
|
178
|
+
export async function setupSubscribers(
|
|
179
|
+
envParser: EnvironmentParser<any>,
|
|
180
|
+
logger: Logger,
|
|
181
|
+
): Promise<void> {
|
|
182
|
+
logger.info('Setting up subscribers in polling mode (local development)');
|
|
183
|
+
|
|
184
|
+
const config = envParser.create((get) => ({
|
|
185
|
+
connectionString: get('EVENT_SUBSCRIBER_CONNECTION_STRING').string().optional(),
|
|
186
|
+
})).parse();
|
|
187
|
+
|
|
188
|
+
if (!config.connectionString) {
|
|
189
|
+
logger.warn('EVENT_SUBSCRIBER_CONNECTION_STRING not configured, skipping subscriber setup');
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const serviceDiscovery = ServiceDiscovery.getInstance(logger, envParser);
|
|
194
|
+
|
|
195
|
+
// Create connection once, outside the loop (more efficient)
|
|
196
|
+
// EventConnectionFactory automatically determines the right connection type
|
|
197
|
+
let connection: EventConnection;
|
|
198
|
+
try {
|
|
199
|
+
connection = await EventConnectionFactory.fromConnectionString(config.connectionString);
|
|
200
|
+
|
|
201
|
+
const connectionType = new URL(config.connectionString).protocol.replace(':', '');
|
|
202
|
+
logger.info({ connectionType }, 'Created shared event connection');
|
|
203
|
+
} catch (error) {
|
|
204
|
+
logger.error({ error }, 'Failed to create event connection');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
for (const subscriber of subscribers) {
|
|
209
|
+
try {
|
|
210
|
+
// Create subscriber from shared connection
|
|
211
|
+
const eventSubscriber = await Subscriber.fromConnection(connection);
|
|
212
|
+
|
|
213
|
+
// Register services
|
|
214
|
+
const services = subscriber.services.length > 0
|
|
215
|
+
? await serviceDiscovery.register(subscriber.services)
|
|
216
|
+
: {};
|
|
217
|
+
|
|
218
|
+
// Subscribe to events
|
|
219
|
+
const subscribedEvents = subscriber.subscribedEvents || [];
|
|
220
|
+
|
|
221
|
+
if (subscribedEvents.length === 0) {
|
|
222
|
+
logger.warn({ subscriber: subscriber.constructor.name }, 'Subscriber has no subscribed events, skipping');
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
await eventSubscriber.subscribe(subscribedEvents, async (event) => {
|
|
227
|
+
try {
|
|
228
|
+
// Process single event (batch of 1)
|
|
229
|
+
await subscriber.handler({
|
|
230
|
+
events: [event],
|
|
231
|
+
services: services as any,
|
|
232
|
+
logger: subscriber.logger,
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
logger.debug({ eventType: event.type }, 'Successfully processed event');
|
|
236
|
+
} catch (error) {
|
|
237
|
+
logger.error({ error, event }, 'Failed to process event');
|
|
238
|
+
// Event will become visible again for retry
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
activeSubscribers.push(eventSubscriber);
|
|
243
|
+
|
|
244
|
+
logger.info(
|
|
245
|
+
{
|
|
246
|
+
events: subscribedEvents,
|
|
247
|
+
},
|
|
248
|
+
'Subscriber started polling'
|
|
249
|
+
);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
logger.error({ error, subscriber: subscriber.constructor.name }, 'Failed to setup subscriber');
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Setup graceful shutdown
|
|
256
|
+
const shutdown = () => {
|
|
257
|
+
logger.info('Stopping all subscribers');
|
|
258
|
+
for (const eventSubscriber of activeSubscribers) {
|
|
259
|
+
connection.stop();
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
process.on('SIGTERM', shutdown);
|
|
264
|
+
process.on('SIGINT', shutdown);
|
|
265
|
+
}
|
|
266
|
+
`;
|
|
267
|
+
|
|
268
|
+
await writeFile(subscribersPath, content);
|
|
269
|
+
return subscribersPath;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import { mkdir, readFile } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import type { Cron, ScheduleExpression } from '@geekmidas/constructs/crons';
|
|
4
|
+
import { itWithDir } from '@geekmidas/testkit/os';
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
6
|
+
import {
|
|
7
|
+
cleanupDir,
|
|
8
|
+
createMockBuildContext,
|
|
9
|
+
createMockCronFile,
|
|
10
|
+
createTempDir,
|
|
11
|
+
createTestCron,
|
|
12
|
+
} from '../../__tests__/test-helpers';
|
|
13
|
+
import { CronGenerator } from '../CronGenerator';
|
|
14
|
+
import type { GeneratedConstruct } from '../Generator';
|
|
15
|
+
|
|
16
|
+
describe('CronGenerator', () => {
|
|
17
|
+
let tempDir: string;
|
|
18
|
+
let outputDir: string;
|
|
19
|
+
let generator: CronGenerator;
|
|
20
|
+
let context: ReturnType<typeof createMockBuildContext>;
|
|
21
|
+
|
|
22
|
+
beforeEach(async () => {
|
|
23
|
+
tempDir = await createTempDir();
|
|
24
|
+
outputDir = join(tempDir, 'output');
|
|
25
|
+
generator = new CronGenerator();
|
|
26
|
+
context = createMockBuildContext();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterEach(async () => {
|
|
30
|
+
await cleanupDir(tempDir);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('isConstruct', () => {
|
|
34
|
+
it('should identify valid crons', () => {
|
|
35
|
+
const testCron = createTestCron('rate(1 hour)', 30);
|
|
36
|
+
expect(generator.isConstruct(testCron)).toBe(true);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should reject invalid constructs', () => {
|
|
40
|
+
expect(generator.isConstruct({})).toBe(false);
|
|
41
|
+
expect(generator.isConstruct('string')).toBe(false);
|
|
42
|
+
expect(generator.isConstruct(null)).toBe(false);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('build', () => {
|
|
47
|
+
const createMockCronConstruct = (
|
|
48
|
+
key: string,
|
|
49
|
+
schedule: ScheduleExpression = 'rate(1 hour)',
|
|
50
|
+
timeout: number = 30,
|
|
51
|
+
): GeneratedConstruct<Cron<any, any, any, any>> => ({
|
|
52
|
+
key,
|
|
53
|
+
name: key.toLowerCase(),
|
|
54
|
+
construct: createTestCron(schedule, timeout),
|
|
55
|
+
path: {
|
|
56
|
+
absolute: join(tempDir, `${key}.ts`),
|
|
57
|
+
relative: `${key}.ts`,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('aws-lambda provider', () => {
|
|
62
|
+
itWithDir('should generate cron handlers', async ({ dir }) => {
|
|
63
|
+
const outputDir = join(dir, 'output');
|
|
64
|
+
const cronsDir = join(dir, 'crons');
|
|
65
|
+
await mkdir(outputDir, { recursive: true });
|
|
66
|
+
|
|
67
|
+
await Promise.all([
|
|
68
|
+
createMockCronFile(
|
|
69
|
+
cronsDir,
|
|
70
|
+
'dailyReport.ts',
|
|
71
|
+
'dailyReport',
|
|
72
|
+
'rate(1 day)',
|
|
73
|
+
),
|
|
74
|
+
createMockCronFile(
|
|
75
|
+
cronsDir,
|
|
76
|
+
'hourlyCleanup.ts',
|
|
77
|
+
'hourlyCleanup',
|
|
78
|
+
'rate(1 hour)',
|
|
79
|
+
),
|
|
80
|
+
createMockCronFile(
|
|
81
|
+
cronsDir,
|
|
82
|
+
'weeklyBackup.ts',
|
|
83
|
+
'weeklyBackup',
|
|
84
|
+
'cron(0 0 ? * SUN *)',
|
|
85
|
+
),
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
89
|
+
|
|
90
|
+
const cronInfos = await generator.build(
|
|
91
|
+
context,
|
|
92
|
+
constructs,
|
|
93
|
+
outputDir,
|
|
94
|
+
{ provider: 'aws-lambda' },
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
expect(cronInfos).toHaveLength(3);
|
|
98
|
+
|
|
99
|
+
// Find crons by name since order may vary
|
|
100
|
+
const dailyReport = cronInfos.find((c) => c.name === 'dailyReport');
|
|
101
|
+
const hourlyCleanup = cronInfos.find((c) => c.name === 'hourlyCleanup');
|
|
102
|
+
const weeklyBackup = cronInfos.find((c) => c.name === 'weeklyBackup');
|
|
103
|
+
|
|
104
|
+
expect(dailyReport).toMatchObject({
|
|
105
|
+
name: 'dailyReport',
|
|
106
|
+
handler: expect.stringContaining('crons/dailyReport.handler'),
|
|
107
|
+
schedule: 'rate(1 day)',
|
|
108
|
+
});
|
|
109
|
+
expect(hourlyCleanup).toMatchObject({
|
|
110
|
+
name: 'hourlyCleanup',
|
|
111
|
+
handler: expect.stringContaining('crons/hourlyCleanup.handler'),
|
|
112
|
+
schedule: 'rate(1 hour)',
|
|
113
|
+
});
|
|
114
|
+
expect(weeklyBackup).toMatchObject({
|
|
115
|
+
name: 'weeklyBackup',
|
|
116
|
+
handler: expect.stringContaining('crons/weeklyBackup.handler'),
|
|
117
|
+
schedule: 'cron(0 0 ? * SUN *)',
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Check that handler files were created
|
|
121
|
+
const dailyReportHandlerPath = join(
|
|
122
|
+
outputDir,
|
|
123
|
+
'crons',
|
|
124
|
+
'dailyReport.ts',
|
|
125
|
+
);
|
|
126
|
+
const dailyReportContent = await readFile(
|
|
127
|
+
dailyReportHandlerPath,
|
|
128
|
+
'utf-8',
|
|
129
|
+
);
|
|
130
|
+
expect(dailyReportContent).toContain('AWSScheduledFunction');
|
|
131
|
+
expect(dailyReportContent).toContain('import { dailyReport }');
|
|
132
|
+
expect(dailyReportContent).toContain('import envParser');
|
|
133
|
+
expect(dailyReportContent).toContain('import logger');
|
|
134
|
+
|
|
135
|
+
const hourlyCleanupHandlerPath = join(
|
|
136
|
+
outputDir,
|
|
137
|
+
'crons',
|
|
138
|
+
'hourlyCleanup.ts',
|
|
139
|
+
);
|
|
140
|
+
const hourlyCleanupContent = await readFile(
|
|
141
|
+
hourlyCleanupHandlerPath,
|
|
142
|
+
'utf-8',
|
|
143
|
+
);
|
|
144
|
+
expect(hourlyCleanupContent).toContain('AWSScheduledFunction');
|
|
145
|
+
expect(hourlyCleanupContent).toContain('import { hourlyCleanup }');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should use default schedule when none provided', async () => {
|
|
149
|
+
// Create a cron with empty schedule that will use default
|
|
150
|
+
const cronWithDefaultSchedule = createTestCron(undefined, 30);
|
|
151
|
+
|
|
152
|
+
const construct: GeneratedConstruct<Cron<any, any, any, any>> = {
|
|
153
|
+
key: 'defaultScheduleCron',
|
|
154
|
+
name: 'default-schedule-cron',
|
|
155
|
+
construct: cronWithDefaultSchedule,
|
|
156
|
+
path: {
|
|
157
|
+
absolute: join(tempDir, 'defaultScheduleCron.ts'),
|
|
158
|
+
relative: 'defaultScheduleCron.ts',
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const cronInfos = await generator.build(
|
|
163
|
+
context,
|
|
164
|
+
[construct],
|
|
165
|
+
outputDir,
|
|
166
|
+
{ provider: 'aws-lambda' },
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
// Since the construct has a schedule, it should use that or default logic
|
|
170
|
+
expect(cronInfos[0].schedule).toBeDefined();
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
itWithDir(
|
|
174
|
+
'should generate correct relative import paths',
|
|
175
|
+
async ({ dir }) => {
|
|
176
|
+
const outputDir = join(dir, 'output');
|
|
177
|
+
const nestedDir = join(dir, 'src', 'crons', 'deep');
|
|
178
|
+
await mkdir(outputDir, { recursive: true });
|
|
179
|
+
|
|
180
|
+
await createMockCronFile(
|
|
181
|
+
nestedDir,
|
|
182
|
+
'deepCron.ts',
|
|
183
|
+
'deepCron',
|
|
184
|
+
'rate(1 day)',
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
const constructs = await generator.load(
|
|
188
|
+
'**/src/crons/deep/*.ts',
|
|
189
|
+
dir,
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
await generator.build(context, constructs, outputDir, {
|
|
193
|
+
provider: 'aws-lambda',
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const handlerPath = join(outputDir, 'crons', 'deepCron.ts');
|
|
197
|
+
const handlerContent = await readFile(handlerPath, 'utf-8');
|
|
198
|
+
|
|
199
|
+
// Check relative imports are correct - the path will be relative from outputDir
|
|
200
|
+
expect(handlerContent).toMatch(
|
|
201
|
+
/from ['"]+.*src\/crons\/deep\/deepCron\.js['"]+/,
|
|
202
|
+
);
|
|
203
|
+
expect(handlerContent).toMatch(/from ['"]+.*\/env['"]+/);
|
|
204
|
+
expect(handlerContent).toMatch(/from ['"]+.*\/logger['"]+/);
|
|
205
|
+
},
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
itWithDir('should log generation progress', async ({ dir }) => {
|
|
209
|
+
const logSpy = vi.spyOn(console, 'log');
|
|
210
|
+
const outputDir = join(dir, 'output');
|
|
211
|
+
const cronsDir = join(dir, 'crons');
|
|
212
|
+
await mkdir(outputDir, { recursive: true });
|
|
213
|
+
|
|
214
|
+
await createMockCronFile(
|
|
215
|
+
cronsDir,
|
|
216
|
+
'testCron.ts',
|
|
217
|
+
'testCron',
|
|
218
|
+
'rate(1 hour)',
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
222
|
+
|
|
223
|
+
await generator.build(context, constructs, outputDir, {
|
|
224
|
+
provider: 'aws-lambda',
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
expect(logSpy).toHaveBeenCalledWith('Generated cron handler: testCron');
|
|
228
|
+
|
|
229
|
+
logSpy.mockRestore();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe('non aws-lambda provider', () => {
|
|
234
|
+
itWithDir(
|
|
235
|
+
'should return empty array for server provider',
|
|
236
|
+
async ({ dir }) => {
|
|
237
|
+
const outputDir = join(dir, 'output');
|
|
238
|
+
const cronsDir = join(dir, 'crons');
|
|
239
|
+
await mkdir(outputDir, { recursive: true });
|
|
240
|
+
|
|
241
|
+
await createMockCronFile(
|
|
242
|
+
cronsDir,
|
|
243
|
+
'testCron.ts',
|
|
244
|
+
'testCron',
|
|
245
|
+
'rate(1 hour)',
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
249
|
+
|
|
250
|
+
const cronInfos = await generator.build(
|
|
251
|
+
context,
|
|
252
|
+
constructs,
|
|
253
|
+
outputDir,
|
|
254
|
+
{ provider: 'server' },
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
expect(cronInfos).toEqual([]);
|
|
258
|
+
},
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
itWithDir(
|
|
262
|
+
'should return empty array for aws-apigatewayv1 provider',
|
|
263
|
+
async ({ dir }) => {
|
|
264
|
+
const outputDir = join(dir, 'output');
|
|
265
|
+
const cronsDir = join(dir, 'crons');
|
|
266
|
+
await mkdir(outputDir, { recursive: true });
|
|
267
|
+
|
|
268
|
+
await createMockCronFile(
|
|
269
|
+
cronsDir,
|
|
270
|
+
'testCron.ts',
|
|
271
|
+
'testCron',
|
|
272
|
+
'rate(1 hour)',
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
276
|
+
|
|
277
|
+
const cronInfos = await generator.build(
|
|
278
|
+
context,
|
|
279
|
+
constructs,
|
|
280
|
+
outputDir,
|
|
281
|
+
{ provider: 'aws-apigatewayv1' },
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
expect(cronInfos).toEqual([]);
|
|
285
|
+
},
|
|
286
|
+
);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('should return empty array for empty constructs', async () => {
|
|
290
|
+
const cronInfos = await generator.build(context, [], outputDir, {
|
|
291
|
+
provider: 'aws-lambda',
|
|
292
|
+
});
|
|
293
|
+
expect(cronInfos).toEqual([]);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
itWithDir(
|
|
297
|
+
'should use default provider when none specified',
|
|
298
|
+
async ({ dir }) => {
|
|
299
|
+
const outputDir = join(dir, 'output');
|
|
300
|
+
const cronsDir = join(dir, 'crons');
|
|
301
|
+
await mkdir(outputDir, { recursive: true });
|
|
302
|
+
|
|
303
|
+
await createMockCronFile(
|
|
304
|
+
cronsDir,
|
|
305
|
+
'defaultCron.ts',
|
|
306
|
+
'defaultCron',
|
|
307
|
+
'rate(1 hour)',
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
311
|
+
|
|
312
|
+
const cronInfos = await generator.build(context, constructs, outputDir);
|
|
313
|
+
|
|
314
|
+
expect(cronInfos).toHaveLength(1);
|
|
315
|
+
expect(cronInfos[0].name).toBe('defaultCron');
|
|
316
|
+
|
|
317
|
+
// Check that handler was created (default is aws-lambda)
|
|
318
|
+
const handlerPath = join(outputDir, 'crons', 'defaultCron.ts');
|
|
319
|
+
const handlerContent = await readFile(handlerPath, 'utf-8');
|
|
320
|
+
expect(handlerContent).toContain('AWSScheduledFunction');
|
|
321
|
+
},
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
itWithDir('should handle various schedule expressions', async ({ dir }) => {
|
|
325
|
+
const outputDir = join(dir, 'output');
|
|
326
|
+
const cronsDir = join(dir, 'crons');
|
|
327
|
+
await mkdir(outputDir, { recursive: true });
|
|
328
|
+
|
|
329
|
+
await Promise.all([
|
|
330
|
+
createMockCronFile(
|
|
331
|
+
cronsDir,
|
|
332
|
+
'rateCron.ts',
|
|
333
|
+
'rateCron',
|
|
334
|
+
'rate(5 minutes)',
|
|
335
|
+
),
|
|
336
|
+
createMockCronFile(
|
|
337
|
+
cronsDir,
|
|
338
|
+
'cronExpression.ts',
|
|
339
|
+
'cronExpression',
|
|
340
|
+
'cron(0 12 * * ? *)',
|
|
341
|
+
),
|
|
342
|
+
createMockCronFile(
|
|
343
|
+
cronsDir,
|
|
344
|
+
'dailyRate.ts',
|
|
345
|
+
'dailyRate',
|
|
346
|
+
'rate(1 day)',
|
|
347
|
+
),
|
|
348
|
+
]);
|
|
349
|
+
|
|
350
|
+
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
351
|
+
|
|
352
|
+
const cronInfos = await generator.build(context, constructs, outputDir, {
|
|
353
|
+
provider: 'aws-lambda',
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// Find crons by name since order may vary
|
|
357
|
+
const rateCron = cronInfos.find((c) => c.name === 'rateCron');
|
|
358
|
+
const cronExpression = cronInfos.find((c) => c.name === 'cronExpression');
|
|
359
|
+
const dailyRate = cronInfos.find((c) => c.name === 'dailyRate');
|
|
360
|
+
|
|
361
|
+
expect(rateCron?.schedule).toBe('rate(5 minutes)');
|
|
362
|
+
expect(cronExpression?.schedule).toBe('cron(0 12 * * ? *)');
|
|
363
|
+
expect(dailyRate?.schedule).toBe('rate(1 day)');
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
itWithDir(
|
|
367
|
+
'should handle crons with different timeout values',
|
|
368
|
+
async ({ dir }) => {
|
|
369
|
+
const outputDir = join(dir, 'output');
|
|
370
|
+
const cronsDir = join(dir, 'crons');
|
|
371
|
+
await mkdir(outputDir, { recursive: true });
|
|
372
|
+
|
|
373
|
+
await Promise.all([
|
|
374
|
+
createMockCronFile(
|
|
375
|
+
cronsDir,
|
|
376
|
+
'quickCron.ts',
|
|
377
|
+
'quickCron',
|
|
378
|
+
'rate(1 minute)',
|
|
379
|
+
),
|
|
380
|
+
createMockCronFile(
|
|
381
|
+
cronsDir,
|
|
382
|
+
'slowCron.ts',
|
|
383
|
+
'slowCron',
|
|
384
|
+
'rate(1 hour)',
|
|
385
|
+
),
|
|
386
|
+
]);
|
|
387
|
+
|
|
388
|
+
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
389
|
+
|
|
390
|
+
const cronInfos = await generator.build(
|
|
391
|
+
context,
|
|
392
|
+
constructs,
|
|
393
|
+
outputDir,
|
|
394
|
+
{
|
|
395
|
+
provider: 'aws-lambda',
|
|
396
|
+
},
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
// Find crons by name since order may vary
|
|
400
|
+
const quickCron = cronInfos.find((c) => c.name === 'quickCron');
|
|
401
|
+
const slowCron = cronInfos.find((c) => c.name === 'slowCron');
|
|
402
|
+
|
|
403
|
+
// Note: timeout comes from the cron construct, not the file creation
|
|
404
|
+
expect(quickCron).toBeDefined();
|
|
405
|
+
expect(slowCron).toBeDefined();
|
|
406
|
+
},
|
|
407
|
+
);
|
|
408
|
+
|
|
409
|
+
itWithDir(
|
|
410
|
+
'should handle crons with custom environment parser patterns',
|
|
411
|
+
async ({ dir }) => {
|
|
412
|
+
const outputDir = join(dir, 'output');
|
|
413
|
+
const cronsDir = join(dir, 'crons');
|
|
414
|
+
await mkdir(outputDir, { recursive: true });
|
|
415
|
+
|
|
416
|
+
const customContext = {
|
|
417
|
+
...context,
|
|
418
|
+
envParserImportPattern: '{ customParser as envParser }',
|
|
419
|
+
loggerImportPattern: '{ customLogger as logger }',
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
await createMockCronFile(
|
|
423
|
+
cronsDir,
|
|
424
|
+
'customCron.ts',
|
|
425
|
+
'customCron',
|
|
426
|
+
'rate(1 hour)',
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
const constructs = await generator.load('**/crons/*.ts', dir);
|
|
430
|
+
|
|
431
|
+
await generator.build(customContext, constructs, outputDir, {
|
|
432
|
+
provider: 'aws-lambda',
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
const handlerPath = join(outputDir, 'crons', 'customCron.ts');
|
|
436
|
+
const handlerContent = await readFile(handlerPath, 'utf-8');
|
|
437
|
+
|
|
438
|
+
expect(handlerContent).toContain(
|
|
439
|
+
'import { customParser as envParser }',
|
|
440
|
+
);
|
|
441
|
+
expect(handlerContent).toContain('import { customLogger as logger }');
|
|
442
|
+
},
|
|
443
|
+
);
|
|
444
|
+
});
|
|
445
|
+
});
|