@lunora/codegen 0.0.0 → 1.0.0-alpha.10

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 (35) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +117 -9
  3. package/__assets__/package-og.svg +14 -0
  4. package/dist/index.d.mts +1562 -0
  5. package/dist/index.d.ts +1562 -0
  6. package/dist/index.mjs +29 -0
  7. package/dist/packem_shared/CONTAINERS_FILENAME-DlP6YM_Q.mjs +224 -0
  8. package/dist/packem_shared/CodegenDiagnosticError-DeblMkzO.mjs +23 -0
  9. package/dist/packem_shared/GENERATED_HEADER-BiFXNUvo.mjs +2692 -0
  10. package/dist/packem_shared/LUNORA_ERROR_CODES-CySpQPD3.mjs +61 -0
  11. package/dist/packem_shared/OPENRPC_VERSION-B4i9Qp3v.mjs +60 -0
  12. package/dist/packem_shared/SCHEMA_SNAPSHOT_FILENAME-Bvv7qa_u.mjs +939 -0
  13. package/dist/packem_shared/SCHEMA_SNAPSHOT_VERSION-wWGP2_5E.mjs +245 -0
  14. package/dist/packem_shared/WORKFLOWS_FILENAME-D62dcBGg.mjs +84 -0
  15. package/dist/packem_shared/buildOpenApiDocument-Cu3mZl-8.mjs +183 -0
  16. package/dist/packem_shared/discover-ast-CT6BgBr4.mjs +13 -0
  17. package/dist/packem_shared/discoverAuthApiCalls-CoirYbg6.mjs +62 -0
  18. package/dist/packem_shared/discoverCrons-Cev7RRAf.mjs +257 -0
  19. package/dist/packem_shared/discoverFunctions-BWMczzBx.mjs +465 -0
  20. package/dist/packem_shared/discoverHttpRoutes-CfP6cMzt.mjs +131 -0
  21. package/dist/packem_shared/discoverInserts-C7zxXkUf.mjs +61 -0
  22. package/dist/packem_shared/discoverMaskProcedures-Cm81kwrb.mjs +217 -0
  23. package/dist/packem_shared/discoverMigrations-Bi5nJ0mJ.mjs +97 -0
  24. package/dist/packem_shared/discoverNondeterministicCalls-C4M8AXmQ.mjs +122 -0
  25. package/dist/packem_shared/discoverQueries-B0wGT-xe.mjs +62 -0
  26. package/dist/packem_shared/discoverR2sqlCalls-BVNMd428.mjs +80 -0
  27. package/dist/packem_shared/discoverRlsMetadata-BS9GOGC5.mjs +280 -0
  28. package/dist/packem_shared/discoverSchema-BnWHHJ4T.mjs +822 -0
  29. package/dist/packem_shared/discoverStorageRulesMetadata-Da8BKXcI.mjs +97 -0
  30. package/dist/packem_shared/emitApp-KfMaKXWe.mjs +603 -0
  31. package/dist/packem_shared/formatAdvisories-8NIv1k0I.mjs +69 -0
  32. package/dist/packem_shared/parse-validator-Cabb60UV.mjs +133 -0
  33. package/dist/packem_shared/paths-BRd6JHuF.mjs +11 -0
  34. package/dist/packem_shared/schemaFromIr-DTYsLBaA.mjs +57 -0
  35. package/package.json +45 -17
