@cosmicdrift/kumiko-dev-server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/bin/kumiko-build.ts +85 -0
  2. package/bin/kumiko-dev.ts +90 -0
  3. package/package.json +45 -0
  4. package/src/__tests__/build-prod-bundle.integration.ts +265 -0
  5. package/src/__tests__/build-prod-bundle.test.ts +262 -0
  6. package/src/__tests__/cache-headers.test.ts +70 -0
  7. package/src/__tests__/classify-change.test.ts +87 -0
  8. package/src/__tests__/compose-features-wiring.integration.ts +352 -0
  9. package/src/__tests__/compose-features.test.ts +81 -0
  10. package/src/__tests__/crash-tracker.test.ts +89 -0
  11. package/src/__tests__/create-kumiko-server.integration.ts +286 -0
  12. package/src/__tests__/few-shot-corpus.test.ts +311 -0
  13. package/src/__tests__/inject-schema.test.ts +62 -0
  14. package/src/__tests__/resolve-stylesheet.test.ts +90 -0
  15. package/src/__tests__/resolve-tailwind-cli.test.ts +49 -0
  16. package/src/__tests__/run-prod-app-spec.test.ts +57 -0
  17. package/src/__tests__/run-prod-app.integration.ts +535 -0
  18. package/src/__tests__/scaffold-feature.test.ts +143 -0
  19. package/src/__tests__/try-hono-first.test.ts +63 -0
  20. package/src/build-prod-bundle.ts +587 -0
  21. package/src/build-server-bundle.ts +308 -0
  22. package/src/build.ts +28 -0
  23. package/src/codegen/__tests__/run-codegen.test.ts +494 -0
  24. package/src/codegen/__tests__/strict-mode-diagnostics.test.ts +467 -0
  25. package/src/codegen/__tests__/watch.test.ts +186 -0
  26. package/src/codegen/index.ts +17 -0
  27. package/src/codegen/render.ts +225 -0
  28. package/src/codegen/run-codegen.ts +157 -0
  29. package/src/codegen/scan-events.ts +574 -0
  30. package/src/codegen/watch.ts +127 -0
  31. package/src/compose-features.ts +128 -0
  32. package/src/crash-tracker.ts +56 -0
  33. package/src/create-kumiko-server.ts +1010 -0
  34. package/src/drizzle-config.ts +44 -0
  35. package/src/drizzle-tables-auth-mode.ts +32 -0
  36. package/src/drizzle-tables-minimal.ts +22 -0
  37. package/src/few-shot-corpus.ts +369 -0
  38. package/src/index.ts +57 -0
  39. package/src/inject-schema.ts +24 -0
  40. package/src/resolve-tailwind-cli.ts +28 -0
  41. package/src/run-dev-app.ts +290 -0
  42. package/src/run-prod-app.ts +892 -0
  43. package/src/scaffold-feature.ts +226 -0
  44. package/src/try-hono-first.ts +46 -0
