@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.
- package/FUNCTION_CRON_SUPPORT.md +266 -0
- package/README.md +21 -4
- package/dist/CronGenerator-ClbRcmz_.mjs +53 -0
- package/dist/CronGenerator-ClbRcmz_.mjs.map +1 -0
- package/dist/CronGenerator-Ctl4USy4.cjs +59 -0
- package/dist/CronGenerator-Ctl4USy4.cjs.map +1 -0
- package/dist/EndpointGenerator-Dj7AumHi.cjs +164 -0
- package/dist/EndpointGenerator-Dj7AumHi.cjs.map +1 -0
- package/dist/EndpointGenerator-uBA1ixUw.mjs +158 -0
- package/dist/EndpointGenerator-uBA1ixUw.mjs.map +1 -0
- package/dist/FunctionGenerator-DN681IUn.cjs +58 -0
- package/dist/FunctionGenerator-DN681IUn.cjs.map +1 -0
- package/dist/FunctionGenerator-crAa-JC7.mjs +52 -0
- package/dist/FunctionGenerator-crAa-JC7.mjs.map +1 -0
- package/dist/Generator-C3tYSTQY.cjs +47 -0
- package/dist/Generator-C3tYSTQY.cjs.map +1 -0
- package/dist/Generator-CDt4pB3W.mjs +41 -0
- package/dist/Generator-CDt4pB3W.mjs.map +1 -0
- package/dist/__tests__/config.spec.cjs +98 -0
- package/dist/__tests__/config.spec.cjs.map +1 -0
- package/dist/__tests__/config.spec.mjs +97 -0
- package/dist/__tests__/config.spec.mjs.map +1 -0
- package/dist/__tests__/test-helpers.cjs +14 -0
- package/dist/__tests__/test-helpers.mjs +4 -0
- package/dist/build/__tests__/index-new.spec.cjs +286 -0
- package/dist/build/__tests__/index-new.spec.cjs.map +1 -0
- package/dist/build/__tests__/index-new.spec.mjs +285 -0
- package/dist/build/__tests__/index-new.spec.mjs.map +1 -0
- package/dist/build/index.cjs +11 -0
- package/dist/build/index.mjs +11 -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-BZdwxCLW.mjs +64 -0
- package/dist/build-BZdwxCLW.mjs.map +1 -0
- package/dist/build-BfQFnU5-.cjs +70 -0
- package/dist/build-BfQFnU5-.cjs.map +1 -0
- package/dist/{chunk-CUT6urMc.cjs → chunk-CsX-DzYB.cjs} +12 -0
- package/dist/config-CXxYmz_o.mjs +30 -0
- package/dist/config-CXxYmz_o.mjs.map +1 -0
- package/dist/config-RcNESK0T.cjs +36 -0
- package/dist/config-RcNESK0T.cjs.map +1 -0
- package/dist/config.cjs +1 -1
- package/dist/config.mjs +1 -1
- package/dist/esm-9eeZntth.mjs +3777 -0
- package/dist/esm-9eeZntth.mjs.map +1 -0
- package/dist/esm-Crmo4h9t.cjs +4392 -0
- package/dist/esm-Crmo4h9t.cjs.map +1 -0
- package/dist/esm-CsJbr7gi.mjs +3 -0
- package/dist/esm-w09tAC4l.cjs +8 -0
- 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/__tests__/CronGenerator.spec.cjs +216 -0
- package/dist/generators/__tests__/CronGenerator.spec.cjs.map +1 -0
- package/dist/generators/__tests__/CronGenerator.spec.mjs +215 -0
- package/dist/generators/__tests__/CronGenerator.spec.mjs.map +1 -0
- package/dist/generators/__tests__/EndpointGenerator.spec.cjs +182 -0
- package/dist/generators/__tests__/EndpointGenerator.spec.cjs.map +1 -0
- package/dist/generators/__tests__/EndpointGenerator.spec.mjs +181 -0
- package/dist/generators/__tests__/EndpointGenerator.spec.mjs.map +1 -0
- package/dist/generators/__tests__/FunctionGenerator.spec.cjs +152 -0
- package/dist/generators/__tests__/FunctionGenerator.spec.cjs.map +1 -0
- package/dist/generators/__tests__/FunctionGenerator.spec.mjs +151 -0
- package/dist/generators/__tests__/FunctionGenerator.spec.mjs.map +1 -0
- package/dist/generators/index.cjs +10 -0
- package/dist/generators/index.mjs +7 -0
- package/dist/generators-CsLujGXs.mjs +0 -0
- package/dist/generators-_pY7sHy1.cjs +0 -0
- package/dist/index.cjs +68 -26
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +67 -25
- package/dist/index.mjs.map +1 -0
- package/dist/manifests-BTtfDMX8.cjs +26 -0
- package/dist/manifests-BTtfDMX8.cjs.map +1 -0
- package/dist/manifests-HX4z4kkz.mjs +20 -0
- package/dist/manifests-HX4z4kkz.mjs.map +1 -0
- package/dist/{openapi-CksVdkh2.mjs → openapi-BivnatiC.mjs} +8 -6
- package/dist/openapi-BivnatiC.mjs.map +1 -0
- package/dist/{openapi-D4QQJUPY.cjs → openapi-DW-qF3oW.cjs} +9 -7
- package/dist/openapi-DW-qF3oW.cjs.map +1 -0
- package/dist/{openapi-react-query-C1JLYUOs.cjs → openapi-react-query-J0BzBHhN.cjs} +4 -3
- package/dist/openapi-react-query-J0BzBHhN.cjs.map +1 -0
- package/dist/{openapi-react-query-DpT3XHFC.mjs → openapi-react-query-lgS7AVEz.mjs} +3 -2
- package/dist/openapi-react-query-lgS7AVEz.mjs.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-Cs-0YCaP.cjs +114 -0
- package/dist/providerResolver-Cs-0YCaP.cjs.map +1 -0
- package/dist/test-helpers-ARd8GDgx.cjs +199 -0
- package/dist/test-helpers-ARd8GDgx.cjs.map +1 -0
- package/dist/test-helpers-DdVBk23F.mjs +133 -0
- package/dist/test-helpers-DdVBk23F.mjs.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/package.json +35 -14
- package/src/__tests__/config.spec.ts +110 -0
- package/src/__tests__/test-helpers.ts +178 -0
- package/src/build/__tests__/index-new.spec.ts +578 -0
- package/src/build/index.ts +136 -0
- package/src/build/manifests.ts +32 -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 +97 -0
- package/src/generators/EndpointGenerator.ts +290 -0
- package/src/generators/FunctionGenerator.ts +96 -0
- package/src/generators/Generator.ts +95 -0
- package/src/generators/__tests__/CronGenerator.spec.ts +445 -0
- package/src/generators/__tests__/EndpointGenerator.spec.ts +372 -0
- package/src/generators/__tests__/FunctionGenerator.spec.ts +257 -0
- package/src/generators/index.ts +8 -0
- package/src/index.ts +57 -22
- package/src/openapi.ts +4 -3
- package/src/types.ts +73 -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,96 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join, relative } from 'node:path';
|
|
3
|
+
import { Function } from '@geekmidas/api/constructs';
|
|
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
|
+
});
|
|
55
|
+
|
|
56
|
+
logger.log(`Generated function handler: ${key}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return functionInfos;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private async generateFunctionHandler(
|
|
63
|
+
outputDir: string,
|
|
64
|
+
sourceFile: string,
|
|
65
|
+
exportName: string,
|
|
66
|
+
context: BuildContext,
|
|
67
|
+
): Promise<string> {
|
|
68
|
+
const handlerFileName = `${exportName}.ts`;
|
|
69
|
+
const handlerPath = join(outputDir, handlerFileName);
|
|
70
|
+
|
|
71
|
+
const relativePath = relative(dirname(handlerPath), sourceFile);
|
|
72
|
+
const importPath = relativePath.replace(/\.ts$/, '.js');
|
|
73
|
+
|
|
74
|
+
const relativeEnvParserPath = relative(
|
|
75
|
+
dirname(handlerPath),
|
|
76
|
+
context.envParserPath,
|
|
77
|
+
);
|
|
78
|
+
const relativeLoggerPath = relative(
|
|
79
|
+
dirname(handlerPath),
|
|
80
|
+
context.loggerPath,
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const content = `import { AWSLambdaFunction } from '@geekmidas/api/aws-lambda';
|
|
84
|
+
import { ${exportName} } from '${importPath}';
|
|
85
|
+
import ${context.envParserImportPattern} from '${relativeEnvParserPath}';
|
|
86
|
+
import ${context.loggerImportPattern} from '${relativeLoggerPath}';
|
|
87
|
+
|
|
88
|
+
const adapter = new AWSLambdaFunction(envParser, ${exportName});
|
|
89
|
+
|
|
90
|
+
export const handler = adapter.handler;
|
|
91
|
+
`;
|
|
92
|
+
|
|
93
|
+
await writeFile(handlerPath, content);
|
|
94
|
+
return handlerPath;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { relative } from 'path';
|
|
2
|
+
import type { Construct } from '@geekmidas/api/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
|
+
}
|
|
@@ -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/api/constructs';
|
|
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
|
+
});
|