@@ -0,0 +1,257 @@
1
+ import { isValidCronExpression, compileCronSchedule, CRON_SCHEDULE_KINDS } from '@lunora/scheduler';
2
+ import { SyntaxKind, Node } from 'ts-morph';
3
+ import { diagnosticAt } from './CodegenDiagnosticError-DeblMkzO.mjs';
4
+ import { listLunoraSourceFiles } from './discoverFunctions-BWMczzBx.mjs';
5
+ import { s as sanitizeNamespace } from './paths-BRd6JHuF.mjs';
6
+
7
+ const CRON_METHODS = /* @__PURE__ */ new Set([...CRON_SCHEDULE_KINDS, "cron"]);
8
+ const CRON_JOBS_SOURCES = /* @__PURE__ */ new Set(["@lunora/scheduler", "@lunora/server"]);
9
+ const isCronJobsFactory = (identifier) => {
10
+ const symbol = identifier.getSymbol();
11
+ if (!symbol) {
12
+ return identifier.getText() === "cronJobs";
13
+ }
14
+ for (const declaration of symbol.getDeclarations()) {
15
+ if (!Node.isImportSpecifier(declaration)) {
16
+ continue;
17
+ }
18
+ if (!CRON_JOBS_SOURCES.has(declaration.getImportDeclaration().getModuleSpecifierValue())) {
19
+ return false;
20
+ }
21
+ return declaration.getNameNode().getText() === "cronJobs";
22
+ }
23
+ return false;
24
+ };
25
+ const collectCronBuilderNames = (source) => {
26
+ const names = /* @__PURE__ */ new Set();
27
+ for (const declaration of source.getVariableDeclarations()) {
28
+ const initializer = declaration.getInitializer();
29
+ if (initializer?.getKind() !== SyntaxKind.CallExpression) {
30
+ continue;
31
+ }
32
+ const callee = initializer.getExpression();
33
+ if (Node.isIdentifier(callee) && isCronJobsFactory(callee)) {
34
+ names.add(declaration.getName());
35
+ }
36
+ }
37
+ return names;
38
+ };
39
+ const rootIdentifierOf = (node) => {
40
+ let current = node;
41
+ while (Node.isCallExpression(current) || Node.isPropertyAccessExpression(current)) {
42
+ current = current.getExpression();
43
+ }
44
+ return Node.isIdentifier(current) ? current.getText() : void 0;
45
+ };
46
+ const stringArgument = (call, index) => {
47
+ const argument = call.getArguments()[index];
48
+ return argument && Node.isStringLiteral(argument) ? argument.getLiteralValue() : void 0;
49
+ };
50
+ const literalValue = (node, jobName) => {
51
+ if (Node.isStringLiteral(node) || Node.isNoSubstitutionTemplateLiteral(node)) {
52
+ return node.getLiteralValue();
53
+ }
54
+ if (Node.isNumericLiteral(node)) {
55
+ return node.getLiteralValue();
56
+ }
57
+ if (Node.isTrueLiteral(node)) {
58
+ return true;
59
+ }
60
+ if (Node.isFalseLiteral(node)) {
61
+ return false;
62
+ }
63
+ if (Node.isPrefixUnaryExpression(node) && node.getOperatorToken() === SyntaxKind.MinusToken) {
64
+ const operand = node.getOperand();
65
+ if (Node.isNumericLiteral(operand)) {
66
+ return -operand.getLiteralValue();
67
+ }
68
+ }
69
+ if (Node.isObjectLiteralExpression(node)) {
70
+ return objectLiteralValue(node, jobName);
71
+ }
72
+ if (Node.isArrayLiteralExpression(node)) {
73
+ return node.getElements().map((element) => literalValue(element, jobName));
74
+ }
75
+ throw diagnosticAt(node, `Cron job "${jobName}" passes a non-static value where a literal is required; codegen can only read literals.`, {
76
+ code: "CRON_NON_STATIC_VALUE",
77
+ name: "LunoraError",
78
+ status: 500
79
+ });
80
+ };
81
+ const objectLiteralValue = (object, jobName) => {
82
+ const result = {};
83
+ for (const property of object.getProperties()) {
84
+ if (!Node.isPropertyAssignment(property)) {
85
+ throw diagnosticAt(
86
+ property,
87
+ `Cron job "${jobName}" uses an unsupported object property (shorthand/spread/method) where a static literal is required.`,
88
+ {
89
+ code: "CRON_NON_STATIC_VALUE",
90
+ name: "LunoraError",
91
+ status: 500
92
+ }
93
+ );
94
+ }
95
+ const initializer = property.getInitializer();
96
+ if (initializer) {
97
+ result[property.getName()] = literalValue(initializer, jobName);
98
+ }
99
+ }
100
+ return result;
101
+ };
102
+ const functionPathFromArgument = (call, index, jobName) => {
103
+ const argument = call.getArguments()[index];
104
+ if (!argument || !Node.isPropertyAccessExpression(argument)) {
105
+ throw diagnosticAt(
106
+ call,
107
+ `Cron job "${jobName}" must reference a function statically (e.g. internal.email.digest); codegen cannot resolve a dynamic reference.`,
108
+ {
109
+ code: "CRON_NON_STATIC_FN",
110
+ name: "LunoraError",
111
+ status: 500
112
+ }
113
+ );
114
+ }
115
+ const functionName = argument.getName();
116
+ const receiver = argument.getExpression();
117
+ if (!Node.isPropertyAccessExpression(receiver)) {
118
+ throw diagnosticAt(argument, `Cron job "${jobName}" function reference must be of the form internal.file.fn (two property accesses).`, {
119
+ code: "CRON_NON_STATIC_FN",
120
+ name: "LunoraError",
121
+ status: 500
122
+ });
123
+ }
124
+ const namespace = receiver.getName();
125
+ return `${sanitizeNamespace(namespace)}:${functionName}`;
126
+ };
127
+ const workflowTarget = (workflow) => {
128
+ return { workflow: { binding: workflow.bindingName, exportName: workflow.exportName } };
129
+ };
130
+ const resolveTarget = (call, index, jobName, workflowsByName) => {
131
+ const argument = call.getArguments()[index];
132
+ if (argument && Node.isPropertyAccessExpression(argument)) {
133
+ const receiver = argument.getExpression();
134
+ if (Node.isIdentifier(receiver) && receiver.getText() === "workflows") {
135
+ const workflow = workflowsByName.get(argument.getName());
136
+ if (workflow) {
137
+ return workflowTarget(workflow);
138
+ }
139
+ throw diagnosticAt(
140
+ argument,
141
+ `Cron job "${jobName}" targets workflows.${argument.getName()}, but no such workflow is declared in lunora/workflows.ts.`,
142
+ {
143
+ code: "CRON_NON_STATIC_FN",
144
+ name: "LunoraError",
145
+ status: 500
146
+ }
147
+ );
148
+ }
149
+ return { functionPath: functionPathFromArgument(call, index, jobName) };
150
+ }
151
+ if (argument && Node.isIdentifier(argument)) {
152
+ const workflow = workflowsByName.get(argument.getText());
153
+ if (workflow) {
154
+ return workflowTarget(workflow);
155
+ }
156
+ throw diagnosticAt(
157
+ argument,
158
+ `Cron job "${jobName}" references "${argument.getText()}", which is neither a function (internal.file.fn / api.file.fn) nor a declared workflow in lunora/workflows.ts.`,
159
+ {
160
+ code: "CRON_NON_STATIC_FN",
161
+ name: "LunoraError",
162
+ status: 500
163
+ }
164
+ );
165
+ }
166
+ return { functionPath: functionPathFromArgument(call, index, jobName) };
167
+ };
168
+ const cronFromCall = (call, callee, builderNames, workflowsByName) => {
169
+ const method = callee.getName();
170
+ if (!CRON_METHODS.has(method)) {
171
+ return void 0;
172
+ }
173
+ if (!builderNames.has(rootIdentifierOf(callee.getExpression()) ?? "")) {
174
+ return void 0;
175
+ }
176
+ const name = stringArgument(call, 0);
177
+ if (name === void 0 || name.trim() === "") {
178
+ throw diagnosticAt(call, `A cron ".${method}(...)" registration must pass a non-empty string-literal name as its first argument.`, {
179
+ code: "CRON_NAME_NOT_STATIC",
180
+ name: "LunoraError",
181
+ status: 500
182
+ });
183
+ }
184
+ let cron;
185
+ if (method === "cron") {
186
+ const expression = stringArgument(call, 1);
187
+ if (expression === void 0) {
188
+ throw diagnosticAt(call, `Cron job "${name}" must pass a string-literal cron expression to ".cron(...)".`, {
189
+ code: "CRON_EXPR_NOT_STATIC",
190
+ name: "LunoraError",
191
+ status: 500
192
+ });
193
+ }
194
+ if (!isValidCronExpression(expression)) {
195
+ throw diagnosticAt(call, `Cron job "${name}" has an invalid cron expression "${expression}" — expected 5 or 6 space-separated fields.`, {
196
+ code: "CRON_EXPR_INVALID",
197
+ name: "LunoraError",
198
+ status: 500
199
+ });
200
+ }
201
+ cron = expression;
202
+ } else {
203
+ const scheduleArgument = call.getArguments()[1];
204
+ if (!scheduleArgument || !Node.isObjectLiteralExpression(scheduleArgument)) {
205
+ throw diagnosticAt(call, `Cron job "${name}" must pass an object-literal schedule to ".${method}(...)".`, {
206
+ code: "CRON_SCHEDULE_NOT_STATIC",
207
+ name: "LunoraError",
208
+ status: 500
209
+ });
210
+ }
211
+ cron = compileCronSchedule(method, objectLiteralValue(scheduleArgument, name));
212
+ }
213
+ const target = resolveTarget(call, 2, name, workflowsByName);
214
+ const argumentsNode = call.getArguments()[3];
215
+ const args = argumentsNode && Node.isObjectLiteralExpression(argumentsNode) ? objectLiteralValue(argumentsNode, name) : {};
216
+ return { args, cron, name, ...target };
217
+ };
218
+ const assertUniqueNames = (crons) => {
219
+ const seen = /* @__PURE__ */ new Set();
220
+ for (const cron of crons) {
221
+ if (seen.has(cron.name)) {
222
+ throw Object.assign(new Error(`Duplicate cron job name "${cron.name}": cron names must be unique across the project.`), {
223
+ code: "DUPLICATE_CRON_NAME",
224
+ name: "LunoraError",
225
+ status: 500
226
+ });
227
+ }
228
+ seen.add(cron.name);
229
+ }
230
+ };
231
+ const discoverCrons = (project, lunoraDirectory, workflows = []) => {
232
+ const filePaths = listLunoraSourceFiles(lunoraDirectory);
233
+ const crons = [];
234
+ const workflowsByName = new Map(workflows.map((workflow) => [workflow.exportName, workflow]));
235
+ for (const filePath of filePaths) {
236
+ const source = project.getSourceFile(filePath) ?? project.addSourceFileAtPath(filePath);
237
+ const builderNames = collectCronBuilderNames(source);
238
+ if (builderNames.size === 0) {
239
+ continue;
240
+ }
241
+ for (const call of source.getDescendantsOfKind(SyntaxKind.CallExpression)) {
242
+ const callee = call.getExpression();
243
+ if (!Node.isPropertyAccessExpression(callee)) {
244
+ continue;
245
+ }
246
+ const cron = cronFromCall(call, callee, builderNames, workflowsByName);
247
+ if (cron) {
248
+ crons.push(cron);
249
+ }
250
+ }
251
+ }
252
+ crons.sort((a, b) => a.name.localeCompare(b.name));
253
+ assertUniqueNames(crons);
254
+ return crons;
255
+ };
256
+
257
+ export { discoverCrons as default };