@@ -0,0 +1,574 @@
1
+ // scanEvents — finds every `r.defineEvent(...)` call across the app's
2
+ // feature files and resolves both the event-name (string literal) AND
3
+ // the schema source needed to derive a TS-Type. The codegen pipeline
4
+ // turns these into a `KumikoEventTypeMap` augmentation, the "single
5
+ // source of truth" the local defineWriteHandler-wrapper binds against
6
+ // for cross-package strict-checking.
7
+ //
8
+ // Output is a flat list of "event entries" — qualified-name + everything
9
+ // needed to emit a `z.infer<typeof Schema>` line in the augmentation.
10
+ //
11
+ // Patterns we resolve to a strict-checked type:
12
+ // 1. r.defineEvent("name", schemaIdentifier)
13
+ // Position-form, named import. The cleanest case — Schema lives
14
+ // in events.ts, gets re-imported by the augmentation as `import
15
+ // type` and threaded through `z.infer<typeof Schema>`.
16
+ //
17
+ // 2. r.defineEvent({ name, schema })
18
+ // Object-form, otherwise identical to (1).
19
+ //
20
+ // 3. r.defineEvent(NAME_CONST.member, schema...)
21
+ // Computed name via `as const` object. We follow the property-
22
+ // access through ts-morph's symbol-resolver, find the literal
23
+ // member, treat it as the string from (1)/(2). Keeps recipes that
24
+ // centralise event-names in a constants module strict-able.
25
+ //
26
+ // 4. r.defineEvent(name..., z.object({ ... }))
27
+ // Inline schema (call-expression instead of an identifier). We
28
+ // extract the call-source-text and emit it into a co-generated
29
+ // `schemas.generated.ts` as a named `export const`, then point
30
+ // the augmentation at that named export. The original feature
31
+ // file keeps its inline schema for runtime validation; the
32
+ // generated schemas-file exists ONLY for type-inference (consumed
33
+ // via `import type`, erased at build).
34
+ //
35
+ // What we still skip with a warning:
36
+ // - Default- or namespace-imports of the schema identifier
37
+ // (`import S from "..."`, `import * as M from "..."`). Rare in
38
+ // real apps; signal-to-noise of supporting them isn't worth it.
39
+ // - Computed names whose const cannot be statically resolved
40
+ // (cross-package re-exports, dynamic property access).
41
+
42
+ import { readdirSync, statSync } from "node:fs";
43
+ import { join, relative, resolve, sep } from "node:path";
44
+ import {
45
+ type CallExpression,
46
+ type ImportDeclaration,
47
+ type Node,
48
+ Project,
49
+ type PropertyAccessExpression,
50
+ type SourceFile,
51
+ SyntaxKind,
52
+ } from "ts-morph";
53
+
54
+ /**
55
+ * Replicates `packages/framework/src/engine/qualified-name.ts:toKebab`.
56
+ * Frame's r.defineEvent runs the feature-name + event-name through this
57
+ * helper before joining them — `defineFeature("driverOrders")` writes
58
+ * events under `driver-orders:event:...`. We MUST mirror the same
59
+ * transform here, otherwise the augmentation key drifts from the
60
+ * runtime event-type and strict-mode would catch a phantom mismatch.
61
+ *
62
+ * Inlined (instead of imported from framework) to keep the codegen
63
+ * package boundary clean — codegen doesn't depend on the runtime
64
+ * framework, only the framework-source-tree via paths-mapping at the
65
+ * caller's compile.
66
+ */
67
+ function toKebab(input: string): string {
68
+ return input
69
+ .replace(/\./g, "-")
70
+ .replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2")
71
+ .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
72
+ .toLowerCase();
73
+ }
74
+
75
+ export type ScannedEvent = {
76
+ /** Qualified event-name as it appears in the events table:
77
+ * `<feature>:event:<inner>`. The KumikoEventTypeMap key. */
78
+ readonly qualifiedName: string;
79
+ /** Where the type for this event's payload comes from. Two flavours:
80
+ *
81
+ * { kind: "imported", schemaIdentifier, schemaModulePath }
82
+ * Schema is a named export of another file. Augmentation
83
+ * `import type`-s it from there.
84
+ *
85
+ * { kind: "inline", schemaSource, generatedConstName }
86
+ * Schema was inlined at the call-site. We extract the source
87
+ * text into `schemas.generated.ts` under `generatedConstName`
88
+ * and import-type from there. */
89
+ readonly schemaSource: SchemaSource;
90
+ /** Absolute disk-path of the feature file (for relative-path
91
+ * resolution + diagnostics). */
92
+ readonly featureFilePath: string;
93
+ /** For diagnostics + dedup logging. */
94
+ readonly source: { readonly file: string; readonly line: number };
95
+ };
96
+
97
+ export type SchemaSource =
98
+ | {
99
+ readonly kind: "imported";
100
+ readonly schemaIdentifier: string;
101
+ readonly schemaModulePath: string;
102
+ }
103
+ | {
104
+ readonly kind: "inline";
105
+ /** zod source text — `z.object({ id: z.string() })` etc. */
106
+ readonly schemaSource: string;
107
+ /** Name we'll generate inside `schemas.generated.ts`. Stable
108
+ * + qualified-name-derived so reorder of features doesn't
109
+ * rename them. */
110
+ readonly generatedConstName: string;
111
+ };
112
+
113
+ export type ScanWarning = {
114
+ readonly file: string;
115
+ readonly line: number;
116
+ readonly reason: string;
117
+ };
118
+
119
+ export type ScanResult = {
120
+ readonly events: readonly ScannedEvent[];
121
+ readonly warnings: readonly ScanWarning[];
122
+ };
123
+
124
+ export type ScanOptions = {
125
+ /** App-Wurzel — alles unter `<root>/src` wird gescannt. Tests +
126
+ * generated-files (`.kumiko`, `dist*`, `node_modules`) sind raus. */
127
+ readonly appRoot: string;
128
+ };
129
+
130
+ /**
131
+ * Scant `appRoot/src/**` nach `r.defineEvent(...)`-Aufrufen und liefert
132
+ * eine deduplizierte Liste aus ScannedEvent. Doppelte qualifiedName
133
+ * landen mit einer Warnung in der Result — Codegen schreibt nur den
134
+ * ERSTEN, sodass das generated File compile-stabil bleibt.
135
+ */
136
+ export function scanEvents(opts: ScanOptions): ScanResult {
137
+ const project = new Project({
138
+ skipAddingFilesFromTsConfig: true,
139
+ skipFileDependencyResolution: true,
140
+ });
141
+
142
+ const filesToScan: string[] = [];
143
+ collectTsFiles(join(opts.appRoot, "src"), filesToScan);
144
+
145
+ // Add ALL files to the project up-front. ts-morph's symbol-resolver
146
+ // needs to see the file that declares the const-object before it can
147
+ // follow `INVOICE_EVENTS.sent` to its string literal.
148
+ for (const filePath of filesToScan) {
149
+ project.addSourceFileAtPath(filePath);
150
+ }
151
+
152
+ const events: ScannedEvent[] = [];
153
+ const warnings: ScanWarning[] = [];
154
+
155
+ for (const filePath of filesToScan) {
156
+ const sourceFile = project.getSourceFile(filePath);
157
+ if (!sourceFile) continue;
158
+ scanFile(sourceFile, filePath, events, warnings);
159
+ }
160
+
161
+ return { events: dedupe(events, warnings), warnings };
162
+ }
163
+
164
+ // ============================================================================
165
+ // Internal — directory walk
166
+ // ============================================================================
167
+
168
+ const SKIP_SEGMENTS = new Set(["node_modules", ".kumiko", "dist", "dist-server", "__tests__"]);
169
+
170
+ function collectTsFiles(dir: string, out: string[]): void {
171
+ let entries: string[];
172
+ try {
173
+ entries = readdirSync(dir);
174
+ } catch {
175
+ // Directory missing — fine, just no files to scan there.
176
+ return;
177
+ }
178
+ for (const entry of entries) {
179
+ if (entry.startsWith(".")) continue;
180
+ if (SKIP_SEGMENTS.has(entry)) continue;
181
+ const full = join(dir, entry);
182
+ let stat: ReturnType<typeof statSync>;
183
+ try {
184
+ stat = statSync(full);
185
+ } catch {
186
+ continue;
187
+ }
188
+ if (stat.isDirectory()) {
189
+ collectTsFiles(full, out);
190
+ } else if (
191
+ stat.isFile() &&
192
+ (entry.endsWith(".ts") || entry.endsWith(".tsx")) &&
193
+ !entry.endsWith(".d.ts") &&
194
+ !entry.endsWith(".test.ts") &&
195
+ !entry.endsWith(".test.tsx")
196
+ ) {
197
+ out.push(full);
198
+ }
199
+ }
200
+ }
201
+
202
+ // ============================================================================
203
+ // Internal — per-file scan
204
+ // ============================================================================
205
+
206
+ function scanFile(
207
+ sourceFile: SourceFile,
208
+ filePath: string,
209
+ events: ScannedEvent[],
210
+ warnings: ScanWarning[],
211
+ ): void {
212
+ // Find every defineFeature(...) — there may be multiple in factory-style
213
+ // packages (one factory function per feature). Each carries its own
214
+ // featureName (1st arg) + setup-arrow-function (2nd arg).
215
+ for (const call of sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression)) {
216
+ if (call.getExpression().getText() !== "defineFeature") continue;
217
+ const featureName = readStringLiteral(call.getArguments()[0]);
218
+ if (!featureName) continue;
219
+ const setup = call.getArguments()[1]?.asKind(SyntaxKind.ArrowFunction);
220
+ if (!setup) continue;
221
+ const registrarParam = setup.getParameters()[0]?.getName();
222
+ if (!registrarParam) continue;
223
+ for (const defCall of setup.getBody().getDescendantsOfKind(SyntaxKind.CallExpression)) {
224
+ const expr = defCall.getExpression().asKind(SyntaxKind.PropertyAccessExpression);
225
+ if (!expr) continue;
226
+ if (expr.getExpression().getText() !== registrarParam) continue;
227
+ if (expr.getName() !== "defineEvent") continue;
228
+
229
+ collectFromDefineEvent(defCall, sourceFile, featureName, filePath, events, warnings);
230
+ }
231
+ }
232
+ }
233
+
234
+ function collectFromDefineEvent(
235
+ call: CallExpression,
236
+ sourceFile: SourceFile,
237
+ featureName: string,
238
+ filePath: string,
239
+ events: ScannedEvent[],
240
+ warnings: ScanWarning[],
241
+ ): void {
242
+ const parsed = parseDefineEventCall(call);
243
+ if (!parsed) {
244
+ warnings.push({
245
+ file: filePath,
246
+ line: call.getStartLineNumber(),
247
+ reason: "r.defineEvent: cannot read event-name + schema statically",
248
+ });
249
+ return;
250
+ }
251
+
252
+ // Mirror the framework's `qn(toKebab(feature), "event", toKebab(name))`
253
+ // — the augmentation key MUST match what `r.defineEvent` writes at
254
+ // runtime. Without the kebab-step, `defineFeature("driverOrders")`
255
+ // would augment `driverOrders:event:*` while the runtime stream
256
+ // carries `driver-orders:event:*`, and strict-mode would reject every
257
+ // correct call.
258
+ const qualifiedName = `${toKebab(featureName)}:event:${toKebab(parsed.eventName)}`;
259
+ const schema = resolveSchemaSource(parsed.schemaNode, sourceFile, qualifiedName);
260
+ if (!schema) {
261
+ warnings.push({
262
+ file: filePath,
263
+ line: call.getStartLineNumber(),
264
+ reason: `r.defineEvent("${parsed.eventName}"): schema "${parsed.schemaNode.getText()}" — not a named import nor an inline z.* call, skipped`,
265
+ });
266
+ return;
267
+ }
268
+
269
+ events.push({
270
+ qualifiedName,
271
+ schemaSource: schema,
272
+ featureFilePath: filePath,
273
+ source: { file: filePath, line: call.getStartLineNumber() },
274
+ });
275
+ }
276
+
277
+ // ============================================================================
278
+ // Internal — extract event-name + schema-node
279
+ // ============================================================================
280
+
281
+ type ParsedDefineEvent = {
282
+ /** Final `<inner>` part of the qualified name. Always a string-literal
283
+ * AFTER resolution (we follow PropertyAccess → const-member). */
284
+ readonly eventName: string;
285
+ /** AST node for the schema argument — left to be resolved by
286
+ * resolveSchemaSource into either an imported-named or inline-call. */
287
+ readonly schemaNode: Node;
288
+ };
289
+
290
+ function parseDefineEventCall(call: CallExpression): ParsedDefineEvent | undefined {
291
+ const args = call.getArguments();
292
+ const first = args[0];
293
+ if (!first) return undefined;
294
+
295
+ // Object-form: r.defineEvent({ name, schema, version? })
296
+ const obj = first.asKind(SyntaxKind.ObjectLiteralExpression);
297
+ if (obj && args.length === 1) {
298
+ const nameProp = obj
299
+ .getProperty("name")
300
+ ?.asKind(SyntaxKind.PropertyAssignment)
301
+ ?.getInitializer();
302
+ const eventName = nameProp ? resolveStringLiteralOrConst(nameProp) : undefined;
303
+ if (!eventName) return undefined;
304
+ const schemaNode = obj
305
+ .getProperty("schema")
306
+ ?.asKind(SyntaxKind.PropertyAssignment)
307
+ ?.getInitializer();
308
+ if (!schemaNode) return undefined;
309
+ return { eventName, schemaNode };
310
+ }
311
+
312
+ // Position-form: r.defineEvent(<name-source>, <schema-node>, ...)
313
+ const eventName = resolveStringLiteralOrConst(first);
314
+ const schemaNode = args[1];
315
+ if (!eventName || !schemaNode) return undefined;
316
+ return { eventName, schemaNode };
317
+ }
318
+
319
+ /**
320
+ * Resolves a node to a string literal — either it IS a string-literal,
321
+ * or it's a property-access on a `const X = { ... } as const` object
322
+ * whose member resolves to one. Returns undefined for anything else
323
+ * (template literals, dynamic property access, function calls).
324
+ *
325
+ * Cross-file resolution: we follow the named import that brings the
326
+ * receiver-identifier into scope, load the target file from the same
327
+ * ts-morph project, and look up the const-declaration there. This
328
+ * works WITHOUT a TypeChecker — the import-graph + AST is enough for
329
+ * the patterns the recipes/showcases use (`as const` objects with
330
+ * string-literal members).
331
+ */
332
+ function resolveStringLiteralOrConst(node: Node): string | undefined {
333
+ // Direct string-literal — fast path.
334
+ const direct = node.asKind(SyntaxKind.StringLiteral);
335
+ if (direct) return direct.getLiteralValue();
336
+
337
+ // PropertyAccessExpression: `INVOICE_EVENTS.sent` form.
338
+ const propAccess = node.asKind(SyntaxKind.PropertyAccessExpression);
339
+ if (propAccess) return resolvePropertyAccessLiteral(propAccess);
340
+
341
+ return undefined;
342
+ }
343
+
344
+ function resolvePropertyAccessLiteral(propAccess: PropertyAccessExpression): string | undefined {
345
+ const receiver = propAccess.getExpression().asKind(SyntaxKind.Identifier);
346
+ if (!receiver) return undefined;
347
+ const memberName = propAccess.getName();
348
+ const callerFile = propAccess.getSourceFile();
349
+
350
+ // Two paths to find the const-declaration:
351
+ // 1. Local: declared in the same file before the call.
352
+ // 2. Imported: a named import points at another file in the project;
353
+ // the const lives there as an `export const`.
354
+ const local = findConstObject(callerFile, receiver.getText());
355
+ if (local) return readMemberLiteral(local, memberName);
356
+
357
+ for (const importDecl of callerFile.getImportDeclarations()) {
358
+ if (!matchesNamedImport(importDecl, receiver.getText())) continue;
359
+ const targetFile = resolveImportedSourceFile(importDecl, callerFile);
360
+ if (!targetFile) continue;
361
+ const remote = findConstObject(targetFile, receiver.getText());
362
+ if (remote) {
363
+ const literal = readMemberLiteral(remote, memberName);
364
+ if (literal !== undefined) return literal;
365
+ }
366
+ }
367
+ return undefined;
368
+ }
369
+
370
+ /**
371
+ * Walk top-level statements for `const RECEIVER = { ... } [as const]`.
372
+ * Returns the inner object-literal if found.
373
+ */
374
+ function findConstObject(sourceFile: SourceFile, receiverName: string): Node | undefined {
375
+ for (const stmt of sourceFile.getVariableStatements()) {
376
+ for (const decl of stmt.getDeclarations()) {
377
+ if (decl.getName() !== receiverName) continue;
378
+ const init = unwrapAsConst(decl.getInitializer());
379
+ const objLit = init?.asKind(SyntaxKind.ObjectLiteralExpression);
380
+ if (objLit) return objLit;
381
+ }
382
+ }
383
+ return undefined;
384
+ }
385
+
386
+ function readMemberLiteral(objLitNode: Node, memberName: string): string | undefined {
387
+ const objLit = objLitNode.asKind(SyntaxKind.ObjectLiteralExpression);
388
+ if (!objLit) return undefined;
389
+ const prop = objLit.getProperty(memberName)?.asKind(SyntaxKind.PropertyAssignment);
390
+ const initLit = prop?.getInitializer()?.asKind(SyntaxKind.StringLiteral);
391
+ return initLit?.getLiteralValue();
392
+ }
393
+
394
+ /**
395
+ * Resolve a relative import-specifier to a SourceFile already loaded
396
+ * in the project. Tries `<base>.ts` and `<base>/index.ts`. Returns
397
+ * undefined for npm-package specifiers (no local resolution needed —
398
+ * we don't follow anything beyond the app's own files).
399
+ */
400
+ function resolveImportedSourceFile(
401
+ importDecl: ImportDeclaration,
402
+ fromFile: SourceFile,
403
+ ): SourceFile | undefined {
404
+ const spec = importDecl.getModuleSpecifierValue();
405
+ if (!spec.startsWith(".")) return undefined;
406
+ const fromDir = fromFile.getDirectoryPath();
407
+ const project = fromFile.getProject();
408
+ const candidates = [`${spec}.ts`, `${spec}.tsx`, `${spec}/index.ts`, `${spec}/index.tsx`];
409
+ for (const cand of candidates) {
410
+ const abs = resolve(fromDir, cand);
411
+ const sf = project.getSourceFile(abs);
412
+ if (sf) return sf;
413
+ }
414
+ return undefined;
415
+ }
416
+
417
+ /**
418
+ * `{ x: 1 } as const` parses as `AsExpression > ObjectLiteralExpression`.
419
+ * Strip the AsExpression so the caller can hit the inner literal directly.
420
+ */
421
+ function unwrapAsConst(node: Node | undefined): Node | undefined {
422
+ if (!node) return undefined;
423
+ const asExpr = node.asKind(SyntaxKind.AsExpression);
424
+ return asExpr ? asExpr.getExpression() : node;
425
+ }
426
+
427
+ function readStringLiteral(node: Node | undefined): string | undefined {
428
+ return node?.asKind(SyntaxKind.StringLiteral)?.getLiteralValue();
429
+ }
430
+
431
+ // ============================================================================
432
+ // Internal — resolve schema node to its source kind
433
+ // ============================================================================
434
+
435
+ function resolveSchemaSource(
436
+ schemaNode: Node,
437
+ sourceFile: SourceFile,
438
+ qualifiedName: string,
439
+ ): SchemaSource | undefined {
440
+ // Named identifier: schema lives in another file as a named export.
441
+ const ident = schemaNode.asKind(SyntaxKind.Identifier);
442
+ if (ident) {
443
+ const importInfo = resolveSchemaImport(sourceFile, ident.getText());
444
+ if (!importInfo) return undefined;
445
+ return {
446
+ kind: "imported",
447
+ schemaIdentifier: ident.getText(),
448
+ schemaModulePath: importInfo.moduleSpecifier,
449
+ };
450
+ }
451
+
452
+ // Inline call: r.defineEvent("name", z.object({...})).
453
+ // We accept any call-expression that LOOKS like a zod schema (cheap
454
+ // structural check on the `z.*` head). The exact ".object/.string/etc"
455
+ // doesn't matter — the codegen will replay the source text in the
456
+ // schemas-file, where TS resolves z.infer correctly.
457
+ const callExpr = schemaNode.asKind(SyntaxKind.CallExpression);
458
+ if (callExpr && looksLikeZodCall(callExpr)) {
459
+ return {
460
+ kind: "inline",
461
+ schemaSource: callExpr.getText(),
462
+ generatedConstName: qualifiedNameToConstName(qualifiedName),
463
+ };
464
+ }
465
+
466
+ return undefined;
467
+ }
468
+
469
+ function looksLikeZodCall(call: CallExpression): boolean {
470
+ // Walk the callee head — `z.something(...)` or `z.something.foo(...)`,
471
+ // anything that traces back to an Identifier `z`. Conservative: we
472
+ // don't try to verify it's the actual zod-import (the runtime check
473
+ // happens through the schemas-file's `import { z } from "zod"` anyway,
474
+ // which fails loudly if the user's `z` is something else).
475
+ let cur: Node = call.getExpression();
476
+ while (cur.asKind(SyntaxKind.PropertyAccessExpression) || cur.asKind(SyntaxKind.CallExpression)) {
477
+ const prop = cur.asKind(SyntaxKind.PropertyAccessExpression);
478
+ if (prop) {
479
+ cur = prop.getExpression();
480
+ continue;
481
+ }
482
+ const innerCall = cur.asKind(SyntaxKind.CallExpression);
483
+ if (innerCall) {
484
+ cur = innerCall.getExpression();
485
+ }
486
+ }
487
+ return cur.asKind(SyntaxKind.Identifier)?.getText() === "z";
488
+ }
489
+
490
+ function resolveSchemaImport(
491
+ sourceFile: SourceFile,
492
+ identifier: string,
493
+ ): { readonly moduleSpecifier: string } | undefined {
494
+ for (const importDecl of sourceFile.getImportDeclarations()) {
495
+ if (matchesNamedImport(importDecl, identifier)) {
496
+ return { moduleSpecifier: importDecl.getModuleSpecifierValue() };
497
+ }
498
+ }
499
+ return undefined;
500
+ }
501
+
502
+ function matchesNamedImport(importDecl: ImportDeclaration, identifier: string): boolean {
503
+ for (const named of importDecl.getNamedImports()) {
504
+ // Handles both `import { x }` and `import { x as y }` — we want the
505
+ // LOCAL alias (what's used in the call site), which matches the
506
+ // identifier the scanner extracted.
507
+ const localName = named.getAliasNode()?.getText() ?? named.getNameNode().getText();
508
+ if (localName === identifier) return true;
509
+ }
510
+ return false;
511
+ }
512
+
513
+ /**
514
+ * Stable identifier-safe rewrite of a qualifiedName like
515
+ * `pubsubOrders:event:order-placed` → `_kg_pubsubOrders__orderPlaced`.
516
+ * Used for the `export const` name in `schemas.generated.ts`. Same
517
+ * transform in scan + render keeps the two sides in sync.
518
+ */
519
+ export function qualifiedNameToConstName(qualifiedName: string): string {
520
+ // Drop the ":event:" infix — every entry has it, no information.
521
+ const withoutEventInfix = qualifiedName.replace(/:event:/g, "__");
522
+ // Replace remaining colons + dashes with `_` and camel-case after `_`
523
+ // so the output is identifier-legal AND visually parseable.
524
+ const sanitised = withoutEventInfix.replace(/[^A-Za-z0-9_]+(.?)/g, (_match, next: string) =>
525
+ next.toUpperCase(),
526
+ );
527
+ return `_kg_${sanitised}`;
528
+ }
529
+
530
+ // ============================================================================
531
+ // Internal — module-specifier rewriting + dedup
532
+ // ============================================================================
533
+
534
+ /**
535
+ * Resolves the relative schema-import path from the feature-file's
536
+ * point-of-view to a path relative to `.kumiko/` (which is where the
537
+ * generated d.ts file lives). Workspace-package specifiers
538
+ * (`@cosmicdrift/kumiko-...`) are returned as-is.
539
+ */
540
+ export function rewriteImportPath(
541
+ schemaModulePath: string,
542
+ featureFilePath: string,
543
+ outputDirAbs: string,
544
+ ): string {
545
+ // Workspace / npm specifiers — pass through. Codegen output imports
546
+ // them by name.
547
+ if (!schemaModulePath.startsWith(".")) return schemaModulePath;
548
+
549
+ const featureDir = featureFilePath.substring(0, featureFilePath.lastIndexOf(sep));
550
+ const absoluteSchemaPath = resolve(featureDir, schemaModulePath);
551
+ const fromOutput = relative(outputDirAbs, absoluteSchemaPath);
552
+ // POSIX-Slash für TS-imports (auch auf Windows).
553
+ const normalised = fromOutput.split(sep).join("/");
554
+ // Strip .ts/.tsx — TS module specifiers don't carry the extension;
555
+ // resolve() didn't add one but a hand-written ".ts" should be removed.
556
+ return normalised.replace(/\.tsx?$/, "");
557
+ }
558
+
559
+ function dedupe(events: ScannedEvent[], warnings: ScanWarning[]): ScannedEvent[] {
560
+ const seen = new Map<string, ScannedEvent>();
561
+ for (const ev of events) {
562
+ const existing = seen.get(ev.qualifiedName);
563
+ if (existing) {
564
+ warnings.push({
565
+ file: ev.source.file,
566
+ line: ev.source.line,
567
+ reason: `duplicate r.defineEvent("${ev.qualifiedName}") — first declared at ${existing.source.file}:${existing.source.line}, ignored here`,
568
+ });
569
+ continue;
570
+ }
571
+ seen.set(ev.qualifiedName, ev);
572
+ }
573
+ return [...seen.values()];
574
+ }
@@ -0,0 +1,127 @@
1
+ // watchAndRegenerate — file-watcher der bei jeder TS-Änderung unter
2
+ // `<appRoot>/src/**` `runCodegen` neu fährt. Lebendige IDE-DX: User
3
+ // editiert ein r.defineEvent, drückt save, das `.kumiko/types.generated.d.ts`
4
+ // ist innerhalb von ~50ms aktualisiert, der TS-Sprachserver merkt es,
5
+ // neue Auto-Complete-Vorschläge erscheinen ohne Server-Restart.
6
+ //
7
+ // Implementation: node:fs.watch (recursive) auf `<appRoot>/src/`.
8
+ // Debounced damit ein batch-edit (z.B. find+replace via sed) nicht
9
+ // 50× hintereinander feuert. Idempotent — runCodegen schreibt nur bei
10
+ // echter Änderung, der Watcher kann ohne Schaden über-ruft werden.
11
+ //
12
+ // Nicht-Ziele: kein Watcher auf `node_modules`, `.kumiko`, `dist*`,
13
+ // `__tests__` (gleicher SKIP-Set wie scan-events). fs.watch's
14
+ // recursive-Mode liefert events für jede Subdir; wir filtern im
15
+ // Callback.
16
+
17
+ import { type FSWatcher, watch } from "node:fs";
18
+ import { join } from "node:path";
19
+ import { runCodegen } from "./run-codegen";
20
+
21
+ export type WatchOptions = {
22
+ /** App-Wurzel — gleiche Bedeutung wie für `runCodegen`. */
23
+ readonly appRoot: string;
24
+ /** Wartet diese Millisekunden zusätzliche Events ab, bevor codegen
25
+ * einmal fährt. 50ms catched typische save-bursts (Editor-Saves
26
+ * feuern oft 2-3 Events: temp-file → rename → cleanup), bleibt aber
27
+ * unmerklich für den User. */
28
+ readonly debounceMs?: number;
29
+ /** Callback nach jedem erfolgreichen Codegen-Pass. Default: stderr-
30
+ * Log mit event-count + warnings. Wer den Output strukturiert
31
+ * konsumieren will (Dev-Server-UI, IDE-Plugin), gibt einen eigenen
32
+ * Handler. */
33
+ readonly onResult?: (result: ReturnType<typeof runCodegen>) => void;
34
+ /** Callback bei runCodegen-Fehlern. Default: stderr-Warning. */
35
+ readonly onError?: (err: unknown) => void;
36
+ };
37
+
38
+ export type WatchHandle = {
39
+ /** Beendet den Watcher. Idempotent — mehrfacher Aufruf ist no-op. */
40
+ readonly close: () => void;
41
+ };
42
+
43
+ const SKIP_SUBSTRINGS = ["/node_modules/", "/.kumiko/", "/dist/", "/dist-server/", "/__tests__/"];
44
+
45
+ const DEFAULT_DEBOUNCE_MS = 50;
46
+
47
+ /**
48
+ * Startet den Watcher. Beim Boot fährt einmalig `runCodegen` (sodass
49
+ * die generated Files sofort frisch sind), dann hängt sich an `fs.watch`
50
+ * und re-runs bei file-changes mit Debounce.
51
+ */
52
+ export function watchAndRegenerate(opts: WatchOptions): WatchHandle {
53
+ const debounceMs = opts.debounceMs ?? DEFAULT_DEBOUNCE_MS;
54
+ const srcDir = join(opts.appRoot, "src");
55
+ let timer: ReturnType<typeof setTimeout> | undefined;
56
+ let watcher: FSWatcher | undefined;
57
+ let closed = false;
58
+
59
+ const fire = () => {
60
+ try {
61
+ const result = runCodegen({ appRoot: opts.appRoot });
62
+ if (opts.onResult) {
63
+ opts.onResult(result);
64
+ } else {
65
+ if (result.warnings.length > 0) {
66
+ for (const w of result.warnings) {
67
+ // biome-ignore lint/suspicious/noConsole: codegen-watcher logs to terminal
68
+ console.warn(`[codegen] ${w.file}:${w.line} — ${w.reason}`);
69
+ }
70
+ }
71
+ }
72
+ } catch (err) {
73
+ if (opts.onError) opts.onError(err);
74
+ else {
75
+ // biome-ignore lint/suspicious/noConsole: codegen-watcher logs to terminal
76
+ console.warn(
77
+ `[codegen] regenerate failed: ${err instanceof Error ? err.message : String(err)}`,
78
+ );
79
+ }
80
+ }
81
+ };
82
+
83
+ // Initial run — gleiches Verhalten wie wenn ein file-change kam, aber
84
+ // ohne Debounce (User wartet auf den ersten codegen).
85
+ fire();
86
+
87
+ try {
88
+ watcher = watch(srcDir, { recursive: true }, (_eventType, filename) => {
89
+ if (closed || !filename) return;
90
+ // node liefert filename relativ zu srcDir, kann aber posix oder
91
+ // windows-style separators haben. Wir prüfen substring-tolerant.
92
+ const normalised = `/${filename.toString().replace(/\\/g, "/")}`;
93
+ if (SKIP_SUBSTRINGS.some((seg) => normalised.includes(seg))) return;
94
+ // Nur .ts/.tsx interessieren — alles andere (CSS, MD, JSON) hat
95
+ // keinen Einfluss auf r.defineEvent-Calls.
96
+ if (!normalised.endsWith(".ts") && !normalised.endsWith(".tsx")) return;
97
+ // .d.ts ausgenommen — die kommen meistens vom codegen selbst.
98
+ if (normalised.endsWith(".d.ts")) return;
99
+ // .test.ts/.test.tsx ausgenommen — Tests definieren keine
100
+ // Production-Features.
101
+ if (normalised.endsWith(".test.ts") || normalised.endsWith(".test.tsx")) return;
102
+
103
+ if (timer) clearTimeout(timer);
104
+ timer = setTimeout(fire, debounceMs);
105
+ });
106
+ } catch (err) {
107
+ // Watch failed (z.B. fs nicht recursive-fähig auf Linux ohne
108
+ // patches) — degraded mode: codegen läuft nur beim initial-call.
109
+ // User kriegt keine live-updates, aber das ist nicht fatal.
110
+ if (opts.onError) opts.onError(err);
111
+ else {
112
+ // biome-ignore lint/suspicious/noConsole: codegen-watcher logs to terminal
113
+ console.warn(
114
+ `[codegen] watcher failed to start (live-updates disabled): ${err instanceof Error ? err.message : String(err)}`,
115
+ );
116
+ }
117
+ }
118
+
119
+ return {
120
+ close: () => {
121
+ if (closed) return;
122
+ closed = true;
123
+ if (timer) clearTimeout(timer);
124
+ watcher?.close();
125
+ },
126
+ };
127
+ }