@effected/engine 0.12.0 → 0.14.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.
@@ -0,0 +1,507 @@
1
+ import { ConfigLoader } from "./ConfigLoader.js";
2
+ import { decodeJson, flattenSchemaError } from "./internal/decode.js";
3
+ import { CatalogReport, CatalogSliceReport, DocumentDiff, MergedCatalogReport, RunReport, SchemaReport } from "./RunReport.js";
4
+ import { Effect, FileSystem, Option, Order, Path, Predicate, Result, Schema } from "effect";
5
+ import { AtomicFile } from "@effected/files";
6
+ import { CanonicalJson, CatalogEntry, DriftPolicy, SchemaFileWriteError, SchemaPipeline, SchemaVersioning } from "@effected/schemastore";
7
+ import { LineDiff } from "@effected/source";
8
+
9
+ // src/schema/Runner.ts
10
+ /**
11
+ * Indicates that one or more schemas advertise a version label (via
12
+ * `ResolvedSchema.frozen`) whose file is missing on disk. Raised by
13
+ * {@link Runner.run} before anything is generated — a build must never
14
+ * publish a catalog pointing a frozen label at a 404. Every miss is
15
+ * collected and reported at once, not just the first.
16
+ *
17
+ * @public
18
+ */
19
+ var FrozenVersionMissingError = class extends Schema.TaggedError()("FrozenVersionMissingError", {
20
+ /** One entry per schema/version whose frozen file is missing or not a file. */
21
+ missing: Schema.Array(Schema.Struct({
22
+ /** The schema's key in the config. */
23
+ name: Schema.String,
24
+ /** The missing frozen version label. */
25
+ version: Schema.String,
26
+ /** The path that does not exist. */
27
+ path: Schema.String
28
+ })) }) {
29
+ get message() {
30
+ const lines = this.missing.map((entry) => ` schema "${entry.name}" version ${entry.version}: ${entry.path}`);
31
+ return `${this.missing.length} frozen version(s) advertised but not on disk; nothing was written.\n${lines.join("\n")}`;
32
+ }
33
+ };
34
+ /**
35
+ * Indicates that one or more frozen files exist but do not declare the
36
+ * `$id` their `ResolvedSchema.frozen` entry derives — the file is
37
+ * absent an `$id`, declares a different one, or does not parse. Raised by
38
+ * {@link Runner.run} before anything is generated: a frozen document is
39
+ * the one file the derivation does not own, so it is the one place `$id`
40
+ * and the advertised URL can still disagree (a `baseUrl` change leaves
41
+ * every frozen file carrying the old host). Every mismatch is collected.
42
+ *
43
+ * @public
44
+ */
45
+ var FrozenVersionIdMismatchError = class extends Schema.TaggedError()("FrozenVersionIdMismatchError", {
46
+ /** One entry per frozen file whose `$id` is not the derived one. */
47
+ mismatched: Schema.Array(Schema.Struct({
48
+ /** The schema's key in the config. */
49
+ name: Schema.String,
50
+ /** The frozen version label. */
51
+ version: Schema.String,
52
+ /** The frozen file. */
53
+ path: Schema.String,
54
+ /** The `$id` the config derives for this label. */
55
+ expected: Schema.String,
56
+ /** The `$id` the file declares; absent when it declares none or does not parse. */
57
+ actual: Schema.optionalKey(Schema.String),
58
+ /** Why the file fails: a different `$id`, no `$id` at all, or text that is not JSON. */
59
+ reason: Schema.Literals([
60
+ "mismatch",
61
+ "absent",
62
+ "unparseable"
63
+ ])
64
+ })) }) {
65
+ get message() {
66
+ const lines = this.mismatched.map((entry) => {
67
+ const detail = entry.reason === "mismatch" ? `declares $id ${entry.actual}, expected ${entry.expected}` : entry.reason === "absent" ? `declares no $id, expected ${entry.expected}` : `does not parse as JSON, expected $id ${entry.expected}`;
68
+ return ` schema "${entry.name}" version ${entry.version}: ${entry.path} ${detail}`;
69
+ });
70
+ return `${this.mismatched.length} frozen version(s) on disk do not carry their derived $id; nothing was written.\n${lines.join("\n")}`;
71
+ }
72
+ };
73
+ /**
74
+ * `catalogDir` exists but cannot be listed — it is a file, or the listing
75
+ * failed (a permission failure, say). Every `*.json` file in `catalogDir` is
76
+ * a catalog slice, so without the listing there is no merged catalog to
77
+ * compute. Raised by {@link Runner.run} before anything is generated or
78
+ * written; a config problem, not a run outcome.
79
+ *
80
+ * @public
81
+ */
82
+ var CatalogDirError = class extends Schema.TaggedError()("CatalogDirError", {
83
+ /** The `catalogDir` that could not be listed. */
84
+ path: Schema.String,
85
+ /** Why, in words: `not a directory` for a file, `permission denied`, or the platform's own description. */
86
+ reason: Schema.String
87
+ }) {
88
+ get message() {
89
+ return `catalogDir ${this.path} cannot be listed (${this.reason}); it must be a directory holding only catalog slices, or not exist yet. Nothing was written.`;
90
+ }
91
+ };
92
+ const listingFailureReason = (error) => {
93
+ switch (error.reason._tag) {
94
+ case "BadResource": return "not a directory";
95
+ case "PermissionDenied": return "permission denied";
96
+ default: return error.reason.description ?? error.reason._tag;
97
+ }
98
+ };
99
+ const pipelineOptions = { contractChanges: "allow" };
100
+ const orNone = (read) => read.pipe(Effect.map(Option.some), Effect.catchIf((error) => error.reason._tag === "NotFound", () => Effect.succeed(Option.none())));
101
+ const pendingOutcome = (wouldWrite, refused) => !wouldWrite ? "unchanged" : refused ? "held" : "would-write";
102
+ const declaredId = (text) => {
103
+ const parsed = decodeJson(text);
104
+ if (Result.isFailure(parsed)) return { reason: "unparseable" };
105
+ const $id = Predicate.isObject(parsed.success) ? parsed.success.$id : void 0;
106
+ return typeof $id === "string" ? {
107
+ reason: "ok",
108
+ $id
109
+ } : { reason: "absent" };
110
+ };
111
+ const parsesEqual = (existing, text) => {
112
+ const before = decodeJson(existing);
113
+ const after = decodeJson(text);
114
+ return Result.isSuccess(before) && Result.isSuccess(after) && CanonicalJson.equals(before.success, after.success);
115
+ };
116
+ const isRecord = (value) => Predicate.isObject(value) && !Array.isArray(value);
117
+ const alignKeys = (value, order) => {
118
+ if (Array.isArray(value)) return value.map((element, index) => alignKeys(element, Array.isArray(order) ? order[index] : void 0));
119
+ if (!isRecord(value)) return value;
120
+ const reference = isRecord(order) ? order : {};
121
+ const shared = Object.keys(reference).filter((key) => Object.hasOwn(value, key));
122
+ const rest = Object.keys(value).filter((key) => !Object.hasOwn(reference, key));
123
+ return Object.fromEntries([...shared, ...rest].map((key) => [key, alignKeys(value[key], Object.hasOwn(reference, key) ? reference[key] : void 0)]));
124
+ };
125
+ const canonicalForm = (existing, generated) => {
126
+ const text = existing.charCodeAt(0) === 65279 ? existing.slice(1) : existing;
127
+ const parsed = decodeJson(text);
128
+ if (Result.isFailure(parsed) || Result.isFailure(CanonicalJson.serializeResult(parsed.success))) return text;
129
+ const order = decodeJson(generated);
130
+ const aligned = Result.isSuccess(order) ? alignKeys(parsed.success, order.success) : parsed.success;
131
+ return Result.getOrElse(CanonicalJson.serializeResult(aligned), () => text);
132
+ };
133
+ const documentDiff = (path, generated, existing) => {
134
+ const hunks = LineDiff.compute(existing === void 0 ? "" : canonicalForm(existing, generated), generated);
135
+ return DocumentDiff.make({
136
+ hunks,
137
+ unified: LineDiff.unified(hunks, {
138
+ before: existing === void 0 ? "/dev/null" : path,
139
+ after: path
140
+ })
141
+ });
142
+ };
143
+ const CatalogEntries = Schema.Array(CatalogEntry);
144
+ const decodeSlice = (text) => {
145
+ const parsed = decodeJson(text);
146
+ if (Result.isFailure(parsed)) return Result.fail("not JSON");
147
+ return Schema.decodeUnknownResult(CatalogEntries)(parsed.success, {
148
+ onExcessProperty: "error",
149
+ errors: "all"
150
+ }).pipe(Result.mapError(flattenSchemaError));
151
+ };
152
+ const bySlice = Order.mapInput(Order.String, (source) => source.slice);
153
+ const byUrl = Order.mapInput(Order.String, (entry) => entry.url);
154
+ const byClaimedValue = Order.mapInput(Order.String, ([value]) => value);
155
+ const syncFile = Effect.fn("Runner.syncFile")(function* (file, text, writing, refused) {
156
+ const fs = yield* FileSystem.FileSystem;
157
+ const path = yield* Path.Path;
158
+ const existing = yield* orNone(fs.readFileString(file));
159
+ const same = Option.isSome(existing) && parsesEqual(existing.value, text);
160
+ const outcome = writing && !same ? "written" : pendingOutcome(!same, refused);
161
+ if (outcome === "written") yield* fs.makeDirectory(path.dirname(file), { recursive: true }).pipe(Effect.andThen(AtomicFile.write(file, text)), Effect.mapError((cause) => SchemaFileWriteError.make({
162
+ path: file,
163
+ cause
164
+ })));
165
+ return outcome;
166
+ });
167
+ const encodeEntries = (entries) => CanonicalJson.serialize(entries.map((entry) => Schema.encodeSync(CatalogEntry)(entry)));
168
+ const syncCatalog = Effect.fn("Runner.syncCatalog")(function* (config, names, writing, refused) {
169
+ const fs = yield* FileSystem.FileSystem;
170
+ const path = yield* Path.Path;
171
+ const slicePath = path.normalize(ConfigLoader.slicePath(config, path));
172
+ const mergedPath = path.normalize(ConfigLoader.mergedCatalogPath(config, path));
173
+ const own = config.schemas.flatMap((schema) => schema.catalog !== void 0 ? [schema.catalog] : []);
174
+ const exact = `${config.name}.json`;
175
+ const folded = names.filter((name) => name.toLowerCase() === exact.toLowerCase());
176
+ const ownOnDisk = names.includes(exact) ? exact : own.length > 0 && folded.length === 1 && Result.isSuccess(yield* Effect.result(fs.stat(slicePath))) ? folded[0] : void 0;
177
+ const caseFolded = own.length > 0 && ownOnDisk !== void 0 && ownOnDisk !== exact ? { caseFoldedMatch: path.normalize(path.join(config.catalogDir, ownOnDisk)) } : {};
178
+ let slice;
179
+ if (own.length === 0) {
180
+ if (Option.isSome(yield* orNone(fs.stat(slicePath)))) slice = CatalogSliceReport.make({
181
+ path: slicePath,
182
+ entries: 0,
183
+ outcome: "orphaned"
184
+ });
185
+ } else {
186
+ const outcome = yield* syncFile(slicePath, yield* encodeEntries(own), writing, refused);
187
+ slice = CatalogSliceReport.make({
188
+ path: slicePath,
189
+ entries: own.length,
190
+ outcome,
191
+ ...caseFolded
192
+ });
193
+ }
194
+ const sources = [];
195
+ const invalid = [];
196
+ for (const name of [...names].sort(Order.String)) {
197
+ const file = path.normalize(path.join(config.catalogDir, name));
198
+ if (!name.endsWith(".json") || name === ownOnDisk && own.length > 0) continue;
199
+ const info = yield* Effect.result(fs.stat(file));
200
+ if (Result.isFailure(info)) {
201
+ if (info.failure.reason._tag !== "NotFound") invalid.push({
202
+ path: file,
203
+ reason: `unreadable: ${info.failure.reason._tag}`
204
+ });
205
+ else if (Result.isSuccess(yield* Effect.result(fs.readLink(file)))) invalid.push({
206
+ path: file,
207
+ reason: "unreadable: a dangling symlink"
208
+ });
209
+ continue;
210
+ }
211
+ if (info.success.type !== "File") continue;
212
+ const text = yield* Effect.result(fs.readFileString(file));
213
+ if (Result.isFailure(text)) {
214
+ if (text.failure.reason._tag !== "NotFound") invalid.push({
215
+ path: file,
216
+ reason: `unreadable: ${text.failure.reason._tag}`
217
+ });
218
+ continue;
219
+ }
220
+ const decoded = decodeSlice(text.success);
221
+ if (Result.isFailure(decoded)) invalid.push({
222
+ path: file,
223
+ reason: decoded.failure
224
+ });
225
+ else sources.push({
226
+ slice: file,
227
+ entries: decoded.success
228
+ });
229
+ }
230
+ if (own.length > 0) sources.push({
231
+ slice: slicePath,
232
+ entries: own
233
+ });
234
+ sources.sort(bySlice);
235
+ let merged;
236
+ if (sources.length === 0 && invalid.length === 0) {
237
+ if (Option.isSome(yield* orNone(fs.stat(mergedPath)))) merged = MergedCatalogReport.make({
238
+ path: mergedPath,
239
+ entries: 0,
240
+ outcome: "orphaned",
241
+ slices: [],
242
+ conflicts: [],
243
+ invalid: []
244
+ });
245
+ } else {
246
+ const claims = /* @__PURE__ */ new Map();
247
+ const nameClaims = /* @__PURE__ */ new Map();
248
+ const union = [];
249
+ for (const source of sources) for (const entry of source.entries) {
250
+ const claimants = claims.get(entry.url);
251
+ if (claimants === void 0) {
252
+ claims.set(entry.url, [source.slice]);
253
+ union.push(entry);
254
+ const nameClaimants = nameClaims.get(entry.name);
255
+ if (nameClaimants === void 0) nameClaims.set(entry.name, [source.slice]);
256
+ else nameClaimants.push(source.slice);
257
+ } else claimants.push(source.slice);
258
+ }
259
+ const repeated = (claimed) => [...claimed].filter(([, slices]) => slices.length > 1).sort(byClaimedValue).map(([value, slices]) => ({
260
+ value,
261
+ slices: [...slices].sort(Order.String)
262
+ }));
263
+ const conflicts = [...repeated(claims).map(({ value, slices }) => ({
264
+ kind: "url",
265
+ url: value,
266
+ slices
267
+ })), ...repeated(nameClaims).map(({ value, slices }) => ({
268
+ kind: "name",
269
+ name: value,
270
+ slices
271
+ }))];
272
+ union.sort(byUrl);
273
+ const outcome = conflicts.length > 0 || invalid.length > 0 ? "blocked" : yield* syncFile(mergedPath, yield* encodeEntries(union), writing, refused);
274
+ merged = MergedCatalogReport.make({
275
+ path: mergedPath,
276
+ entries: union.length,
277
+ outcome,
278
+ slices: sources.map((source) => source.slice),
279
+ conflicts,
280
+ invalid
281
+ });
282
+ }
283
+ return slice === void 0 && merged === void 0 ? void 0 : CatalogReport.make({
284
+ ...slice !== void 0 ? { slice } : {},
285
+ ...merged !== void 0 ? { merged } : {}
286
+ });
287
+ });
288
+ /**
289
+ * The shared `build` / `check` walk: verify every advertised frozen version
290
+ * exists, classify every current target through `DriftPolicy` over
291
+ * `SchemaPipeline.checkOne`, then write through `SchemaPipeline.run` only when
292
+ * nothing is refused.
293
+ *
294
+ * @remarks
295
+ * **The frozen check runs first, before anything is generated.** Every
296
+ * schema's `ResolvedSchema.frozen` versions are walked, and every miss
297
+ * is reported at once: a build fails typed with
298
+ * {@link FrozenVersionMissingError} listing every label with no file on disk
299
+ * — nothing is written — a catalog that points a label at a 404 is a worse
300
+ * failure than an early refusal. A file that is there is read, and must
301
+ * declare the `$id` its entry derives, else the run fails typed with
302
+ * {@link FrozenVersionIdMismatchError} (a different `$id`, none, or text
303
+ * that is not JSON) — a `baseUrl` change is a re-publish event for every
304
+ * frozen label, not a silent re-advertisement.
305
+ *
306
+ * **Drift is classified per schema, under that schema's own
307
+ * `ResolvedSchema.drift` tolerance — unless `options.policy` is set,
308
+ * in which case it overrides every schema's own for this run** (the `--drift`
309
+ * / `--force` flags). A build writes NOTHING when any schema fails its gate,
310
+ * or when any schema drifts under `onDrift: "error"` — a partial write would
311
+ * leave a repository half-bumped. Every otherwise-writable schema then
312
+ * reports `held`, so a reader sees why a clean schema was not written — in
313
+ * both modes, since `check` reports what `build` would do under the same
314
+ * flags. Both modes share one `SchemaFile`; the single `writing` predicate
315
+ * (`mode === "build" && !refused`) gates every write, schemas and the
316
+ * catalog files alike. Under `onDrift: "warn"` drifting schemas are written
317
+ * and keep their `"drift"` verdict for the renderer to shout about.
318
+ *
319
+ * **A document whose `change` is not `none` carries a {@link DocumentDiff}**,
320
+ * from the text the check read to the text a build writes, both in the kit's
321
+ * canonical layout. Its line numbers therefore refer to the canonical form,
322
+ * and a document that does not parse diffs as its raw text.
323
+ *
324
+ * **Every catalog entry the config declares lands in ONE slice file**,
325
+ * `<catalogDir>/<name>.json` — a bare SchemaStore catalog entry array,
326
+ * never one file per schema — serialized canonically, compared by parsed
327
+ * content against the file on disk, and rewritten wholesale only when
328
+ * different and only when the run is writing. When no schema declares one,
329
+ * no slice is written; a slice still on disk is reported `orphaned` (stale
330
+ * under `check`) and left in place — and still merged, so the merged
331
+ * catalog keeps advertising its entries until it is deleted by hand.
332
+ *
333
+ * **The merged catalog is the union of every slice in `catalogDir`**,
334
+ * written to `catalog.json` in `catalogDir`'s parent, sorted by entry
335
+ * `url`, and compared and written by the same rule. When the running
336
+ * config declares entries, its slice is replaced by the entries it
337
+ * computes now, so `check` compares against what a build would produce;
338
+ * when it declares none, its on-disk slice is merged as-is. The merge is
339
+ * therefore a pure function of disk plus the non-empty fresh entries, and
340
+ * whichever config sharing the directory builds last writes the identical
341
+ * file. A URL advertised by two
342
+ * slices, or a slice that is not JSON or not a catalog entry array, is
343
+ * reported and blocks the merged write (`blocked`) rather than being
344
+ * silently merged or dropped. With no slice file left in `catalogDir` and
345
+ * no entry declared, a merged file still on disk is `orphaned`.
346
+ * `catalogDir` is the one directory the Runner lists: every `*.json` file
347
+ * in it is a slice by construction. It is listed before anything is
348
+ * generated; one that is a file or cannot be listed fails typed with
349
+ * {@link CatalogDirError}, nothing written. The running config's own slice
350
+ * on disk is an exact `<name>.json` match, or — only when there is none,
351
+ * exactly one listed file case-folds to it, and the volume itself resolves
352
+ * the exact path to that file (a `stat` that succeeds, i.e. a
353
+ * case-insensitive volume) — that one file, reported as
354
+ * `CatalogSliceReport.caseFoldedMatch`. On a case-sensitive volume the
355
+ * variant is another slice: a case-only rename leftover blocks the merge on
356
+ * the first build like any other rename leftover.
357
+ *
358
+ * **A moved path leaves an orphan the derivation cannot see**: an
359
+ * `appendVersion` flip or a `layout` change renames a document's derived
360
+ * path, and the previously written file stays on disk under the old name —
361
+ * for a `published` label, its advertised URL keeps serving a stale
362
+ * document with no report. The derivation has exactly four shapes for a
363
+ * name and label (`<name>.json`, `<name>-<v>.json`, `<v>/<name>.json`,
364
+ * `<v>/<name>-<v>.json`), so both modes probe the sibling shapes of every
365
+ * label the config still knows and report each one that exists as a FILE
366
+ * and that no target, frozen version, catalog slice, or merged catalog claims in
367
+ * `RunReport.orphaned`. Nothing else on disk is looked at: an
368
+ * `outputDir` shared with another config, a deploy folder, or the
369
+ * repository root holds documents this config cannot tell from its own
370
+ * leftovers, so they are never reported. A `name` change is therefore not
371
+ * caught either — the old name is unknowable. Orphans are reported, never
372
+ * deleted: the tool may not have written them.
373
+ *
374
+ * @public
375
+ */
376
+ var Runner = class {
377
+ constructor() {}
378
+ static run = Effect.fn("Runner.run")(function* (config, options) {
379
+ const fs = yield* FileSystem.FileSystem;
380
+ const path = yield* Path.Path;
381
+ const missing = [];
382
+ const mismatched = [];
383
+ for (const schema of config.schemas) for (const frozen of schema.frozen) {
384
+ const info = yield* orNone(fs.stat(frozen.path));
385
+ if (Option.isNone(info) || info.value.type !== "File") {
386
+ missing.push({
387
+ name: schema.name,
388
+ version: frozen.version,
389
+ path: frozen.path
390
+ });
391
+ continue;
392
+ }
393
+ const text = yield* fs.readFileString(frozen.path);
394
+ const declared = declaredId(text);
395
+ if (declared.reason !== "ok") mismatched.push({
396
+ name: schema.name,
397
+ version: frozen.version,
398
+ path: frozen.path,
399
+ expected: frozen.$id,
400
+ reason: declared.reason
401
+ });
402
+ else if (declared.$id !== frozen.$id) mismatched.push({
403
+ name: schema.name,
404
+ version: frozen.version,
405
+ path: frozen.path,
406
+ expected: frozen.$id,
407
+ actual: declared.$id,
408
+ reason: "mismatch"
409
+ });
410
+ }
411
+ if (missing.length > 0) return yield* Effect.fail(new FrozenVersionMissingError({ missing }));
412
+ if (mismatched.length > 0) return yield* Effect.fail(new FrozenVersionIdMismatchError({ mismatched }));
413
+ const listing = yield* Effect.result(fs.readDirectory(config.catalogDir));
414
+ if (Result.isFailure(listing) && listing.failure.reason._tag !== "NotFound") return yield* Effect.fail(new CatalogDirError({
415
+ path: config.catalogDir,
416
+ reason: listingFailureReason(listing.failure)
417
+ }));
418
+ const catalogNames = Result.isSuccess(listing) ? listing.success : [];
419
+ const targets = config.schemas.map((schema) => schema.target);
420
+ const classified = [];
421
+ for (const schema of config.schemas) {
422
+ const target = schema.target;
423
+ const check = yield* SchemaPipeline.checkOne(target, pipelineOptions);
424
+ const policy = options.policy ?? schema.drift;
425
+ const verdict = DriftPolicy.classify({
426
+ published: target.published,
427
+ change: check.change
428
+ }, policy);
429
+ const nextVersion = target.version !== void 0 && check.change === "contract" && SchemaVersioning.isPinned(target.version) ? SchemaVersioning.next(target.version, "contract") : void 0;
430
+ classified.push({
431
+ schema,
432
+ target,
433
+ check,
434
+ verdict,
435
+ policy,
436
+ nextVersion
437
+ });
438
+ if (options.onChecked !== void 0) yield* options.onChecked({
439
+ name: schema.name,
440
+ change: check.change,
441
+ verdict,
442
+ blocked: check.blocked
443
+ });
444
+ }
445
+ const gateFailed = classified.some((entry) => entry.check.blocked);
446
+ const drifted = classified.some((entry) => entry.verdict === "drift");
447
+ const refused = gateFailed || drifted && options.onDrift === "error";
448
+ const writing = options.mode === "build" && !refused;
449
+ const written = writing ? yield* SchemaPipeline.run(targets, pipelineOptions).pipe(Effect.catchTags({
450
+ SchemaGateError: (error) => Effect.die(error),
451
+ SchemaContractChangeError: (error) => Effect.die(error)
452
+ })) : void 0;
453
+ const schemas = classified.map(({ schema, target, check, verdict, policy, nextVersion }, i) => {
454
+ const outcome = check.blocked ? "gate-failed" : written !== void 0 ? written[i].outcome : verdict === "drift" ? "drift" : pendingOutcome(check.wouldWrite, refused);
455
+ return SchemaReport.make({
456
+ $id: target.$id,
457
+ path: target.path,
458
+ name: schema.name,
459
+ published: target.published,
460
+ change: check.change,
461
+ verdict,
462
+ policy,
463
+ outcome,
464
+ findings: check.findings,
465
+ frozen: schema.frozen.map((frozen) => frozen.version),
466
+ ...target.version !== void 0 ? { version: target.version } : {},
467
+ ...nextVersion !== void 0 ? { nextVersion } : {},
468
+ ...check.change !== "none" ? { diff: documentDiff(target.path, check.generated, check.existing) } : {}
469
+ });
470
+ });
471
+ const catalog = yield* syncCatalog(config, catalogNames, writing, refused);
472
+ const claimed = /* @__PURE__ */ new Set([path.normalize(ConfigLoader.slicePath(config, path)), path.normalize(ConfigLoader.mergedCatalogPath(config, path))]);
473
+ for (const schema of config.schemas) {
474
+ claimed.add(path.normalize(schema.target.path));
475
+ for (const frozen of schema.frozen) claimed.add(path.normalize(frozen.path));
476
+ }
477
+ const orphaned = [];
478
+ for (const schema of config.schemas) {
479
+ const labels = [schema.target.version, ...schema.frozen.map((frozen) => frozen.version)];
480
+ const shapes = [SchemaVersioning.fileName(schema.name), ...labels.flatMap((version) => version === void 0 ? [] : [
481
+ SchemaVersioning.fileName(schema.name, version, "flat"),
482
+ SchemaVersioning.fileName(schema.name, version, "versioned"),
483
+ SchemaVersioning.fileName(schema.name, version, "versioned", false)
484
+ ])];
485
+ for (const shape of shapes) {
486
+ const file = path.normalize(path.join(config.outputDir, shape));
487
+ if (claimed.has(file)) continue;
488
+ const info = yield* orNone(fs.stat(file));
489
+ if (Option.isSome(info) && info.value.type === "File") orphaned.push(file);
490
+ }
491
+ }
492
+ return RunReport.make({
493
+ mode: options.mode,
494
+ configPath: options.configPath,
495
+ onDrift: options.onDrift,
496
+ ...options.policy !== void 0 ? { policy: options.policy } : {},
497
+ schemas,
498
+ ...catalog !== void 0 ? { catalog } : {},
499
+ ...orphaned.length > 0 ? { orphaned } : {},
500
+ drifted,
501
+ gateFailed,
502
+ wrote: schemas.some((s) => s.outcome === "written") || catalog?.slice?.outcome === "written" || catalog?.merged?.outcome === "written"
503
+ });
504
+ });
505
+ };
506
+
507
+ export { CatalogDirError, FrozenVersionIdMismatchError, FrozenVersionMissingError, Runner };
@@ -0,0 +1,108 @@
1
+ import { AjvValidator } from "./AjvValidator.js";
2
+ import { ConfigLoader } from "./ConfigLoader.js";
3
+ import { Runner } from "./Runner.js";
4
+ import { RunReport } from "./RunReport.js";
5
+ import { VersionCheck, VersionMismatch, VersionMismatchError } from "./VersionCheck.js";
6
+ import { Effect, Option, Schema } from "effect";
7
+ import { SchemaFile } from "@effected/schemastore";
8
+
9
+ // src/schema/SchemaRun.ts
10
+ /**
11
+ * `--force` (shorthand for `--drift=allow`) was combined with an explicit
12
+ * `--drift` that is not `allow`. Contradictory, so refused as a usage
13
+ * error rather than silently resolving to `allow`.
14
+ *
15
+ * @public
16
+ */
17
+ var ConflictingFlagsError = class extends Schema.TaggedError()("ConflictingFlagsError", { policy: Schema.String }) {
18
+ get message() {
19
+ return `--force conflicts with --drift=${this.policy}: --force means --drift=allow`;
20
+ }
21
+ };
22
+ /**
23
+ * One run's answer: the report, the verdict the front ends map to an exit code
24
+ * or return as data, and any version mismatch the run went on past.
25
+ *
26
+ * @public
27
+ */
28
+ var SchemaRunResult = class extends Schema.Class("SchemaRunResult")({
29
+ report: RunReport,
30
+ verdict: Schema.Literals([
31
+ "ok",
32
+ "gate-failed",
33
+ "drift",
34
+ "catalog-blocked",
35
+ "stale"
36
+ ]),
37
+ versionMismatch: Schema.optionalKey(VersionMismatch)
38
+ }) {};
39
+ const verdictOf = (report) => {
40
+ if (report.gateFailed) return "gate-failed";
41
+ if (report.drifted && report.onDrift === "error") return "drift";
42
+ const merged = report.catalog?.merged;
43
+ if (merged?.outcome === "blocked") return "catalog-blocked";
44
+ return report.mode === "check" && (report.schemas.some((schema) => schema.outcome === "would-write") || [report.catalog?.slice?.outcome, merged?.outcome].some((outcome) => outcome === "would-write" || outcome === "orphaned") || (report.orphaned?.length ?? 0) > 0) ? "stale" : "ok";
45
+ };
46
+ /**
47
+ * Runs one `build` or `check`.
48
+ *
49
+ * @remarks
50
+ * Before anything loads, `force` beside a `drift` other than `allow` fails
51
+ * {@link ConflictingFlagsError}. The version check runs right after the load and
52
+ * before anything is generated. A failing gate, refused drift, blocked catalog or
53
+ * stale tree is a verdict rather than a failure, so a front end can always show
54
+ * the report; the error channel holds a run that could not start or finish.
55
+ * `check` writes nothing: {@link Runner.run}'s one `writing` predicate decides
56
+ * that for documents, and appending the step summary is left to the CLI.
57
+ *
58
+ * @public
59
+ */
60
+ var SchemaRun = class {
61
+ constructor() {}
62
+ static run = Effect.fn("SchemaRun.run")(function* (input, deps) {
63
+ if (input.force && input.drift !== void 0 && input.drift !== "allow") return yield* new ConflictingFlagsError({ policy: input.drift });
64
+ const loaded = yield* ConfigLoader.load({
65
+ cwd: deps.cwd,
66
+ ...input.config !== void 0 ? { explicit: input.config } : {},
67
+ ...deps.discoveryCeiling !== void 0 ? { ceiling: deps.discoveryCeiling } : {},
68
+ ...deps.importModule !== void 0 ? { importModule: deps.importModule } : {}
69
+ });
70
+ const mismatch = VersionCheck.inspect(loaded.config);
71
+ if (Option.isSome(mismatch) && input.versionMismatch === "fail") {
72
+ const { tool, found, message } = mismatch.value;
73
+ return yield* new VersionMismatchError({
74
+ tool,
75
+ found,
76
+ message
77
+ });
78
+ }
79
+ const { onProgress } = deps;
80
+ yield* onProgress?.({
81
+ _tag: "Started",
82
+ names: loaded.config.schemas.map((schema) => schema.name),
83
+ ...Option.isSome(mismatch) ? { versionMismatch: mismatch.value } : {}
84
+ }) ?? Effect.void;
85
+ const policy = input.force ? "allow" : input.drift;
86
+ const report = yield* Runner.run(loaded.config, {
87
+ mode: input.mode,
88
+ configPath: loaded.path,
89
+ onDrift: input.onDrift ?? loaded.config.onDrift,
90
+ ...policy !== void 0 ? { policy } : {},
91
+ ...onProgress !== void 0 ? { onChecked: (checked) => onProgress({
92
+ _tag: "SchemaChecked",
93
+ ...checked
94
+ }) } : {}
95
+ }).pipe(Effect.provide(SchemaFile.layer), Effect.provide(deps.validator ?? AjvValidator.layer));
96
+ for (const schema of report.schemas) yield* onProgress?.({
97
+ _tag: "SchemaDone",
98
+ report: schema
99
+ }) ?? Effect.void;
100
+ return SchemaRunResult.make({
101
+ report,
102
+ verdict: verdictOf(report),
103
+ ...Option.isSome(mismatch) ? { versionMismatch: mismatch.value } : {}
104
+ });
105
+ });
106
+ };
107
+
108
+ export { ConflictingFlagsError, SchemaRun, SchemaRunResult };