@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.
- package/build/ActionInit.js +461 -0
- package/build/ActionRun.js +74 -0
- package/build/ActionRunResult.js +22 -0
- package/build/BuildRun.js +162 -0
- package/build/BuildRunResult.js +26 -0
- package/build.d.ts +446 -0
- package/build.js +8 -0
- package/package.json +24 -4
- package/schema/AjvInstanceValidator.js +72 -0
- package/schema/AjvValidator.js +80 -0
- package/schema/ConfigLoader.js +273 -0
- package/schema/Report.js +203 -0
- package/schema/RunReport.js +352 -0
- package/schema/Runner.js +507 -0
- package/schema/SchemaRun.js +108 -0
- package/schema/SchemaValidate.js +324 -0
- package/schema/StepSummary.js +30 -0
- package/schema/VersionCheck.js +81 -0
- package/schema/internal/ajv.js +50 -0
- package/schema/internal/causeMessage.js +57 -0
- package/schema/internal/decode.js +23 -0
- package/schema.d.ts +1084 -0
- package/schema.js +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effected/engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "The effected tool's shared logic, consumed by its CLI and MCP front ends",
|
|
6
6
|
"keywords": [
|
|
@@ -33,11 +33,31 @@
|
|
|
33
33
|
"import": "./index.js",
|
|
34
34
|
"default": "./index.js"
|
|
35
35
|
},
|
|
36
|
-
"./
|
|
36
|
+
"./build": {
|
|
37
|
+
"types": "./build.d.ts",
|
|
38
|
+
"import": "./build.js",
|
|
39
|
+
"default": "./build.js"
|
|
40
|
+
},
|
|
41
|
+
"./package.json": "./package.json",
|
|
42
|
+
"./schema": {
|
|
43
|
+
"types": "./schema.d.ts",
|
|
44
|
+
"import": "./schema.js",
|
|
45
|
+
"default": "./schema.js"
|
|
46
|
+
}
|
|
37
47
|
},
|
|
38
48
|
"dependencies": {
|
|
39
|
-
"@effected/
|
|
40
|
-
"
|
|
49
|
+
"@effected/bundler": "0.1.0",
|
|
50
|
+
"@effected/files": "^0.2.0",
|
|
51
|
+
"@effected/jsonc": "^0.24.0",
|
|
52
|
+
"@effected/jsonl": "^0.18.0",
|
|
53
|
+
"@effected/kit": "^0.13.0",
|
|
54
|
+
"@effected/schemastore": "0.31.0",
|
|
55
|
+
"@effected/source": "^0.4.0",
|
|
56
|
+
"@effected/workspaces": "^0.44.0",
|
|
57
|
+
"ajv": "8.20.0",
|
|
58
|
+
"ajv-formats": "3.0.1",
|
|
59
|
+
"effect": "~4.1.2",
|
|
60
|
+
"jiti": "~2.7.0"
|
|
41
61
|
},
|
|
42
62
|
"engines": {
|
|
43
63
|
"node": ">=24.11.0"
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { makeAjv } from "./internal/ajv.js";
|
|
2
|
+
import { Effect, Layer } from "effect";
|
|
3
|
+
import { InstanceFinding, InstanceValidator, InstanceValidatorError } from "@effected/schemastore";
|
|
4
|
+
|
|
5
|
+
// src/schema/AjvInstanceValidator.ts
|
|
6
|
+
const findingFromAjvError = (error) => InstanceFinding.make({
|
|
7
|
+
path: error.instancePath,
|
|
8
|
+
message: error.message ?? "instance is not valid",
|
|
9
|
+
keyword: error.keyword
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* The shipped `InstanceValidator` implementation: the same ajv strict-mode
|
|
13
|
+
* setup `AjvValidator` gates documents with — the declared `KeywordFamilies`
|
|
14
|
+
* keywords and the standard `ajv-formats` vocabulary registered, through the
|
|
15
|
+
* one shared `makeAjv` — pointed at a payload instance instead of the
|
|
16
|
+
* meta-schema.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* Lives in the tool, not the library, for the same reason `AjvValidator`
|
|
20
|
+
* does: `@effected/schemastore` owns the `InstanceValidator` contract and
|
|
21
|
+
* its doubles, an application that imports it at runtime never pulls an
|
|
22
|
+
* engine, and `effected schema validate` composes this layer at its edge. It is
|
|
23
|
+
* exported for a program that validates payloads itself and wants the same
|
|
24
|
+
* verdict the command gives — a document the `check` gate admits always compiles
|
|
25
|
+
* here, so the two engines cannot drift.
|
|
26
|
+
*
|
|
27
|
+
* `validate` compiles the document and runs the instance against it. An
|
|
28
|
+
* instance the document rejects answers `InstanceFinding` values — ajv's
|
|
29
|
+
* structured `instancePath` pointer and `keyword` preserved, `allErrors`
|
|
30
|
+
* on, so one run reports every problem, not just the first. A document the
|
|
31
|
+
* engine cannot compile (a meta-schema failure, a strict-mode rejection, a
|
|
32
|
+
* declared keyword whose NAME ajv's grammar refuses) fails
|
|
33
|
+
* `InstanceValidatorError`: unlike `AjvValidator`, whose subject IS the
|
|
34
|
+
* document, this contract's subject is the instance, and a document that
|
|
35
|
+
* yields no verdict is the engine failing to run as a mechanism. The
|
|
36
|
+
* document's own gate is `SchemaValidator`'s job — `effected schema check` runs
|
|
37
|
+
* it before a document is ever published.
|
|
38
|
+
*
|
|
39
|
+
* `strict` defaults to `true` — SchemaStore's gate. Each call builds its own
|
|
40
|
+
* ajv instance, so documents sharing an `$id` never collide.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { InstanceValidator } from "@effected/schemastore";
|
|
45
|
+
* import { AjvInstanceValidator } from "@effected/engine/schema";
|
|
46
|
+
* import { Effect } from "effect";
|
|
47
|
+
*
|
|
48
|
+
* const program = Effect.gen(function* () {
|
|
49
|
+
* const validator = yield* InstanceValidator;
|
|
50
|
+
* return yield* validator.validate({ type: "object" }, { any: "payload" });
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* Effect.runPromise(Effect.provide(program, AjvInstanceValidator.layer));
|
|
54
|
+
* // => []
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
var AjvInstanceValidator = class {
|
|
60
|
+
constructor() {}
|
|
61
|
+
/** The engine, as an `InstanceValidator` layer. */
|
|
62
|
+
static layer = Layer.succeed(InstanceValidator, { validate: (document, instance, options) => Effect.try({
|
|
63
|
+
try: () => {
|
|
64
|
+
const validate = makeAjv(document, options?.strict ?? true).compile(document);
|
|
65
|
+
if (validate(instance)) return [];
|
|
66
|
+
return (validate.errors ?? []).map(findingFromAjvError);
|
|
67
|
+
},
|
|
68
|
+
catch: (cause) => InstanceValidatorError.make({ cause })
|
|
69
|
+
}) });
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export { AjvInstanceValidator };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { makeAjv } from "./internal/ajv.js";
|
|
2
|
+
import { Effect, Layer } from "effect";
|
|
3
|
+
import { SchemaValidator, SchemaValidatorError, ValidationFinding } from "@effected/schemastore";
|
|
4
|
+
|
|
5
|
+
// src/schema/AjvValidator.ts
|
|
6
|
+
const findingFromAjvError = (error) => ValidationFinding.make({
|
|
7
|
+
path: error.instancePath,
|
|
8
|
+
message: error.message ?? "schema is not valid",
|
|
9
|
+
keyword: error.keyword
|
|
10
|
+
});
|
|
11
|
+
/**
|
|
12
|
+
* The shipped `SchemaValidator` implementation: ajv strict mode over the
|
|
13
|
+
* Draft-07 meta-schema, with every declared `KeywordFamilies` keyword and
|
|
14
|
+
* the standard `ajv-formats` vocabulary registered.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* Lives in the tool, not the library, so `ajv` is a cost only the tool pays:
|
|
18
|
+
* `@effected/schemastore` owns the `SchemaValidator` contract and its doubles,
|
|
19
|
+
* an application that imports it at runtime never pulls an engine, and
|
|
20
|
+
* `SchemaRun` composes this layer at its edge. It is exported for a program that
|
|
21
|
+
* drives `SchemaPipeline` itself and wants the same verdict the command gives.
|
|
22
|
+
*
|
|
23
|
+
* `validate` checks the document against the Draft-07 meta-schema and then
|
|
24
|
+
* compiles it, reporting BOTH as `ValidationFinding` values: meta-schema
|
|
25
|
+
* failures keep ajv's structured `instancePath` and `keyword`, while a
|
|
26
|
+
* rejection ajv raises by *throwing* becomes a root-pathed finding — both a
|
|
27
|
+
* strict-mode compile failure and a declared keyword whose NAME ajv's own
|
|
28
|
+
* grammar (`/^[a-z_$][a-z0-9_$:-]*$/i`) refuses, such as an `x-ai-*` key
|
|
29
|
+
* carrying a dot or a space. The error channel stays reserved for the engine
|
|
30
|
+
* failing as a mechanism (`SchemaValidatorError`).
|
|
31
|
+
*
|
|
32
|
+
* The ajv setup is `internal/ajv.ts`'s `makeAjv`, shared with
|
|
33
|
+
* `AjvInstanceValidator` so a document this gate admits always compiles in
|
|
34
|
+
* the instance engine too — the two verdicts cannot drift.
|
|
35
|
+
*
|
|
36
|
+
* `strict` defaults to `true` — SchemaStore's gate. Each call builds its own
|
|
37
|
+
* ajv instance, so documents sharing an `$id` never collide. Registering the
|
|
38
|
+
* declared families keeps the engine's verdict consistent with
|
|
39
|
+
* `DocumentLint`'s through the same predicate, so the two cannot drift; the
|
|
40
|
+
* plugin's `formatMaximum` / `formatMinimum` limit keywords are deliberately
|
|
41
|
+
* NOT registered, because the lint answers those as unknown keywords.
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { SchemaValidator } from "@effected/schemastore";
|
|
45
|
+
* import { AjvValidator } from "@effected/engine/schema";
|
|
46
|
+
* import { Effect } from "effect";
|
|
47
|
+
*
|
|
48
|
+
* const program = Effect.gen(function* () {
|
|
49
|
+
* const validator = yield* SchemaValidator;
|
|
50
|
+
* return yield* validator.validate({ type: "object" });
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* Effect.runPromise(Effect.provide(program, AjvValidator.layer));
|
|
54
|
+
* // => []
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
var AjvValidator = class {
|
|
60
|
+
constructor() {}
|
|
61
|
+
/** The engine, as a `SchemaValidator` layer. */
|
|
62
|
+
static layer = Layer.succeed(SchemaValidator, { validate: (document, options) => Effect.try({
|
|
63
|
+
try: () => {
|
|
64
|
+
try {
|
|
65
|
+
const ajv = makeAjv(document, options?.strict ?? true);
|
|
66
|
+
if (!ajv.validateSchema(document)) return (ajv.errors ?? []).map(findingFromAjvError);
|
|
67
|
+
ajv.compile(document);
|
|
68
|
+
} catch (cause) {
|
|
69
|
+
return [ValidationFinding.make({
|
|
70
|
+
path: "",
|
|
71
|
+
message: cause instanceof Error ? cause.message : String(cause)
|
|
72
|
+
})];
|
|
73
|
+
}
|
|
74
|
+
return [];
|
|
75
|
+
},
|
|
76
|
+
catch: (cause) => SchemaValidatorError.make({ cause })
|
|
77
|
+
}) });
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export { AjvValidator };
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { causeMessage } from "./internal/causeMessage.js";
|
|
2
|
+
import { flattenSchemaError } from "./internal/decode.js";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { PathContainment } from "@effected/kit";
|
|
5
|
+
import { Effect, FileSystem, Path, Predicate, Result, Schema } from "effect";
|
|
6
|
+
import { CatalogEntry, SchemaVersioning, isSchemastoreConfig } from "@effected/schemastore";
|
|
7
|
+
import { createJiti } from "jiti";
|
|
8
|
+
|
|
9
|
+
// src/schema/ConfigLoader.ts
|
|
10
|
+
/**
|
|
11
|
+
* No `schemastore.config.*` was found — by upward discovery from the working
|
|
12
|
+
* directory, or at the explicit path handed to `--config`.
|
|
13
|
+
*
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
var ConfigNotFoundError = class extends Schema.TaggedError()("ConfigNotFoundError", {
|
|
17
|
+
/** The directories walked (discovery) or the one explicit path checked. */
|
|
18
|
+
searched: Schema.Array(Schema.String) }) {
|
|
19
|
+
get message() {
|
|
20
|
+
return `No schemastore config found. Searched for ${ConfigLoader.CONFIG_NAMES.join(", ")} in: ${this.searched.join(", ")}`;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The config file exists but could not be turned into a `SchemastoreConfig`:
|
|
25
|
+
* the module threw on import, its default export is not a `defineConfig(...)`
|
|
26
|
+
* value, a field the pipeline reads is malformed (`name` not a simple file
|
|
27
|
+
* base name, `outputDir`/`catalogDir` not a string, `onDrift` or a schema's
|
|
28
|
+
* `drift` outside its literal set, a `schemas` element, its `target`,
|
|
29
|
+
* `catalog` or a `frozen` entry not shaped — the catalog decoded as a whole
|
|
30
|
+
* `CatalogEntry`, so a non-string `fileMatch` element or `versions` value
|
|
31
|
+
* fails too — every such issue reported at once, each as
|
|
32
|
+
* `<message> at <path>`), two outputs
|
|
33
|
+
* resolve to one absolute path, a document resolves into `catalogDir`, or
|
|
34
|
+
* `catalogDir` resolves to `outputDir` or to the merged catalog's path.
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
var ConfigLoadError = class extends Schema.TaggedError()("ConfigLoadError", {
|
|
39
|
+
/** The config file that failed to load. */
|
|
40
|
+
path: Schema.String,
|
|
41
|
+
/** Why it failed; the first line is the message, any remainder is detail such as a stack. */
|
|
42
|
+
reason: Schema.String
|
|
43
|
+
}) {
|
|
44
|
+
get message() {
|
|
45
|
+
return `Failed to load schemastore config ${this.path}: ${this.reason}`;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const IN_NODE_MODULES = /[\\/]node_modules[\\/]/;
|
|
49
|
+
const jitiImport = async (path, source) => {
|
|
50
|
+
const { cache } = createRequire(path);
|
|
51
|
+
for (const key of Object.keys(cache)) if (key.endsWith(".json") && !IN_NODE_MODULES.test(key)) Reflect.deleteProperty(cache, key);
|
|
52
|
+
return createJiti(path, {
|
|
53
|
+
interopDefault: true,
|
|
54
|
+
moduleCache: false,
|
|
55
|
+
fsCache: false
|
|
56
|
+
}).evalModule(source, {
|
|
57
|
+
filename: path,
|
|
58
|
+
async: true,
|
|
59
|
+
forceTranspile: true
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
const describeCause = (cause) => {
|
|
63
|
+
return (Predicate.hasProperty(cause, "stack") && Predicate.isString(cause.stack) && cause.stack.trim() !== "" ? cause.stack : void 0) ?? causeMessage(cause) ?? String(cause);
|
|
64
|
+
};
|
|
65
|
+
const IMPORT_HINTS = [[/Cannot find (?:module|package) '@effected\/plugin['/]/, "Install it as a devDependency: pnpm add -D @effected/plugin"], [/Expected a HostedSchema/, "The config's @effected/schemastore may be older than the tool's. Align @effected/schemastore and @effected/plugin on one version, as catalog:effected does"]];
|
|
66
|
+
const describeImportFailure = (cause) => {
|
|
67
|
+
const text = describeCause(cause);
|
|
68
|
+
const hint = IMPORT_HINTS.find(([pattern]) => pattern.test(text))?.[1];
|
|
69
|
+
if (hint === void 0) return text;
|
|
70
|
+
const [first, ...rest] = text.split("\n");
|
|
71
|
+
return [`${first}. ${hint}`, ...rest].join("\n");
|
|
72
|
+
};
|
|
73
|
+
const ForgedConfigShape = Schema.Struct({
|
|
74
|
+
name: Schema.String.check(Schema.makeFilter((name) => SchemaVersioning.isSimpleName(name) || "not a simple file base name")),
|
|
75
|
+
outputDir: Schema.String,
|
|
76
|
+
onDrift: Schema.Literals(["error", "warn"]),
|
|
77
|
+
catalogDir: Schema.String,
|
|
78
|
+
schemas: Schema.Array(Schema.Struct({
|
|
79
|
+
name: Schema.String,
|
|
80
|
+
target: Schema.Struct({
|
|
81
|
+
schema: Schema.declare(Schema.isSchema, { expected: "an Effect Schema" }),
|
|
82
|
+
$id: Schema.String,
|
|
83
|
+
path: Schema.String,
|
|
84
|
+
published: Schema.Boolean
|
|
85
|
+
}),
|
|
86
|
+
frozen: Schema.Array(Schema.Struct({
|
|
87
|
+
version: Schema.String,
|
|
88
|
+
path: Schema.String,
|
|
89
|
+
$id: Schema.String,
|
|
90
|
+
url: Schema.String
|
|
91
|
+
})),
|
|
92
|
+
drift: Schema.Literals([
|
|
93
|
+
"strict",
|
|
94
|
+
"semantic",
|
|
95
|
+
"allow"
|
|
96
|
+
]),
|
|
97
|
+
catalog: Schema.optionalKey(Schema.Struct(CatalogEntry.fields))
|
|
98
|
+
}))
|
|
99
|
+
});
|
|
100
|
+
const validateForgedShape = Schema.decodeUnknownResult(ForgedConfigShape);
|
|
101
|
+
const describeMalformed = (config) => {
|
|
102
|
+
const checked = validateForgedShape(config, { errors: "all" });
|
|
103
|
+
return Result.isFailure(checked) ? flattenSchemaError(checked.failure) : void 0;
|
|
104
|
+
};
|
|
105
|
+
const describeClashingPath = (config, path) => {
|
|
106
|
+
const documents = config.schemas.flatMap((schema) => [schema.target.path, ...schema.frozen.map((f) => f.path)]);
|
|
107
|
+
const catalogDir = path.normalize(config.catalogDir);
|
|
108
|
+
if (catalogDir === path.normalize(ConfigLoader.mergedCatalogPath(config, path))) return `catalogDir "${config.catalogDir}" must not be the merged catalog's path (catalog.json in its parent)`;
|
|
109
|
+
if (catalogDir === path.normalize(config.outputDir)) return `catalogDir "${config.catalogDir}" must not be outputDir: every *.json file in it is read as a catalog slice`;
|
|
110
|
+
for (const document of documents) if (path.normalize(path.dirname(document)) === catalogDir) return `output path "${document}" sits in catalogDir "${config.catalogDir}", which holds only catalog slices`;
|
|
111
|
+
const seen = /* @__PURE__ */ new Set();
|
|
112
|
+
for (const p of [
|
|
113
|
+
...documents,
|
|
114
|
+
ConfigLoader.slicePath(config, path),
|
|
115
|
+
ConfigLoader.mergedCatalogPath(config, path)
|
|
116
|
+
]) {
|
|
117
|
+
const normalized = path.normalize(p);
|
|
118
|
+
if (seen.has(normalized)) return `output path "${p}" is declared twice after resolution`;
|
|
119
|
+
seen.add(normalized);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
const VERSION_STAMP = Symbol.for("@effected/schemastore/version");
|
|
123
|
+
/**
|
|
124
|
+
* Finds and loads a `schemastore.config.*` file.
|
|
125
|
+
*
|
|
126
|
+
* @remarks
|
|
127
|
+
* `discover` walks upward from a start directory checking
|
|
128
|
+
* {@link ConfigLoader.CONFIG_NAMES} in order at each level, so the nearest
|
|
129
|
+
* directory wins and, within one directory, `.ts` beats `.mjs`. `load` either
|
|
130
|
+
* takes the explicit path or discovers one, imports it, checks the default
|
|
131
|
+
* export is a `defineConfig(...)` value, and resolves relative paths against
|
|
132
|
+
* the config's directory.
|
|
133
|
+
*
|
|
134
|
+
* Each `load` evaluates the config, and the TypeScript and `.json` it imports,
|
|
135
|
+
* afresh. A `type: module` or CommonJS package keeps one instance across reloads.
|
|
136
|
+
* An imported `.mjs`, `.cjs` or `.js` under `type: module` stays cached by Node's
|
|
137
|
+
* ESM loader, and a sibling workspace package resolved to built `.js` under
|
|
138
|
+
* `type: module` is the same case: an edit to any of these is not seen until the
|
|
139
|
+
* process restarts, so restart a long-lived server to pick one up. An untyped
|
|
140
|
+
* package of ESM `.js` gets a new instance on each reload.
|
|
141
|
+
*
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
144
|
+
var ConfigLoader = class ConfigLoader {
|
|
145
|
+
constructor() {}
|
|
146
|
+
/** The file names discovery looks for, in preference order. */
|
|
147
|
+
static CONFIG_NAMES = [
|
|
148
|
+
"schemastore.config.ts",
|
|
149
|
+
"schemastore.config.mts",
|
|
150
|
+
"schemastore.config.js",
|
|
151
|
+
"schemastore.config.mjs"
|
|
152
|
+
];
|
|
153
|
+
/**
|
|
154
|
+
* Walk upward from `start` until a config file is found; fails with the
|
|
155
|
+
* list of directories searched when none is.
|
|
156
|
+
*
|
|
157
|
+
* @remarks
|
|
158
|
+
* With a `ceiling`, the walk checks that directory and stops, comparing real
|
|
159
|
+
* paths so a link cannot walk past it. A server answering an agent passes one:
|
|
160
|
+
* a config is code, and on a checkout under a shared directory such as `/tmp`
|
|
161
|
+
* the one found above it is someone else's. A `start` outside the ceiling
|
|
162
|
+
* searches nothing rather than walking unbounded. Omitted, the walk runs to the
|
|
163
|
+
* filesystem root, which is right when a person ran the command.
|
|
164
|
+
*/
|
|
165
|
+
static discover = Effect.fn("ConfigLoader.discover")(function* (start, ceiling) {
|
|
166
|
+
const fs = yield* FileSystem.FileSystem;
|
|
167
|
+
const path = yield* Path.Path;
|
|
168
|
+
const real = (p) => fs.realPath(p).pipe(Effect.orElseSucceed(() => p));
|
|
169
|
+
const searched = [];
|
|
170
|
+
let dir = path.resolve(start);
|
|
171
|
+
if (ceiling !== void 0 && !(yield* PathContainment.isWithin(ceiling, dir))) return yield* Effect.fail(new ConfigNotFoundError({ searched }));
|
|
172
|
+
const stop = ceiling === void 0 ? void 0 : yield* real(path.resolve(ceiling));
|
|
173
|
+
for (;;) {
|
|
174
|
+
searched.push(dir);
|
|
175
|
+
for (const name of ConfigLoader.CONFIG_NAMES) {
|
|
176
|
+
const candidate = path.join(dir, name);
|
|
177
|
+
if (yield* fs.exists(candidate).pipe(Effect.orElseSucceed(() => false))) return candidate;
|
|
178
|
+
}
|
|
179
|
+
const parent = path.dirname(dir);
|
|
180
|
+
if (parent === dir || stop !== void 0 && (yield* real(dir)) === stop) return yield* Effect.fail(new ConfigNotFoundError({ searched }));
|
|
181
|
+
dir = parent;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
/** This config's own catalog slice: `<catalogDir>/<name>.json`. */
|
|
185
|
+
static slicePath(config, path) {
|
|
186
|
+
return path.join(config.catalogDir, `${config.name}.json`);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* The merged catalog every config sharing `catalogDir` maintains:
|
|
190
|
+
* `catalog.json` in `catalogDir`'s parent (`<outputDir>/catalog.json` under
|
|
191
|
+
* the default `catalogDir`).
|
|
192
|
+
*/
|
|
193
|
+
static mergedCatalogPath(config, path) {
|
|
194
|
+
return path.join(path.dirname(config.catalogDir), "catalog.json");
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Resolve every relative `path` in the config (`outputDir`, `catalogDir`,
|
|
198
|
+
* each schema's current target, and every frozen predecessor) against
|
|
199
|
+
* `directory`; absolute paths are left alone. `defineConfig` already
|
|
200
|
+
* prefixes `outputDir` onto every target/frozen `path`, so resolving them
|
|
201
|
+
* against the config directory equals resolving against the resolved
|
|
202
|
+
* `outputDir`. The result keeps the `defineConfig` brand and the version
|
|
203
|
+
* stamp of the copy that produced it.
|
|
204
|
+
*/
|
|
205
|
+
static resolvePaths = Effect.fn("ConfigLoader.resolvePaths")(function* (config, directory) {
|
|
206
|
+
const path = yield* Path.Path;
|
|
207
|
+
const absolute = (p) => path.isAbsolute(p) ? p : path.resolve(directory, p);
|
|
208
|
+
const resolved = {
|
|
209
|
+
...config,
|
|
210
|
+
outputDir: absolute(config.outputDir),
|
|
211
|
+
catalogDir: absolute(config.catalogDir),
|
|
212
|
+
schemas: config.schemas.map((schema) => ({
|
|
213
|
+
...schema,
|
|
214
|
+
target: {
|
|
215
|
+
...schema.target,
|
|
216
|
+
path: absolute(schema.target.path)
|
|
217
|
+
},
|
|
218
|
+
frozen: schema.frozen.map((f) => ({
|
|
219
|
+
...f,
|
|
220
|
+
path: absolute(f.path)
|
|
221
|
+
}))
|
|
222
|
+
}))
|
|
223
|
+
};
|
|
224
|
+
const stamp = Object.getOwnPropertyDescriptor(config, VERSION_STAMP);
|
|
225
|
+
return stamp === void 0 ? resolved : Object.defineProperty(resolved, VERSION_STAMP, stamp);
|
|
226
|
+
});
|
|
227
|
+
/** Locate, import and validate the config; see {@link ConfigLoadOptions}. */
|
|
228
|
+
static load = Effect.fn("ConfigLoader.load")(function* (options) {
|
|
229
|
+
const fs = yield* FileSystem.FileSystem;
|
|
230
|
+
const path = yield* Path.Path;
|
|
231
|
+
const { importModule } = options;
|
|
232
|
+
let configPath;
|
|
233
|
+
if (options.explicit !== void 0) {
|
|
234
|
+
configPath = path.resolve(options.cwd, options.explicit);
|
|
235
|
+
if (!(yield* fs.exists(configPath).pipe(Effect.orElseSucceed(() => false)))) return yield* Effect.fail(new ConfigNotFoundError({ searched: [configPath] }));
|
|
236
|
+
} else configPath = yield* ConfigLoader.discover(options.cwd, options.ceiling);
|
|
237
|
+
const failed = (cause) => new ConfigLoadError({
|
|
238
|
+
path: configPath,
|
|
239
|
+
reason: describeImportFailure(cause)
|
|
240
|
+
});
|
|
241
|
+
const module = importModule !== void 0 ? yield* Effect.tryPromise({
|
|
242
|
+
try: () => importModule(configPath),
|
|
243
|
+
catch: failed
|
|
244
|
+
}) : yield* fs.readFileString(configPath).pipe(Effect.mapError(failed), Effect.flatMap((source) => Effect.tryPromise({
|
|
245
|
+
try: () => jitiImport(configPath, source),
|
|
246
|
+
catch: failed
|
|
247
|
+
})));
|
|
248
|
+
const exported = module?.default ?? module;
|
|
249
|
+
if (!isSchemastoreConfig(exported)) return yield* Effect.fail(new ConfigLoadError({
|
|
250
|
+
path: configPath,
|
|
251
|
+
reason: "default export is not a defineSchemastore(...) value from @effected/plugin or a defineConfig(...) value from @effected/schemastore"
|
|
252
|
+
}));
|
|
253
|
+
const malformed = describeMalformed(exported);
|
|
254
|
+
if (malformed !== void 0) return yield* Effect.fail(new ConfigLoadError({
|
|
255
|
+
path: configPath,
|
|
256
|
+
reason: malformed
|
|
257
|
+
}));
|
|
258
|
+
const directory = path.dirname(configPath);
|
|
259
|
+
const config = yield* ConfigLoader.resolvePaths(exported, directory);
|
|
260
|
+
const duplicate = describeClashingPath(config, path);
|
|
261
|
+
if (duplicate !== void 0) return yield* Effect.fail(new ConfigLoadError({
|
|
262
|
+
path: configPath,
|
|
263
|
+
reason: duplicate
|
|
264
|
+
}));
|
|
265
|
+
return {
|
|
266
|
+
path: configPath,
|
|
267
|
+
directory,
|
|
268
|
+
config
|
|
269
|
+
};
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
export { ConfigLoadError, ConfigLoader, ConfigNotFoundError };
|
package/schema/Report.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { RunReport } from "./RunReport.js";
|
|
2
|
+
import { VersionMismatch } from "./VersionCheck.js";
|
|
3
|
+
import { Schema } from "effect";
|
|
4
|
+
|
|
5
|
+
// src/schema/Report.ts
|
|
6
|
+
const findingLine = (finding) => ` ${finding.label} at "${finding.path}": ${finding.message}`;
|
|
7
|
+
const blockingCount = (findings) => findings.filter((finding) => finding.severity === "warning").length;
|
|
8
|
+
const suggestionClause = (schema) => schema.nextVersion !== void 0 && schema.nextVersion !== schema.version ? ` → suggest ${schema.nextVersion}` : "";
|
|
9
|
+
const publishedClause = (schema) => schema.version !== void 0 ? ` at published ${schema.version}` : "";
|
|
10
|
+
const policyClause = (schema, report) => report.policy === void 0 ? ` [policy ${schema.policy}]` : "";
|
|
11
|
+
const frozenClause = (schema) => schema.frozen.length > 0 ? ` (frozen: ${schema.frozen.join(", ")})` : "";
|
|
12
|
+
const schemaLines = (schema, report) => {
|
|
13
|
+
const suffix = `${policyClause(schema, report)}${frozenClause(schema)}`;
|
|
14
|
+
switch (schema.outcome) {
|
|
15
|
+
case "written": return [`written (${schema.change}) ${schema.path}${suffix}`];
|
|
16
|
+
case "unchanged": return [`unchanged ${schema.path}${suffix}`];
|
|
17
|
+
case "would-write": return [`would write (${schema.change}) ${schema.path}${suffix}`];
|
|
18
|
+
case "drift": return [`DRIFT ${schema.change}${publishedClause(schema)}${suggestionClause(schema)} — ${schema.path}${suffix}`];
|
|
19
|
+
case "held": return [`held (${report.gateFailed ? "gate failed elsewhere" : "drift elsewhere"}) ${schema.path}${suffix}`];
|
|
20
|
+
case "gate-failed": return [`GATE FAILED ${schema.path}${suffix} (${blockingCount(schema.findings)} blocking finding(s))`, ...schema.findings.map(findingLine)];
|
|
21
|
+
default: return schema.outcome;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const sliceOwner = (slicePath) => (slicePath.split(/[\\/]/).pop() ?? slicePath).replace(/\.json$/, "");
|
|
25
|
+
const sliceCounts = (slice) => slice.caseFoldedMatch !== void 0 ? `(${slice.entries} entries; claimed ${slice.caseFoldedMatch} by case-folded match for "${sliceOwner(slice.path)}")` : `(${slice.entries} entries)`;
|
|
26
|
+
const sliceLine = (slice) => {
|
|
27
|
+
switch (slice.outcome) {
|
|
28
|
+
case "written": return `written catalog slice ${slice.path} ${sliceCounts(slice)}`;
|
|
29
|
+
case "unchanged": return `unchanged catalog slice ${slice.path} ${sliceCounts(slice)}`;
|
|
30
|
+
case "would-write": return `would write catalog slice ${slice.path} ${sliceCounts(slice)}`;
|
|
31
|
+
case "held": return `held catalog slice ${slice.path} ${sliceCounts(slice)}`;
|
|
32
|
+
case "orphaned": return `orphaned catalog slice ${slice.path} (no schema declares a catalog; the merged catalog keeps advertising its entries until it is deleted — delete it by hand; build never will)`;
|
|
33
|
+
default: return slice.outcome;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const conflictSubject = (conflict) => conflict.kind === "url" ? `url ${conflict.url}` : `name ${conflict.name}`;
|
|
37
|
+
const mergedLines = (merged) => {
|
|
38
|
+
const counts = `(${merged.entries} entries from ${merged.slices.length} slice(s))`;
|
|
39
|
+
switch (merged.outcome) {
|
|
40
|
+
case "written": return [`written catalog ${merged.path} ${counts}`];
|
|
41
|
+
case "unchanged": return [`unchanged catalog ${merged.path} ${counts}`];
|
|
42
|
+
case "would-write": return [`would write catalog ${merged.path} ${counts}`];
|
|
43
|
+
case "held": return [`held catalog ${merged.path} ${counts}`];
|
|
44
|
+
case "orphaned": return [`orphaned catalog ${merged.path} (no catalog slice remains — delete it by hand; build never will)`];
|
|
45
|
+
case "blocked": return [
|
|
46
|
+
`CATALOG BLOCKED ${merged.path} (not written: fix the slices below)`,
|
|
47
|
+
...merged.conflicts.map((conflict) => ` ${conflictSubject(conflict)} advertised by ${conflict.slices.join(", ")}`),
|
|
48
|
+
...merged.invalid.map(({ path, reason }) => ` slice ${path} is invalid: ${reason}`)
|
|
49
|
+
];
|
|
50
|
+
default: return merged.outcome;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const catalogLines = (catalog) => [...catalog.slice !== void 0 ? [sliceLine(catalog.slice)] : [], ...catalog.merged !== void 0 ? mergedLines(catalog.merged) : []];
|
|
54
|
+
const orphanedLine = (orphan) => `orphaned document ${orphan} (no target, frozen version, or catalog entry claims it — delete it by hand; build never will)`;
|
|
55
|
+
const driftClause = (report) => `drift ${report.policy !== void 0 ? `${report.policy} (flag)` : "per schema (config)"}, on-drift ${report.onDrift}`;
|
|
56
|
+
const summaryLine = (report) => {
|
|
57
|
+
const written = report.schemas.filter((schema) => schema.outcome === "written").length;
|
|
58
|
+
const unchanged = report.schemas.filter((schema) => schema.outcome === "unchanged").length;
|
|
59
|
+
const drift = report.schemas.filter((schema) => schema.verdict === "drift").length;
|
|
60
|
+
const gateFailed = report.schemas.filter((schema) => schema.outcome === "gate-failed").length;
|
|
61
|
+
return `${report.schemas.length} schema(s): ${written} written, ${unchanged} unchanged, ${drift} drift, ${gateFailed} gate failed — ${driftClause(report)}`;
|
|
62
|
+
};
|
|
63
|
+
const warningLine = (schema, report) => `warning: DRIFT ${schema.change}${publishedClause(schema)} ${report.mode === "build" ? "written" : "would write"} under --on-drift=warn — ${schema.path}`;
|
|
64
|
+
const tableCell = (cell) => cell.replace(/\\/g, "\\\\").replace(/\|/g, "\\|").replace(/\r?\n/g, " ");
|
|
65
|
+
const tableRow = (columns) => `| ${columns.map(tableCell).join(" | ")} |`;
|
|
66
|
+
const STEP_SUMMARY_DIFF_LINES = 200;
|
|
67
|
+
const fenceFor = (lines) => {
|
|
68
|
+
const longest = Math.max(0, ...lines.flatMap((line) => (line.match(/`+/g) ?? []).map((run) => run.length)));
|
|
69
|
+
return "`".repeat(Math.max(3, longest + 1));
|
|
70
|
+
};
|
|
71
|
+
const diffBlock = (schema, diff) => {
|
|
72
|
+
const lines = diff.unified.replace(/\n$/, "").split("\n");
|
|
73
|
+
const shown = lines.slice(0, STEP_SUMMARY_DIFF_LINES);
|
|
74
|
+
const fence = fenceFor(shown);
|
|
75
|
+
return [
|
|
76
|
+
"",
|
|
77
|
+
`**${schema.name}** ${schema.path}`,
|
|
78
|
+
"",
|
|
79
|
+
`${fence}diff`,
|
|
80
|
+
...shown,
|
|
81
|
+
fence,
|
|
82
|
+
...lines.length > shown.length ? ["", `… ${lines.length - shown.length} more lines`] : []
|
|
83
|
+
];
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Renders a {@link RunReport} for a terminal, a JSON consumer or a GitHub
|
|
87
|
+
* step summary.
|
|
88
|
+
*
|
|
89
|
+
* @public
|
|
90
|
+
*/
|
|
91
|
+
var Report = class {
|
|
92
|
+
constructor() {}
|
|
93
|
+
/**
|
|
94
|
+
* stdout lines.
|
|
95
|
+
*
|
|
96
|
+
* @remarks
|
|
97
|
+
* The summary's `drift` count is the number of schemas whose VERDICT is
|
|
98
|
+
* `"drift"`, independent of `written`/`unchanged`: under
|
|
99
|
+
* `onDrift: "warn"` a drifting schema is written AND counted as drift,
|
|
100
|
+
* so the four counts need not sum to the schema total.
|
|
101
|
+
*/
|
|
102
|
+
static human(report) {
|
|
103
|
+
const lines = [];
|
|
104
|
+
for (const schema of report.schemas) lines.push(...schemaLines(schema, report));
|
|
105
|
+
if (report.catalog !== void 0) lines.push(...catalogLines(report.catalog));
|
|
106
|
+
for (const orphan of report.orphaned ?? []) lines.push(orphanedLine(orphan));
|
|
107
|
+
lines.push(summaryLine(report));
|
|
108
|
+
return lines;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* stderr lines: one per drift written (or, under `check`, would be written)
|
|
112
|
+
* under `onDrift: "warn"`. Empty when the gate failed — nothing was (or
|
|
113
|
+
* would be) written, so there is no drift to warn about.
|
|
114
|
+
*/
|
|
115
|
+
static warnings(report) {
|
|
116
|
+
if (report.onDrift !== "warn" || report.gateFailed) return [];
|
|
117
|
+
return report.schemas.filter((schema) => schema.verdict === "drift").map((schema) => warningLine(schema, report));
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* One JSON document, stable key order: {@link RunReport.Json}'s encoding.
|
|
121
|
+
*
|
|
122
|
+
* @remarks
|
|
123
|
+
* A version mismatch the run went on past is not part of the report, so it is
|
|
124
|
+
* passed beside it and comes last, where `ValidateReport.Json` has it: an agent
|
|
125
|
+
* reading only stdout must see it. Without one the document is unchanged.
|
|
126
|
+
*/
|
|
127
|
+
static json(report, versionMismatch) {
|
|
128
|
+
const encoded = Schema.encodeSync(RunReport.Json)(report);
|
|
129
|
+
return JSON.stringify(versionMismatch === void 0 ? encoded : {
|
|
130
|
+
...encoded,
|
|
131
|
+
versionMismatch: Schema.encodeSync(VersionMismatch)(versionMismatch)
|
|
132
|
+
}, null, " ");
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The step-summary tables, then one fenced `diff` block per changed document,
|
|
136
|
+
* each capped; ends with a newline so a later append starts on its own line.
|
|
137
|
+
*/
|
|
138
|
+
static markdown(report) {
|
|
139
|
+
const lines = [`### effected schema ${report.mode}`, ""];
|
|
140
|
+
lines.push(tableRow([
|
|
141
|
+
"schema",
|
|
142
|
+
"version",
|
|
143
|
+
"frozen",
|
|
144
|
+
"published",
|
|
145
|
+
"change",
|
|
146
|
+
"outcome"
|
|
147
|
+
]), tableRow([
|
|
148
|
+
"---",
|
|
149
|
+
"---",
|
|
150
|
+
"---",
|
|
151
|
+
"---",
|
|
152
|
+
"---",
|
|
153
|
+
"---"
|
|
154
|
+
]));
|
|
155
|
+
for (const schema of report.schemas) lines.push(tableRow([
|
|
156
|
+
schema.name,
|
|
157
|
+
schema.version ?? "",
|
|
158
|
+
schema.frozen.join(", "),
|
|
159
|
+
schema.published ? "yes" : "no",
|
|
160
|
+
schema.change,
|
|
161
|
+
schema.outcome
|
|
162
|
+
]));
|
|
163
|
+
if (report.catalog !== void 0) {
|
|
164
|
+
const { slice, merged } = report.catalog;
|
|
165
|
+
lines.push("", tableRow([
|
|
166
|
+
"catalog",
|
|
167
|
+
"file",
|
|
168
|
+
"entries",
|
|
169
|
+
"outcome"
|
|
170
|
+
]), tableRow([
|
|
171
|
+
"---",
|
|
172
|
+
"---",
|
|
173
|
+
"---",
|
|
174
|
+
"---"
|
|
175
|
+
]), ...slice !== void 0 ? [tableRow([
|
|
176
|
+
"slice",
|
|
177
|
+
slice.path,
|
|
178
|
+
String(slice.entries),
|
|
179
|
+
slice.caseFoldedMatch !== void 0 ? `${slice.outcome} (case-folded match: ${slice.caseFoldedMatch})` : slice.outcome
|
|
180
|
+
])] : [], ...merged !== void 0 ? [tableRow([
|
|
181
|
+
"merged",
|
|
182
|
+
merged.path,
|
|
183
|
+
String(merged.entries),
|
|
184
|
+
merged.outcome
|
|
185
|
+
])] : []);
|
|
186
|
+
if (merged !== void 0 && (merged.conflicts.length > 0 || merged.invalid.length > 0)) lines.push("", tableRow(["catalog problem", "slices"]), tableRow(["---", "---"]), ...merged.conflicts.map((conflict) => tableRow([conflictSubject(conflict), conflict.slices.join(", ")])), ...merged.invalid.map(({ path, reason }) => tableRow([`invalid: ${reason}`, path])));
|
|
187
|
+
}
|
|
188
|
+
if (report.orphaned !== void 0) lines.push("", tableRow(["orphaned document", "claimed by"]), tableRow(["---", "---"]), ...report.orphaned.map((orphan) => tableRow([orphan, "nothing — delete by hand"])));
|
|
189
|
+
lines.push("");
|
|
190
|
+
if (report.gateFailed) {
|
|
191
|
+
const failed = report.schemas.filter((schema) => schema.outcome === "gate-failed").length;
|
|
192
|
+
lines.push(`**Gate:** ${failed} schema(s) failed`);
|
|
193
|
+
} else if (report.drifted) {
|
|
194
|
+
const drifted = report.schemas.filter((schema) => schema.verdict === "drift").length;
|
|
195
|
+
lines.push(`**Drift:** ${drifted} schema(s) drifted — ${driftClause(report)}`);
|
|
196
|
+
} else lines.push("**Drift:** none");
|
|
197
|
+
for (const schema of report.schemas) if (schema.diff !== void 0) lines.push(...diffBlock(schema, schema.diff));
|
|
198
|
+
lines.push("");
|
|
199
|
+
return lines.join("\n");
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
export { Report };
|