@gaganref/confect-cli 10.3.1-fork.9 → 10.3.1-fork.91

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.
@@ -1,8 +1,8 @@
1
1
  import { fromBundlerError } from "../BuildError.mjs";
2
2
  import { ProjectRoot } from "../ProjectRoot.mjs";
3
3
  import { logFileAdded, logFileModified, logFileRemoved, logPending, logSuccess, logWarn } from "../log.mjs";
4
- import { bundle, resolveModule } from "../Bundler.mjs";
5
- import { CallbackPrefixError, ComponentNodeGroupError, ConflictingDocNameError, InvalidConvexConfigError, LegacySchemaFileError, tapAndLog } from "../CodegenError.mjs";
4
+ import { bundle, exportNames, resolveModule } from "../Bundler.mjs";
5
+ import { CallbackPrefixError, ComponentNodeGroupError, ConflictingDocNameError, InvalidConvexConfigError, InvalidDeclarationError, LegacySchemaFileError, tapAndLog } from "../CodegenError.mjs";
6
6
  import { ConvexDirectory } from "../ConvexDirectory.mjs";
7
7
  import { ConfectDirectory } from "../ConfectDirectory.mjs";
8
8
  import { assemblyNodesFromLeaves } from "../SpecAssemblyNode.mjs";
@@ -12,13 +12,15 @@ import { CONVEX_CONFIG_FILENAME, componentName, discoverInstalledComponents, res
12
12
  import { flags, layer } from "../DirectoryOptions.mjs";
13
13
  import { fromTableName } from "../DocName.mjs";
14
14
  import { FunctionPaths, make } from "../FunctionPaths.mjs";
15
+ import { removeUnclaimed, sync, verify } from "../SectionFiles.mjs";
15
16
  import { diff, fromLoadedLeaves, update } from "../GenerationSnapshot.mjs";
16
- import { applyFunctionPlan, generateFunctions } from "../FunctionModule.mjs";
17
- import { implPathForSpec, loadAll, registeredFunctionsRelativePath, validateAllImpls, withImplFiles } from "../LeafModule.mjs";
17
+ import { applyFunctionPlan, assertOwnedEntrypoints, generateFunctions } from "../FunctionModule.mjs";
18
+ import { discoverLeafSpecFiles, implPathForSpec, loadAll, registeredFunctionsRelativePath, toLeafModule, validateAllImpls, withImplFiles } from "../LeafModule.mjs";
18
19
  import { TABLES_DIRNAME, discover, validate } from "../TableModule.mjs";
19
20
  import { planDeclarations } from "../EntryPoint.mjs";
20
21
  import { preserveComponentOutput } from "../Rollback.mjs";
21
- import { syncDeclaration, syncDeclaredHttp } from "./declaration.mjs";
22
+ import { refuseSpecSections } from "../ManifestSections.mjs";
23
+ import { readPrevious, syncDeclaration, syncDeclaredHttp, syncDeployModule, syncManifestSections } from "./declaration.mjs";
22
24
  import * as Effect from "effect/Effect";
23
25
  import * as Command from "effect/cli/Command";
24
26
  import { App, Callback, Env, Spec } from "@confect/core";
@@ -33,6 +35,10 @@ import * as Option from "effect/Option";
33
35
  import * as Record from "effect/Record";
34
36
  //#region src/confect/codegen.ts
35
37
  const GENERATED_DIRNAME = "_generated";
38
+ const declaredSections = (backend) => Option.match(backend, {
39
+ onNone: () => [],
40
+ onSome: ({ sections }) => sections
41
+ });
36
42
  const isComponent = Effect.map(ConvexDirectory.target, (target) => target.kind === "component");
37
43
  const GENERATED_SPEC_PATH = Effect.map(Path.Path, (path) => path.join(GENERATED_DIRNAME, "spec.ts"));
38
44
  const GENERATED_SCHEMA_PATH = Effect.map(Path.Path, (path) => path.join(GENERATED_DIRNAME, "schema.ts"));
@@ -60,47 +66,58 @@ const codegen = Command.make("codegen", flags, () => Effect.gen(function* () {
60
66
  })).pipe(Command.provide(layer), Command.withDescription("Generate `confect/_generated` files and the contents of the `convex` directory (except `tsconfig.json`, and `convex.config.ts` unless `confect/app.ts` declares the backend)"));
61
67
  const generateWithSnapshot = Effect.gen(function* () {
62
68
  yield* validateComponentConfiguration;
69
+ yield* rejectDeployModuleGroup;
63
70
  yield* generateConfectGeneratedDirectory;
64
- const declared = yield* syncDeclaration;
71
+ const previousManifest = yield* readPrevious;
65
72
  yield* rejectLegacySchemaFile;
66
73
  const tableModules = yield* discover;
67
74
  yield* warnIfNoTables(tableModules);
68
75
  yield* generateIdConstructor(tableModules);
69
- yield* generateComponents;
76
+ yield* generateTableWrappers(tableModules);
77
+ yield* removeObsoleteTableWrappers(tableModules);
78
+ yield* generateAssembledSpecs(yield* Effect.forEach(yield* discoverLeafSpecFiles, toLeafModule));
79
+ yield* generateRefs;
80
+ yield* provisionalComponents;
81
+ const { app: declared, digest, backend } = yield* syncDeclaration(previousManifest);
82
+ const mounts = yield* generateComponents;
70
83
  yield* validateCallbackPrefixes(declared);
71
84
  yield* validate(tableModules);
72
85
  yield* validateNoDocNameCollisions(tableModules);
73
- yield* generateTableWrappers(tableModules);
74
- yield* removeObsoleteTableWrappers(tableModules);
75
86
  yield* generateRuntimeSchema(tableModules);
76
87
  const leaves = yield* loadAll;
77
88
  yield* rejectComponentNodeGroups(leaves);
89
+ yield* Effect.fromResult(refuseSpecSections(leaves));
90
+ const sections = yield* sync(backend, leaves, mounts);
91
+ yield* assertOwnedEntrypoints;
92
+ yield* syncManifestSections(declaredSections(backend), leaves, previousManifest, digest, sections.modules);
78
93
  yield* removeLegacyFiles;
79
94
  const snapshot = yield* Effect.fromResult(fromLoadedLeaves(leaves));
80
- yield* generateAssembledSpecs(leaves);
81
95
  yield* Effect.all([
82
96
  removeGeneratedApi,
83
- generateRefs,
84
97
  generateComponentContract(tableModules),
85
98
  removeGeneratedNodeApi,
86
99
  generateDocs(tableModules),
87
100
  generateServices,
88
101
  generateConvexSchema(tableModules)
89
102
  ], { concurrency: "unbounded" });
90
- yield* validateAllImpls(leaves);
91
- yield* generateGroupRegisteredFunctions(leaves);
103
+ const required = Option.isSome(declared) ? declared.value.requiresMiddleware : [];
104
+ yield* validateAllImpls(leaves, required);
105
+ yield* generateGroupRegisteredFunctions(leaves, required, sections.files);
92
106
  yield* removeObsoleteRegisteredFunctions(leaves);
93
107
  const [functionPaths] = yield* Effect.all([
94
108
  generateFunctions(snapshot),
95
109
  generateConvexSchemaReexport,
96
110
  logGenerated(Option.match(declared, {
97
111
  onNone: () => generateHttp,
98
- onSome: syncDeclaredHttp
112
+ onSome: (app) => syncDeclaredHttp(app, sections.files)
99
113
  })),
100
114
  logGenerated(generateCrons),
101
115
  Option.isNone(declared) ? logGenerated(generateAuthConfig) : Effect.succeedNone
102
116
  ], { concurrency: "unbounded" });
117
+ yield* removeUnclaimed(sections.files);
118
+ yield* syncDeployModule(declared);
103
119
  if (Option.isSome(declared)) yield* generateTesting(declared.value);
120
+ yield* verify(backend, mounts);
104
121
  return {
105
122
  functionPaths,
106
123
  snapshot
@@ -109,15 +126,27 @@ const generateWithSnapshot = Effect.gen(function* () {
109
126
  const makeCodegenHandler = (duringRestore) => preserveComponentOutput(generateWithSnapshot, duringRestore).pipe(Effect.tap(() => touchConvexSchema), Effect.map(({ functionPaths }) => ({ functionPaths })));
110
127
  const codegenHandler = makeCodegenHandler();
111
128
  const updateDeclarations = Effect.fn("Cli.updateDeclarations")(function* (snapshot, declarations, affectedImpls) {
129
+ yield* rejectDeployModuleGroup;
130
+ const previousManifest = yield* readPrevious;
112
131
  const path = yield* Path.Path;
113
132
  const { paths, tables } = planDeclarations(declarations, affectedImpls, path);
114
133
  yield* validate(tables);
115
134
  return yield* withImplFiles(paths, (leaves) => Effect.gen(function* () {
116
135
  yield* rejectComponentNodeGroups(leaves);
136
+ yield* Effect.fromResult(refuseSpecSections(leaves));
117
137
  const next = yield* Effect.fromResult(update(snapshot, leaves));
118
- yield* validateAllImpls(leaves);
119
- yield* generateGroupRegisteredFunctions(leaves);
138
+ const { app: declared, digest, backend } = yield* syncDeclaration(previousManifest);
139
+ const all = Option.isSome(declared) ? yield* loadAll : [];
140
+ const mounts = Option.isSome(backend) ? yield* installedMounts : [];
141
+ const sections = yield* sync(backend, all, mounts);
142
+ if (Option.isSome(declared)) yield* syncManifestSections(declaredSections(backend), all, previousManifest, digest, sections.modules);
143
+ const required = Option.isSome(declared) ? declared.value.requiresMiddleware : [];
144
+ yield* validateAllImpls(leaves, required);
145
+ yield* generateGroupRegisteredFunctions(sections.changed ? all : leaves, required, sections.files);
146
+ if (sections.changed && Option.isSome(declared)) yield* logGenerated(syncDeclaredHttp(declared.value, sections.files));
147
+ yield* removeUnclaimed(sections.files);
120
148
  yield* applyFunctionPlan(diff(snapshot, next));
149
+ yield* verify(backend, mounts);
121
150
  return next;
122
151
  }));
123
152
  });
@@ -196,14 +225,16 @@ const generateAssembledSpecs = Effect.fnUntraced(function* (leaves) {
196
225
  const specContents = yield* assembledSpec({ nodes });
197
226
  yield* writeFileStringAndLog(path.join(confectDirectory, generatedSpecPath), specContents);
198
227
  });
199
- const generateGroupRegisteredFunctions = Effect.fnUntraced(function* (leaves) {
228
+ const generateGroupRegisteredFunctions = Effect.fnUntraced(function* (leaves, required, sectionFiles) {
200
229
  const path = yield* Path.Path;
230
+ const fs = yield* FileSystem.FileSystem;
201
231
  const confectDirectory = yield* ConfectDirectory.get;
232
+ const runtimePath = path.join(confectDirectory, "runtime.ts");
233
+ const hasRuntime = yield* fs.exists(runtimePath);
202
234
  yield* Effect.forEach(leaves, Effect.fnUntraced(function* (leaf) {
203
235
  const registryRelativePath = yield* registeredFunctionsRelativePath(leaf);
204
236
  const registryPath = path.join(confectDirectory, "_generated", registryRelativePath);
205
237
  const registryDir = path.dirname(registryPath);
206
- const fs = yield* FileSystem.FileSystem;
207
238
  if (!(yield* fs.exists(registryDir))) yield* fs.makeDirectory(registryDir, { recursive: true });
208
239
  const implRelativePath = yield* implPathForSpec(leaf.relativePath);
209
240
  const schemaImportPath = yield* toModuleImportPath(path.relative(path.dirname(registryPath), path.join(confectDirectory, "_generated", "schema.ts")));
@@ -211,9 +242,13 @@ const generateGroupRegisteredFunctions = Effect.fnUntraced(function* (leaves) {
211
242
  const implImportPath = yield* toModuleImportPath(path.relative(path.dirname(registryPath), path.join(confectDirectory, implRelativePath)));
212
243
  const contents = yield* registeredFunctionsForGroup({
213
244
  spec: leaf.spec,
245
+ required,
246
+ groupPath: leaf.groupPathDot,
214
247
  schemaImportPath,
215
248
  specImportPath,
216
- implImportPath
249
+ implImportPath,
250
+ runtimeImportPath: hasRuntime ? yield* toModuleImportPath(path.relative(registryDir, runtimePath)) : void 0,
251
+ sectionImportPaths: yield* Effect.forEach(sectionFiles, (file) => toModuleImportPath(path.relative(registryDir, path.join(confectDirectory, "_generated", file))))
217
252
  });
218
253
  yield* writeFileStringAndLog(registryPath, contents);
219
254
  }));
@@ -294,6 +329,15 @@ const rejectLegacySchemaFile = Effect.gen(function* () {
294
329
  const legacyPath = path.join(confectDirectory, "schema.ts");
295
330
  if (yield* fs.exists(legacyPath)) return yield* new LegacySchemaFileError({ schemaPath: "schema.ts" });
296
331
  });
332
+ const rejectDeployModuleGroup = Effect.gen(function* () {
333
+ const fs = yield* FileSystem.FileSystem;
334
+ const path = yield* Path.Path;
335
+ const confectDirectory = yield* ConfectDirectory.get;
336
+ if (yield* fs.exists(path.join(confectDirectory, "confectDeploy.spec.ts"))) return yield* new InvalidDeclarationError({
337
+ declarationPath: "confect/confectDeploy.spec.ts",
338
+ reason: "its functions would go in convex/confectDeploy.ts, which codegen writes for the mounts a push initialises. Rename the group, for example to confect/deploy.spec.ts."
339
+ });
340
+ });
297
341
  /**
298
342
  * Surface a yellow `⚠` warning when codegen sees no tables—either the
299
343
  * `confect/tables/` directory is missing or it contains no `.ts` files.
@@ -458,30 +502,44 @@ const generateRefs = Effect.gen(function* () {
458
502
  const refsContents = yield* refs({ specImportPath });
459
503
  yield* writeFileStringAndLog(refsPath, refsContents);
460
504
  });
505
+ const installedMounts = Effect.gen(function* () {
506
+ const fs = yield* FileSystem.FileSystem;
507
+ const path = yield* Path.Path;
508
+ const confectDirectory = yield* ConfectDirectory.get;
509
+ const convexDirectory = yield* ConvexDirectory.get;
510
+ const projectRoot = yield* ProjectRoot.get;
511
+ const convexConfigPath = path.join(convexDirectory, CONVEX_CONFIG_FILENAME);
512
+ const installedComponents = (yield* fs.exists(convexConfigPath)) ? yield* discoverInstalledComponents(convexConfigPath, path.relative(projectRoot, convexConfigPath), (yield* ConvexDirectory.target).kind) : [];
513
+ return Array.map(installedComponents, ({ name, componentDefinitionPath }) => ({
514
+ name,
515
+ definition: typeImportPath(path, componentDefinitionPath, path.join(confectDirectory, GENERATED_DIRNAME))
516
+ }));
517
+ });
461
518
  /**
462
519
  * Generate `confect/_generated/components.ts`—the typed registry of Convex
463
520
  * components installed in `convex/convex.config.ts` (see
464
521
  * {@link templates.components}). The file is emitted even when there is no
465
522
  * `convex.config.ts` (as an empty registry) so the import surface stays stable.
466
523
  * It must exist before loading tables and specs because they may import bound
467
- * component IDs and schemas.
524
+ * component IDs and schemas. Returns each mount with the specifier of its
525
+ * definition, relative to `_generated` for an in-repo component.
468
526
  */
469
527
  const generateComponents = Effect.gen(function* () {
470
- const fs = yield* FileSystem.FileSystem;
471
528
  const path = yield* Path.Path;
472
529
  const confectDirectory = yield* ConfectDirectory.get;
473
- const convexDirectory = yield* ConvexDirectory.get;
474
- const projectRoot = yield* ProjectRoot.get;
475
- const confectGeneratedDirectory = path.join(confectDirectory, GENERATED_DIRNAME);
476
- const generatedComponentsPath = yield* GENERATED_COMPONENTS_PATH;
477
- const componentsPath = path.join(confectDirectory, generatedComponentsPath);
478
- const convexConfigPath = path.join(convexDirectory, CONVEX_CONFIG_FILENAME);
479
- const installedComponents = (yield* fs.exists(convexConfigPath)) ? yield* discoverInstalledComponents(convexConfigPath, path.relative(projectRoot, convexConfigPath), (yield* ConvexDirectory.target).kind) : [];
480
- const contents = yield* components({ components: Array.map(installedComponents, ({ name, componentDefinitionPath }) => ({
530
+ const mounts = yield* installedMounts;
531
+ const contents = yield* components({ components: Array.map(mounts, ({ name, definition }) => ({
481
532
  name,
482
- typeImportPath: typeImportPath(path, componentDefinitionPath, confectGeneratedDirectory)
533
+ typeImportPath: definition
483
534
  })) });
484
- yield* writeFileStringAndLog(componentsPath, contents);
535
+ yield* writeFileStringAndLog(path.join(confectDirectory, yield* GENERATED_COMPONENTS_PATH), contents);
536
+ return mounts;
537
+ });
538
+ const provisionalComponents = Effect.gen(function* () {
539
+ const fs = yield* FileSystem.FileSystem;
540
+ const registry = (yield* Path.Path).join(yield* ConfectDirectory.get, yield* GENERATED_COMPONENTS_PATH);
541
+ if (yield* fs.exists(registry)) return;
542
+ yield* writeFileStringAndLog(registry, yield* components({ components: [] }));
485
543
  });
486
544
  const logGenerated = (effect) => effect.pipe(Effect.tap(Option.match({
487
545
  onNone: () => Effect.void,
@@ -491,12 +549,31 @@ const logGenerated = (effect) => effect.pipe(Effect.tap(Option.match({
491
549
  * Test-only imports never enter the runtime schema or function wrappers.
492
550
  */
493
551
  const quote = (value) => JSON.stringify(value);
552
+ /**
553
+ * A descriptor's `testing` kit must be a package specifier, so the generated
554
+ * registry holds no machine-specific path, and must export `register`. A kit
555
+ * whose exports cannot be listed without running it passes here, and
556
+ * `TestConfect.layer` names the mount if it has no `register`.
557
+ */
558
+ const checkTestKit = Effect.fnUntraced(function* (mount, specifier, config, configDisplay) {
559
+ const path = yield* Path.Path;
560
+ const invalid = (problem) => new InvalidDeclarationError({
561
+ declarationPath: "confect/app.ts",
562
+ reason: `the mount ${quote(mount)} names the test kit ${quote(specifier)} in its component descriptor${problem}`
563
+ });
564
+ if (specifier.startsWith(".") || path.isAbsolute(specifier)) return yield* invalid("; name it by a package specifier, such as \"<package>/testing\".");
565
+ const resolved = resolveModule(specifier, config);
566
+ if (Option.isNone(resolved)) return yield* invalid(`, which does not resolve from ${configDisplay}.`);
567
+ const exports = yield* exportNames(resolved.value).pipe(Effect.mapError((error) => fromBundlerError(specifier, error)));
568
+ if (Option.isSome(exports) && !exports.value.includes("register")) return yield* invalid(", which does not export `register(t, name)`.");
569
+ });
494
570
  const generateTesting = Effect.fnUntraced(function* (app) {
495
571
  const fs = yield* FileSystem.FileSystem;
496
572
  const path = yield* Path.Path;
497
573
  const confectDirectory = yield* ConfectDirectory.get;
498
574
  const convexDirectory = yield* ConvexDirectory.get;
499
575
  const generated = path.join(confectDirectory, "_generated");
576
+ const configDisplay = path.relative(yield* ProjectRoot.get, path.join(convexDirectory, CONVEX_CONFIG_FILENAME)).split(path.sep).join("/");
500
577
  const relative = (file) => {
501
578
  const value = path.relative(generated, file).split(path.sep).join("/");
502
579
  return value.startsWith(".") ? value : `./${value}`;
@@ -519,15 +596,21 @@ const generateTesting = Effect.fnUntraced(function* (app) {
519
596
  const installed = yield* discoverInstalledComponents(config, relative(config), kind);
520
597
  for (const component of installed) {
521
598
  const name = prefix + component.name;
599
+ const mount = prefix === "" ? App.mounts(app).find((declared) => declared.name === component.name) : void 0;
522
600
  const fromPackage = !path.isAbsolute(component.componentDefinitionPath);
601
+ const named = fromPackage ? mount?.component.testing : void 0;
602
+ if (named !== void 0) yield* checkTestKit(name, named, config, configDisplay);
603
+ const kitEntries = named !== void 0 ? [named] : fromPackage ? [`${component.componentDefinitionPath}/testing`, `${component.componentDefinitionPath}/test`] : [];
604
+ const kitEntry = kitEntries.find((entry) => Option.isSome(resolveModule(entry, config)));
523
605
  const resolved = fromPackage ? path.dirname(yield* resolveDefinition(`${component.componentDefinitionPath}/convex.config`, config, path).pipe(Effect.fromOption, Effect.mapError(() => new InvalidConvexConfigError({
524
606
  configPath: relative(config),
525
607
  reason: `Cannot resolve component ${component.componentDefinitionPath} for testing.`
526
608
  })))) : component.componentDefinitionPath;
527
- const localPrefix = prefix === "" ? App.mounts(app).find((mount) => mount.name === component.name)?.httpPrefix : component.httpPrefix;
609
+ const localPrefix = prefix === "" ? mount?.httpPrefix : component.httpPrefix;
528
610
  const httpPrefix = parentHttpPrefix === void 0 || localPrefix === void 0 ? void 0 : `${parentHttpPrefix.replace(/\/+$/, "")}${localPrefix}`.replace(/\/+$/, "") || "/";
529
611
  if (httpPrefix !== void 0 && ((yield* fs.exists(path.join(resolved, "http.ts"))) || (yield* fs.exists(path.join(resolved, "http.js"))))) httpMounts.push(`{ componentPath: ${quote(name)}, httpPrefix: ${quote(httpPrefix)} }`);
530
- let identifier = kits.get(resolved);
612
+ const kitKey = kitEntry ?? resolved;
613
+ let identifier = kits.get(kitKey);
531
614
  if (identifier === void 0) {
532
615
  const cleaned = name.replaceAll(/[^A-Za-z0-9_$]/g, "_");
533
616
  const base = /^[A-Za-z_$]/.test(cleaned) ? cleaned : `_${cleaned}`;
@@ -539,17 +622,16 @@ const generateTesting = Effect.fnUntraced(function* (app) {
539
622
  }
540
623
  identifiers.add(identifier);
541
624
  identifiers.add(schemaIdentifier);
542
- kits.set(resolved, identifier);
543
- if (fromPackage) {
544
- const testEntry = `${component.componentDefinitionPath}/test`;
545
- if (Option.isNone(resolveModule(testEntry, config))) return yield* new InvalidConvexConfigError({
546
- configPath: relative(config),
547
- reason: `the component package ${quote(component.componentDefinitionPath)} has no ${quote(testEntry)} export. Tests register a component from another package through that entry's \`register(t, name)\`.`
548
- });
549
- imports.push(`import * as ${identifier} from ${quote(testEntry)};`);
550
- } else {
625
+ kits.set(kitKey, identifier);
626
+ if (!fromPackage) {
551
627
  imports.push(`import ${schemaIdentifier} from ${quote(relative(path.join(resolved, "schema.js")))};`);
552
628
  declarations.push(`const ${identifier} = { schema: ${schemaIdentifier}, modules: ${yield* moduleMap(resolved)} };`);
629
+ } else {
630
+ if (kitEntry === void 0) return yield* new InvalidConvexConfigError({
631
+ configPath: relative(config),
632
+ reason: `the component package ${quote(component.componentDefinitionPath)} has no ${kitEntries.map(quote).join(" or ")} export. Tests register a component from another package through that entry's \`register(t, name)\`.`
633
+ });
634
+ imports.push(`import * as ${identifier} from ${quote(kitEntry)};`);
553
635
  }
554
636
  }
555
637
  components.push(`${quote(name)}: ${identifier},`);