@pbuilder/sdk 0.18.0 → 0.20.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/README.md +89 -3
- package/dist/acl/irwire/stdio-engine-client.d.ts +2 -1
- package/dist/acl/irwire/stdio-engine-client.js +12 -2
- package/dist/bin/pbuilder-codegen.js +148 -18
- package/dist/bin/pbuilder-conformance.js +24 -5
- package/dist/bounded-contexts/authoring/application/usecases/authoring-verbs/index.d.ts +15 -0
- package/dist/bounded-contexts/authoring/application/usecases/authoring-verbs/index.js +17 -0
- package/dist/bounded-contexts/authoring/application/usecases/run-factory/context.d.ts +21 -5
- package/dist/bounded-contexts/authoring/application/usecases/run-factory/context.js +59 -12
- package/dist/bounded-contexts/authoring/domain/aggregates/session/session.d.ts +4 -2
- package/dist/bounded-contexts/authoring/domain/aggregates/session/session.js +12 -3
- package/dist/bounded-contexts/authoring/domain/ports/engine-client.d.ts +2 -1
- package/dist/bounded-contexts/authoring/domain/services/define-dialect.d.ts +28 -1
- package/dist/bounded-contexts/authoring/domain/services/define-dialect.js +27 -3
- package/dist/bounded-contexts/authoring/domain/services/evaluation.d.ts +29 -0
- package/dist/bounded-contexts/authoring/domain/services/evaluation.js +43 -0
- package/dist/bounded-contexts/authoring/domain/services/glob-match.d.ts +4 -0
- package/dist/bounded-contexts/authoring/domain/services/glob-match.js +81 -0
- package/dist/bounded-contexts/authoring/domain/services/schema/input-rejection.d.ts +2 -0
- package/dist/bounded-contexts/authoring/domain/services/schema/input-rejection.js +8 -0
- package/dist/bounded-contexts/authoring/domain/services/schema/schema-parse.d.ts +3 -2
- package/dist/bounded-contexts/authoring/domain/services/schema/schema-parse.js +46 -4
- package/dist/bounded-contexts/authoring/domain/services/schema/schema-validate.d.ts +18 -2
- package/dist/bounded-contexts/authoring/domain/services/schema/schema-validate.js +131 -2
- package/dist/bounded-contexts/authoring/domain/valueobjects/authoring-error.d.ts +3 -2
- package/dist/bounded-contexts/authoring/domain/valueobjects/authoring-error.js +2 -0
- package/dist/bounded-contexts/authoring/domain/valueobjects/findings.d.ts +12 -0
- package/dist/bounded-contexts/authoring/domain/valueobjects/findings.js +57 -0
- package/dist/bounded-contexts/authoring/domain/valueobjects/schema-model.d.ts +19 -0
- package/dist/bounded-contexts/authoring/domain/valueobjects/schema-model.js +1 -0
- package/dist/bounded-contexts/authoring/infrastructure/in/codegen/cli.d.ts +7 -2
- package/dist/bounded-contexts/authoring/infrastructure/in/codegen/cli.js +28 -7
- package/dist/bounded-contexts/authoring/infrastructure/in/codegen/emit-type.d.ts +6 -1
- package/dist/bounded-contexts/authoring/infrastructure/in/codegen/emit-type.js +44 -11
- package/dist/bounded-contexts/authoring/infrastructure/in/codegen/project-codegen.d.ts +1 -0
- package/dist/bounded-contexts/authoring/infrastructure/in/codegen/project-codegen.js +5 -2
- package/dist/bounded-contexts/authoring/infrastructure/in/commons/index.d.ts +2 -1
- package/dist/bounded-contexts/authoring/infrastructure/in/commons/index.js +1 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/angular/index.d.ts +14 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/angular/index.js +15 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/aspnet/index.d.ts +14 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/aspnet/index.js +15 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/index.d.ts +14 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/index.js +15 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/css/index.d.ts +14 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/css/index.js +15 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/json/index.d.ts +14 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/json/index.js +15 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/msbuild/index.d.ts +14 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/msbuild/index.js +15 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/nestjs/index.d.ts +14 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/nestjs/index.js +15 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/index.d.ts +14 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/index.js +15 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/typescript/index.d.ts +14 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/typescript/index.js +15 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/yaml/index.d.ts +14 -1
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/yaml/index.js +15 -0
- package/dist/bounded-contexts/authoring/testing/conformance/run-package.d.ts +17 -0
- package/dist/bounded-contexts/authoring/testing/conformance/run-package.js +124 -44
- package/dist/bounded-contexts/authoring/testing/contract-fake.d.ts +1 -0
- package/dist/bounded-contexts/authoring/testing/contract-fake.js +8 -0
- package/dist/bounded-contexts/authoring/testing/glob-pattern.d.ts +5 -0
- package/dist/bounded-contexts/authoring/testing/glob-pattern.js +20 -0
- package/dist/bounded-contexts/authoring/testing/harness/index.d.ts +18 -4
- package/dist/bounded-contexts/authoring/testing/harness/index.js +17 -4
- package/dist/bounded-contexts/authoring/testing/run-vehicle.d.ts +1 -0
- package/dist/bounded-contexts/authoring/testing/run-vehicle.js +4 -0
- package/dist/bounded-contexts/runner-hosting/infrastructure/in/bootstrap-bridge.d.ts +3 -0
- package/dist/bounded-contexts/runner-hosting/infrastructure/in/bootstrap-bridge.js +4 -0
- package/dist/bounded-contexts/runner-hosting/infrastructure/in/runner.js +52 -13
- package/dist/bounded-contexts/runner-hosting/infrastructure/in/single-instance-probe.d.ts +14 -3
- package/dist/bounded-contexts/runner-hosting/infrastructure/in/single-instance-probe.js +17 -8
- package/dist/conformance/index.d.ts +1 -1
- package/dist/conformance/index.js +1 -1
- package/dist/runner-manifest.json +37 -13
- package/dist/shared-kernel/wire-protocol.d.ts +1 -1
- package/dist/shared-kernel/wire-protocol.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -219,6 +219,80 @@ everything you can do inside a factory, and the
|
|
|
219
219
|
[quickstart](./docs/guides/quickstart.md) covers the standalone-package setup (tsconfig
|
|
220
220
|
included) in more depth.
|
|
221
221
|
|
|
222
|
+
## Returning a Result
|
|
223
|
+
|
|
224
|
+
A factory may hand a value back: to Launch when it is the root schematic, with the Plan of a
|
|
225
|
+
Dry Run or the applied operations of a Commit. Declare it under `result` in `schema.json`,
|
|
226
|
+
next to `properties`:
|
|
227
|
+
|
|
228
|
+
```json
|
|
229
|
+
{
|
|
230
|
+
"properties": {},
|
|
231
|
+
"result": {
|
|
232
|
+
"type": "object",
|
|
233
|
+
"properties": { "name": { "type": "string" }, "files": { "type": "array", "items": { "type": "string" } } },
|
|
234
|
+
"required": ["name"]
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
`type` is one of `string`, `number`, `boolean`, `null`, `object` and `array`, or a list of
|
|
240
|
+
them (`["string", "null"]`). An object with `properties` accepts only the keys it names, and
|
|
241
|
+
`required` lists the ones it must have; an object without `properties` accepts any JSON
|
|
242
|
+
object. An array may give its `items`. A key that is not `required` may be left out, but never set to
|
|
243
|
+
`undefined`: `undefined` is not JSON, so `{ "name": "a", "files": undefined }` fails. The
|
|
244
|
+
generated type marks such a key `files?:`, which TypeScript lets you set to `undefined` unless
|
|
245
|
+
your tsconfig enables `exactOptionalPropertyTypes`; leave the key out instead. Codegen emits the matching `Result` type next to
|
|
246
|
+
`Input`, so the compiler checks what the factory returns:
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
import type { Input, Result } from "./schema.generated.ts";
|
|
250
|
+
|
|
251
|
+
export default (input: Input): Result => ({ name: "payments", files: [] });
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
The SDK checks the returned value at the factory boundary before anything is written. It
|
|
255
|
+
must match the declaration and be JSON: no functions, class instances (a `Date`, a `Map`),
|
|
256
|
+
cycles, `undefined` or non-finite numbers. Its JSON encoding may take at most 1 MiB
|
|
257
|
+
(1048576 bytes). A schematic that declares no Result must return nothing. When any of this
|
|
258
|
+
fails, the run fails with an `AuthoringError` whose reason is `invalid-result`, and nothing
|
|
259
|
+
is written. A declared `null` is a Result and is returned as `null`.
|
|
260
|
+
|
|
261
|
+
## Evaluation schematics
|
|
262
|
+
|
|
263
|
+
A schematic whose Collection definition entry declares `"role": "evaluation"` reads the
|
|
264
|
+
workspace and returns `Findings`, which the role fixes as its Result (A-120). It declares no
|
|
265
|
+
`result` in `schema.json`, and no input named `include` or `exclude`; codegen refuses either.
|
|
266
|
+
Codegen reads the role only in project mode (`pbuilder-codegen` or `--project`), since a single
|
|
267
|
+
directory has no Collection definition entry. For an evaluation schematic it emits `Findings`
|
|
268
|
+
as the `Result` type, and adds the scope inputs `include` and `exclude`, arrays of globs, to
|
|
269
|
+
`Input` (A-122):
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
import type { Input, Result } from "./schema.generated.ts";
|
|
273
|
+
import { glob } from "@pbuilder/sdk/commons";
|
|
274
|
+
|
|
275
|
+
export default async (input: Input): Promise<Result> => {
|
|
276
|
+
const routes = await glob("src/**/route.ts");
|
|
277
|
+
return {
|
|
278
|
+
items: routes.length > 0 ? [] : [{ rule: "route-missing", message: "no route file", severity: "error" }],
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
A finding has `rule`, `message` and `severity` (`error` or `warning`), and may have a
|
|
284
|
+
workspace-relative `path`, a 1-based `range` (`{ start: { line, column }, end? }`) and a `fix`
|
|
285
|
+
(`{ schematic: "collection:schematic", inputs? }`). The SDK checks the returned value against
|
|
286
|
+
that shape as it checks any Result, then sets each finding's `source` to the schematic's
|
|
287
|
+
qualified name. A finding that sets `source` itself fails the run with `invalid-result` (A-121).
|
|
288
|
+
|
|
289
|
+
The scope is what `include` names, less what `exclude` names; `exclude` alone keeps every file
|
|
290
|
+
it does not name, and with neither nothing is narrowed. Every `glob` result, from commons or a
|
|
291
|
+
Dialect, is narrowed to the scope (A-123), and a finding whose `path` falls outside it is dropped
|
|
292
|
+
before the Result leaves the schematic (A-124). A finding without `path` is always kept. `find`
|
|
293
|
+
is not narrowed, so a check can read the files it compares against (A-125). Every verb a
|
|
294
|
+
generator has is offered as well; the engine discards an evaluation Frame's edits (A-126).
|
|
295
|
+
|
|
222
296
|
## Project-wide type generation
|
|
223
297
|
|
|
224
298
|
```sh
|
|
@@ -238,7 +312,7 @@ Two registration forms are supported:
|
|
|
238
312
|
```json
|
|
239
313
|
{
|
|
240
314
|
"collections": {
|
|
241
|
-
"local": { "hello": { "path": "./schematics/hello" } },
|
|
315
|
+
"local": { "hello": { "path": "./schematics/hello" }, "check": { "path": "./schematics/check", "role": "evaluation" } },
|
|
242
316
|
"shared": { "path": "./schematics/collection.json" }
|
|
243
317
|
}
|
|
244
318
|
}
|
|
@@ -313,10 +387,13 @@ The verbs that write to a new path — `create`, `rename`, `move`, `copy`, `copy
|
|
|
313
387
|
overwrite: `create` and `scaffold` inside their options/args object, the rest as a
|
|
314
388
|
trailing argument.
|
|
315
389
|
|
|
316
|
-
|
|
390
|
+
Four companions round out the surface:
|
|
317
391
|
|
|
318
392
|
- [`find(path).read()`](./docs/guides/authoring-verbs.md#the-read-trichotomy-findpathread) — read
|
|
319
393
|
a file back from the staging tree.
|
|
394
|
+
- [`glob(pattern, { ignore })`](./docs/guides/authoring-verbs.md#many-files-at-once-globpattern--ignore-)
|
|
395
|
+
resolves a handle for every file the pattern matches, ordered by path, or `[]` when none
|
|
396
|
+
does. Every dialect has its own `glob`, which keeps the files of the extensions it declares.
|
|
320
397
|
- [`scaffold({ from, to, options })`](./docs/guides/authoring-verbs.md#scaffold) — mirror an
|
|
321
398
|
entire package-local folder into the tree, with dynamic file names. The workhorse for
|
|
322
399
|
multi-file generators — see [Scaffolding a folder of templates](#scaffolding-a-folder-of-templates).
|
|
@@ -554,6 +631,14 @@ Three things to know:
|
|
|
554
631
|
into schema-derived input validation against the adjacent `schema.json`. Without it those
|
|
555
632
|
verbs have nothing to resolve against.
|
|
556
633
|
|
|
634
|
+
- `result.result` is the Result the run committed: what the factory returned, checked
|
|
635
|
+
against the `result` its `schema.json` declares (see [Returning a Result](#returning-a-result)).
|
|
636
|
+
It is `undefined` when the factory declares none or the run failed.
|
|
637
|
+
|
|
638
|
+
- `role: "evaluation"` with `name: "collection:schematic"` runs the factory as an
|
|
639
|
+
[evaluation schematic](#evaluation-schematics): `result.result` holds its Findings with their
|
|
640
|
+
`source`, narrowed to the scope of the `include` and `exclude` inputs.
|
|
641
|
+
|
|
557
642
|
- `result.reads` counts the reads the engine served for each path, so a test can check
|
|
558
643
|
that the factory reads a file once however many ops it chains on it. A path the factory
|
|
559
644
|
never read is absent.
|
|
@@ -582,7 +667,8 @@ ship today: `@pbuilder/sdk/typescript`, `@pbuilder/sdk/react`, `@pbuilder/sdk/ne
|
|
|
582
667
|
A dialect's entry point is its own `find(path)`: it opens a **coalescing, awaitable
|
|
583
668
|
handle**. Every op you chain mutates the same live AST, and the handle serializes to
|
|
584
669
|
exactly **one** `replaceContent`-style directive when it flushes — chaining three ops does
|
|
585
|
-
not produce three writes.
|
|
670
|
+
not produce three writes. Its `glob(pattern, { ignore })` opens every matching file of the
|
|
671
|
+
dialect's extensions the same way.
|
|
586
672
|
|
|
587
673
|
### `@pbuilder/sdk/typescript`
|
|
588
674
|
|
|
@@ -22,6 +22,7 @@ export declare class StdioEngineClient implements EngineClient {
|
|
|
22
22
|
});
|
|
23
23
|
emit(batch: Batch): Promise<void>;
|
|
24
24
|
read(path: string): Promise<string | undefined>;
|
|
25
|
-
|
|
25
|
+
list(pattern: string, ignore?: readonly string[]): Promise<readonly string[]>;
|
|
26
|
+
commit(result?: unknown): Promise<void>;
|
|
26
27
|
discard(): Promise<void>;
|
|
27
28
|
}
|
|
@@ -228,8 +228,18 @@ export class StdioEngineClient {
|
|
|
228
228
|
return undefined;
|
|
229
229
|
return result.content;
|
|
230
230
|
}
|
|
231
|
-
|
|
232
|
-
|
|
231
|
+
// ADR-0070 (engine): `ignore` is optional on the wire, so it is sent only when given. A host
|
|
232
|
+
// rejection, such as a pattern that escapes the workspace, throws as a read's does.
|
|
233
|
+
async list(pattern, ignore) {
|
|
234
|
+
const response = await this.#call("tree.list", ignore === undefined ? { pattern } : { pattern, ignore });
|
|
235
|
+
if (response.error !== undefined) {
|
|
236
|
+
throw new IntentRejectedError(response.error.message);
|
|
237
|
+
}
|
|
238
|
+
return response.result.paths;
|
|
239
|
+
}
|
|
240
|
+
// A-14: the Result rides in the ir.commit params, the key present only when one is declared.
|
|
241
|
+
async commit(result) {
|
|
242
|
+
const response = await this.#call("ir.commit", result === undefined ? {} : { result });
|
|
233
243
|
if (response.error !== undefined) {
|
|
234
244
|
throw new IntentRejectedError(response.error.message);
|
|
235
245
|
}
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
import { join as join3, dirname as dirname2, basename, resolve as resolve2, sep } from "node:path";
|
|
20
20
|
|
|
21
21
|
// src/bounded-contexts/authoring/domain/valueobjects/schema-model.ts
|
|
22
|
+
var RESULT_KINDS = new Set(["string", "number", "boolean", "null", "object", "array"]);
|
|
22
23
|
var FORBIDDEN_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
23
24
|
function isPlainObject(value) {
|
|
24
25
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
@@ -256,7 +257,46 @@ function parseSchema(raw) {
|
|
|
256
257
|
description: typeof value.description === "string" ? value.description : undefined
|
|
257
258
|
});
|
|
258
259
|
}
|
|
259
|
-
|
|
260
|
+
if (!Object.hasOwn(parsed, "result"))
|
|
261
|
+
return { properties };
|
|
262
|
+
const result = parseResult(parsed.result);
|
|
263
|
+
if (result === undefined) {
|
|
264
|
+
throw new SchemaParseFailure({ problem: 'invalid "result" declaration', line: undefined, column: undefined });
|
|
265
|
+
}
|
|
266
|
+
return { properties, result };
|
|
267
|
+
}
|
|
268
|
+
function parseResult(raw) {
|
|
269
|
+
if (!isPlainObject(raw))
|
|
270
|
+
return;
|
|
271
|
+
const types = Array.isArray(raw.type) ? raw.type : [raw.type];
|
|
272
|
+
if (types.length === 0 || !types.every((type) => typeof type === "string" && RESULT_KINDS.has(type)))
|
|
273
|
+
return;
|
|
274
|
+
const schema = { types };
|
|
275
|
+
if (raw.properties !== undefined) {
|
|
276
|
+
if (!isPlainObject(raw.properties))
|
|
277
|
+
return;
|
|
278
|
+
const properties = [];
|
|
279
|
+
for (const key of Object.keys(raw.properties)) {
|
|
280
|
+
const property = FORBIDDEN_KEYS.has(key) ? undefined : parseResult(raw.properties[key]);
|
|
281
|
+
if (property === undefined)
|
|
282
|
+
return;
|
|
283
|
+
properties.push({ key, schema: property });
|
|
284
|
+
}
|
|
285
|
+
schema.properties = properties;
|
|
286
|
+
}
|
|
287
|
+
if (raw.required !== undefined) {
|
|
288
|
+
const declared = new Set(schema.properties?.map((property) => property.key));
|
|
289
|
+
if (!Array.isArray(raw.required) || !raw.required.every((key) => typeof key === "string" && declared.has(key)))
|
|
290
|
+
return;
|
|
291
|
+
schema.required = raw.required;
|
|
292
|
+
}
|
|
293
|
+
if (raw.items !== undefined) {
|
|
294
|
+
const items = parseResult(raw.items);
|
|
295
|
+
if (items === undefined)
|
|
296
|
+
return;
|
|
297
|
+
schema.items = items;
|
|
298
|
+
}
|
|
299
|
+
return schema;
|
|
260
300
|
}
|
|
261
301
|
|
|
262
302
|
// src/bounded-contexts/authoring/domain/services/schema/schema-digest.ts
|
|
@@ -311,6 +351,57 @@ function isErrnoException(err) {
|
|
|
311
351
|
return err instanceof Error && "code" in err;
|
|
312
352
|
}
|
|
313
353
|
|
|
354
|
+
// src/bounded-contexts/authoring/domain/valueobjects/findings.ts
|
|
355
|
+
var SCOPE_INPUTS = ["include", "exclude"];
|
|
356
|
+
var STRING = { types: ["string"] };
|
|
357
|
+
var NUMBER = { types: ["number"] };
|
|
358
|
+
var POSITION = {
|
|
359
|
+
types: ["object"],
|
|
360
|
+
properties: [
|
|
361
|
+
{ key: "line", schema: NUMBER },
|
|
362
|
+
{ key: "column", schema: NUMBER }
|
|
363
|
+
],
|
|
364
|
+
required: ["line", "column"]
|
|
365
|
+
};
|
|
366
|
+
function findingsWith(extra) {
|
|
367
|
+
const finding = {
|
|
368
|
+
types: ["object"],
|
|
369
|
+
properties: [
|
|
370
|
+
{ key: "rule", schema: STRING },
|
|
371
|
+
{ key: "message", schema: STRING },
|
|
372
|
+
{ key: "severity", schema: { types: ["string"], choices: ["error", "warning"] } },
|
|
373
|
+
{ key: "path", schema: STRING },
|
|
374
|
+
{
|
|
375
|
+
key: "range",
|
|
376
|
+
schema: {
|
|
377
|
+
types: ["object"],
|
|
378
|
+
properties: [
|
|
379
|
+
{ key: "start", schema: POSITION },
|
|
380
|
+
{ key: "end", schema: POSITION }
|
|
381
|
+
],
|
|
382
|
+
required: ["start"]
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
key: "fix",
|
|
387
|
+
schema: {
|
|
388
|
+
types: ["object"],
|
|
389
|
+
properties: [
|
|
390
|
+
{ key: "schematic", schema: STRING },
|
|
391
|
+
{ key: "inputs", schema: { types: ["object"] } }
|
|
392
|
+
],
|
|
393
|
+
required: ["schematic"]
|
|
394
|
+
}
|
|
395
|
+
},
|
|
396
|
+
...extra
|
|
397
|
+
],
|
|
398
|
+
required: ["rule", "message", "severity"]
|
|
399
|
+
};
|
|
400
|
+
return { types: ["object"], properties: [{ key: "items", schema: { types: ["array"], items: finding } }], required: ["items"] };
|
|
401
|
+
}
|
|
402
|
+
var FINDINGS = findingsWith([]);
|
|
403
|
+
var SOURCED_FINDINGS = findingsWith([{ key: "source", schema: STRING }]);
|
|
404
|
+
|
|
314
405
|
// src/bounded-contexts/authoring/infrastructure/in/codegen/emit-type.ts
|
|
315
406
|
var GENERATED_HEADER = "// AUTO-GENERATED by pbuilder-codegen — do not edit. Regenerate: pbuilder-codegen <package-dir>";
|
|
316
407
|
var IDENTIFIER_RE = /^[A-Za-z_$][\w$]*$/;
|
|
@@ -347,18 +438,36 @@ function emitProperty(property) {
|
|
|
347
438
|
` : "";
|
|
348
439
|
return `${doc} ${key}${optional}: ${type};`;
|
|
349
440
|
}
|
|
350
|
-
function
|
|
351
|
-
|
|
441
|
+
function emitResult(result) {
|
|
442
|
+
return result.types.map((kind) => {
|
|
443
|
+
switch (kind) {
|
|
444
|
+
case "array":
|
|
445
|
+
return `Array<${result.items === undefined ? "unknown" : emitResult(result.items)}>`;
|
|
446
|
+
case "object": {
|
|
447
|
+
if (result.properties === undefined)
|
|
448
|
+
return "{ [key: string]: unknown }";
|
|
449
|
+
const required = new Set(result.required);
|
|
450
|
+
const members = result.properties.map(({ key, schema }) => `${emitPropertyKey(key)}${required.has(key) ? "" : "?"}: ${emitResult(schema)}`);
|
|
451
|
+
return members.length === 0 ? "Record<string, never>" : `{ ${members.join("; ")} }`;
|
|
452
|
+
}
|
|
453
|
+
case "string":
|
|
454
|
+
return result.choices === undefined ? kind : result.choices.map((choice) => JSON.stringify(choice)).join(" | ");
|
|
455
|
+
default:
|
|
456
|
+
return kind;
|
|
457
|
+
}
|
|
458
|
+
}).join(" | ");
|
|
459
|
+
}
|
|
460
|
+
var SCOPE_MEMBERS = [
|
|
461
|
+
" /** Globs or literal paths to check */",
|
|
462
|
+
" include?: string[];",
|
|
463
|
+
" /** Globs or literal paths to skip */",
|
|
464
|
+
" exclude?: string[];"
|
|
465
|
+
];
|
|
466
|
+
function emitInputType(schema, digest, evaluation = false) {
|
|
467
|
+
const members = [...schema.properties.map(emitProperty), ...evaluation ? SCOPE_MEMBERS : []].join(`
|
|
352
468
|
`);
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
`// @schema-digest sha256:${digest}`,
|
|
356
|
-
"",
|
|
357
|
-
"export type Input = {",
|
|
358
|
-
members,
|
|
359
|
-
"};",
|
|
360
|
-
""
|
|
361
|
-
].join(`
|
|
469
|
+
const result = evaluation ? ["", `export type Findings = ${emitResult(FINDINGS)};`, "", "export type Result = Findings;"] : schema.result === undefined ? [] : ["", `export type Result = ${emitResult(schema.result)};`];
|
|
470
|
+
return [GENERATED_HEADER, `// @schema-digest sha256:${digest}`, "", "export type Input = {", members, "};", ...result, ""].join(`
|
|
362
471
|
`);
|
|
363
472
|
}
|
|
364
473
|
|
|
@@ -373,6 +482,9 @@ function readObject(path) {
|
|
|
373
482
|
throw new Error("invalid object");
|
|
374
483
|
return value;
|
|
375
484
|
}
|
|
485
|
+
function isEvaluation(registration) {
|
|
486
|
+
return isPlainObject(registration) && registration.role === "evaluation";
|
|
487
|
+
}
|
|
376
488
|
function discoverProject(directory) {
|
|
377
489
|
let selected = resolve(directory ?? process.cwd());
|
|
378
490
|
if (directory === undefined) {
|
|
@@ -436,7 +548,7 @@ function discoverProject(directory) {
|
|
|
436
548
|
try {
|
|
437
549
|
if (!statSync(module).isFile())
|
|
438
550
|
throw new Error("nonregular module");
|
|
439
|
-
entries.push({ kind: "target", label, directory: dirname(realpathSync(module)) });
|
|
551
|
+
entries.push({ kind: "target", label, directory: dirname(realpathSync(module)), evaluation: isEvaluation(registration) });
|
|
440
552
|
} catch {
|
|
441
553
|
entries.push({ kind: "failure", label, diagnostic: `cannot resolve factory module ${JSON.stringify(module)}` });
|
|
442
554
|
}
|
|
@@ -477,7 +589,7 @@ function discoverProject(directory) {
|
|
|
477
589
|
}
|
|
478
590
|
if (factories !== 1)
|
|
479
591
|
throw new Error("ambiguous or missing factory");
|
|
480
|
-
entries.push({ kind: "target", label, directory: realpathSync(path) });
|
|
592
|
+
entries.push({ kind: "target", label, directory: realpathSync(path), evaluation: isEvaluation(registration) });
|
|
481
593
|
} catch {
|
|
482
594
|
entries.push({ kind: "failure", label, diagnostic: `directory requires exactly one regular factory.ts or factory.js: ${JSON.stringify(path)}` });
|
|
483
595
|
}
|
|
@@ -505,6 +617,22 @@ class SchemaSufficiencyFailure extends Error {
|
|
|
505
617
|
}
|
|
506
618
|
}
|
|
507
619
|
|
|
620
|
+
class RoleContractFailure extends Error {
|
|
621
|
+
constructor(problem) {
|
|
622
|
+
super(problem);
|
|
623
|
+
this.name = "RoleContractFailure";
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
function checkEvaluationSchema(schema) {
|
|
627
|
+
if (schema.result !== undefined) {
|
|
628
|
+
throw new RoleContractFailure("an evaluation schematic declares a Result, which the role fixes as Findings");
|
|
629
|
+
}
|
|
630
|
+
const imposed = schema.properties.find((property) => SCOPE_INPUTS.includes(property.key));
|
|
631
|
+
if (imposed !== undefined) {
|
|
632
|
+
throw new RoleContractFailure(`an evaluation schematic declares the input ${JSON.stringify(imposed.key)}, which the role imposes`);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
508
636
|
class OutputWriteFailure extends Error {
|
|
509
637
|
constructor(outputPath, cause, reason) {
|
|
510
638
|
const code = isErrnoException(cause) ? cause.code : undefined;
|
|
@@ -527,7 +655,7 @@ function assertOutputLeaf(outputPath) {
|
|
|
527
655
|
throw err instanceof OutputWriteFailure ? err : new OutputWriteFailure(outputPath, err);
|
|
528
656
|
}
|
|
529
657
|
}
|
|
530
|
-
function generateSchema(packageDir) {
|
|
658
|
+
function generateSchema(packageDir, evaluation = false) {
|
|
531
659
|
const schemaPath = schemaPathFor(packageDir);
|
|
532
660
|
const raw = readFileSync2(schemaPath, "utf-8");
|
|
533
661
|
const schema = parseSchema(raw);
|
|
@@ -535,8 +663,10 @@ function generateSchema(packageDir) {
|
|
|
535
663
|
if (sufficiencyFindings.length > 0) {
|
|
536
664
|
throw new SchemaSufficiencyFailure(sufficiencyFindings);
|
|
537
665
|
}
|
|
666
|
+
if (evaluation)
|
|
667
|
+
checkEvaluationSchema(schema);
|
|
538
668
|
const digest = computeSchemaDigest(raw);
|
|
539
|
-
const output = emitInputType(schema, digest);
|
|
669
|
+
const output = emitInputType(schema, digest, evaluation);
|
|
540
670
|
const outputPath = join3(packageDir, GENERATED_FILENAME);
|
|
541
671
|
try {
|
|
542
672
|
if (process.platform === "linux" || process.platform === "darwin") {
|
|
@@ -719,11 +849,11 @@ function runProject(directory) {
|
|
|
719
849
|
seen.add(output);
|
|
720
850
|
if (!statSync2(schemaPath).isFile())
|
|
721
851
|
throw new Error("nonregular schema");
|
|
722
|
-
generateSchema(entry.directory);
|
|
852
|
+
generateSchema(entry.directory, entry.evaluation);
|
|
723
853
|
generated++;
|
|
724
854
|
} catch (error) {
|
|
725
855
|
failed++;
|
|
726
|
-
const diagnostic = error instanceof SchemaParseFailure ? formatParseError(schemaPath, error) : error instanceof SchemaSufficiencyFailure ? formatSufficiencyError(schemaPath, error) : error instanceof WriteContainmentRefusal ? "refusing to write outside the selected project root" : error instanceof OutputWriteFailure ? "cannot write a regular, non-symbolic-link output" : "cannot read or generate schema.json";
|
|
856
|
+
const diagnostic = error instanceof SchemaParseFailure ? formatParseError(schemaPath, error) : error instanceof SchemaSufficiencyFailure ? formatSufficiencyError(schemaPath, error) : error instanceof RoleContractFailure ? `pbuilder-codegen: ${schemaPath}: ${error.message}` : error instanceof WriteContainmentRefusal ? "refusing to write outside the selected project root" : error instanceof OutputWriteFailure ? "cannot write a regular, non-symbolic-link output" : "cannot read or generate schema.json";
|
|
727
857
|
console.error(`pbuilder-codegen: ${JSON.stringify(entry.label)} (${JSON.stringify(entry.directory)}): ${JSON.stringify(diagnostic)}`);
|
|
728
858
|
}
|
|
729
859
|
}
|
|
@@ -1,21 +1,40 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
// `pbuilder-conformance --dialect <installed package dir>` (CA-13)
|
|
2
|
+
// `pbuilder-conformance --dialect <installed package dir>` (CA-13), or
|
|
3
|
+
// `pbuilder-conformance --local <name> --conformance <file> --path <base file>` for a local dialect
|
|
4
|
+
// (CA-18): prints one JSON report line.
|
|
3
5
|
// Exit 0: the fixture passed. Exit 1: it ran and failed, a missing fixture included. Exit 2: it
|
|
4
6
|
// could not run, with nothing on stdout. Runs under Bun, from the SDK root of the Run, so the
|
|
5
7
|
// dialect's `@pbuilder/sdk` imports reach this same SDK.
|
|
6
|
-
import { checkDialectPackage } from "../conformance/index.js";
|
|
8
|
+
import { checkDialectPackage, checkLocalDialect } from "../conformance/index.js";
|
|
7
9
|
import { redirectConsoleToStderr } from "../shared-kernel/framing.js";
|
|
8
10
|
// The fixture runs the author's code, so everything but the report goes to stderr.
|
|
9
11
|
const writeReport = process.stdout.write.bind(process.stdout);
|
|
10
12
|
Object.defineProperty(process, "stdout", { value: process.stderr, writable: true, configurable: true });
|
|
11
13
|
redirectConsoleToStderr();
|
|
12
14
|
const COULD_NOT_RUN = 2;
|
|
15
|
+
const USAGE = "usage: pbuilder-conformance --dialect <installed package dir>\n" +
|
|
16
|
+
" pbuilder-conformance --local <name> --conformance <file> --path <base file>\n";
|
|
17
|
+
function check(argv) {
|
|
18
|
+
if (argv.some((value) => value === ""))
|
|
19
|
+
return undefined;
|
|
20
|
+
if (argv.length === 2 && argv[0] === "--dialect")
|
|
21
|
+
return checkDialectPackage(argv[1]);
|
|
22
|
+
if (argv.length !== 6 || argv[0] !== "--local")
|
|
23
|
+
return undefined;
|
|
24
|
+
const flags = new Map([[argv[2], argv[3]], [argv[4], argv[5]]]);
|
|
25
|
+
const conformance = flags.get("--conformance");
|
|
26
|
+
const path = flags.get("--path");
|
|
27
|
+
if (conformance === undefined || path === undefined)
|
|
28
|
+
return undefined;
|
|
29
|
+
return checkLocalDialect({ name: argv[1], conformance, path });
|
|
30
|
+
}
|
|
13
31
|
async function main(argv) {
|
|
14
|
-
|
|
15
|
-
|
|
32
|
+
const checking = check(argv);
|
|
33
|
+
if (checking === undefined) {
|
|
34
|
+
process.stderr.write(USAGE);
|
|
16
35
|
return COULD_NOT_RUN;
|
|
17
36
|
}
|
|
18
|
-
const report = await
|
|
37
|
+
const report = await checking;
|
|
19
38
|
await new Promise((done) => writeReport(`${JSON.stringify(report)}\n`, () => done()));
|
|
20
39
|
return report.passed ? 0 : 1;
|
|
21
40
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { JsonValue } from "../../../domain/valueobjects/wire.ts";
|
|
2
2
|
import type { FoundHandle, WritableHandle } from "../../../domain/entities/handle-state.ts";
|
|
3
3
|
import type { PendingDirective } from "../pending-directives/render.ts";
|
|
4
|
+
import type { GlobOptions } from "../../../domain/services/define-dialect.ts";
|
|
4
5
|
/**
|
|
5
6
|
* Locates an existing file and returns a `FoundHandle` for reading or removing it.
|
|
6
7
|
*
|
|
@@ -17,6 +18,20 @@ import type { PendingDirective } from "../pending-directives/render.ts";
|
|
|
17
18
|
* else replaceContent("src/config.ts", patch(c));
|
|
18
19
|
*/
|
|
19
20
|
export declare function find(path: string): FoundHandle;
|
|
21
|
+
/**
|
|
22
|
+
* Opens every file `pattern` matches as a `FoundHandle`, ordered by path, leaving out every file
|
|
23
|
+
* an `ignore` pattern matches; resolves `[]` when none matches (A-130). `*` matches within one
|
|
24
|
+
* path segment, `?` one character, `**` any number of segments and `{a,b}` either alternative;
|
|
25
|
+
* every other character is literal, so `ignore` is the only way to leave files out. It sees the
|
|
26
|
+
* run (A-131): a file created earlier is listed, one removed earlier is not. Unlike `find`, it
|
|
27
|
+
* is asynchronous, because it lists files first.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* for (const file of await glob("src/domain/**", { ignore: ["**\/*.test.ts"] })) {
|
|
31
|
+
* const content = await file.read();
|
|
32
|
+
* }
|
|
33
|
+
*/
|
|
34
|
+
export declare function glob(pattern: string, options?: GlobOptions): Promise<FoundHandle[]>;
|
|
20
35
|
export declare function create(path: string, opts: {
|
|
21
36
|
options: JsonValue | undefined;
|
|
22
37
|
force?: boolean;
|
|
@@ -86,6 +86,23 @@ function buildFoundHandle(path) {
|
|
|
86
86
|
export function find(path) {
|
|
87
87
|
return buildFoundHandle(path);
|
|
88
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Opens every file `pattern` matches as a `FoundHandle`, ordered by path, leaving out every file
|
|
91
|
+
* an `ignore` pattern matches; resolves `[]` when none matches (A-130). `*` matches within one
|
|
92
|
+
* path segment, `?` one character, `**` any number of segments and `{a,b}` either alternative;
|
|
93
|
+
* every other character is literal, so `ignore` is the only way to leave files out. It sees the
|
|
94
|
+
* run (A-131): a file created earlier is listed, one removed earlier is not. Unlike `find`, it
|
|
95
|
+
* is asynchronous, because it lists files first.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* for (const file of await glob("src/domain/**", { ignore: ["**\/*.test.ts"] })) {
|
|
99
|
+
* const content = await file.read();
|
|
100
|
+
* }
|
|
101
|
+
*/
|
|
102
|
+
export async function glob(pattern, options) {
|
|
103
|
+
const paths = await currentContext().session.list(pattern, options?.ignore);
|
|
104
|
+
return paths.map(buildFoundHandle);
|
|
105
|
+
}
|
|
89
106
|
// `template` receives the defaulted options and runs after the active run is resolved, so a
|
|
90
107
|
// call outside a run fails before any template file is read.
|
|
91
108
|
export function create(path, opts, template) {
|
|
@@ -27,6 +27,14 @@ export declare function requirePackageAnchors(missingAnchorMessage: string): {
|
|
|
27
27
|
packageDir: string;
|
|
28
28
|
files: PackageFiles;
|
|
29
29
|
};
|
|
30
|
+
/** What a run of a wrapped factory receives besides its input. */
|
|
31
|
+
export interface RunDeps {
|
|
32
|
+
client: EngineClient;
|
|
33
|
+
/** The role of the schematic (CA-14), as the runner received it; only `evaluation` changes the run. */
|
|
34
|
+
role?: string;
|
|
35
|
+
/** The qualified name `collection:schematic` of the schematic, as the runner received it. */
|
|
36
|
+
name?: string;
|
|
37
|
+
}
|
|
30
38
|
/**
|
|
31
39
|
* Builds a SessionContext and runs the factory inside it, flushing buffered
|
|
32
40
|
* directives at run end (REQ-KIT-05) — a write-only factory still emits.
|
|
@@ -46,7 +54,10 @@ export declare function requirePackageAnchors(missingAnchorMessage: string): {
|
|
|
46
54
|
* Author code never imports this directly; it calls `runFactoryForTest` (test-time) or the
|
|
47
55
|
* future production runner instead — both wrap this SAME seam internally.
|
|
48
56
|
*
|
|
49
|
-
* @param fn - the authoring logic; receives the resolved, schema-validated input
|
|
57
|
+
* @param fn - the authoring logic; receives the resolved, schema-validated input and returns
|
|
58
|
+
* the schematic's Result, or nothing when its `schema.json` declares none (A-14). The Result
|
|
59
|
+
* is checked against that declaration before the flush; the runner resolves to the committed
|
|
60
|
+
* copy. A bare `defineFactory(fn)` declares no Result, so `fn` must return nothing.
|
|
50
61
|
* @param options.packageFiles - the package-files adapter the two reads go through; every
|
|
51
62
|
* caller passes it with `packageDir`.
|
|
52
63
|
* @param options.packageDir - opts into schema-derived input validation and reserved-name
|
|
@@ -78,8 +89,15 @@ export declare function requirePackageAnchors(missingAnchorMessage: string): {
|
|
|
78
89
|
*
|
|
79
90
|
* const runner = defineFactory<Input>(bareFactory, { packageDir: import.meta.dir });
|
|
80
91
|
* await runner(input, { client });
|
|
92
|
+
*
|
|
93
|
+
* The returned runner takes, besides the client, the `role` and the qualified `name` of the
|
|
94
|
+
* schematic it runs, as the caller received them (CA-19). With the role `evaluation`, `fn`
|
|
95
|
+
* returns its Findings, checked against the shape the role imposes, each finding's `source` is
|
|
96
|
+
* set to `name`, and `fn` receives the scope inputs `include` and `exclude`; every `glob` result
|
|
97
|
+
* is narrowed to the scope and so is every finding with a path (A-120 to A-124). Any other role,
|
|
98
|
+
* or none, runs as a generator.
|
|
81
99
|
*/
|
|
82
|
-
export declare function defineFactory<O>(fn: (o: O) =>
|
|
100
|
+
export declare function defineFactory<O, R = void>(fn: (o: O) => R | Promise<R>, options?: ({
|
|
83
101
|
packageDir: string | URL;
|
|
84
102
|
packageFiles: PackageFiles;
|
|
85
103
|
} | {
|
|
@@ -87,6 +105,4 @@ export declare function defineFactory<O>(fn: (o: O) => void | Promise<void>, opt
|
|
|
87
105
|
packageFiles?: undefined;
|
|
88
106
|
}) & {
|
|
89
107
|
declaredDialects?: readonly string[];
|
|
90
|
-
}): (o: O, deps:
|
|
91
|
-
client: EngineClient;
|
|
92
|
-
}) => Promise<void>;
|
|
108
|
+
}): (o: O, deps: RunDeps) => Promise<R>;
|