@pbuilder/sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +202 -0
- package/dist/bin/pbuilder-codegen.js +506 -0
- package/dist/bin/pbuilder-runner.d.ts +1 -0
- package/dist/bin/pbuilder-runner.js +47 -0
- package/dist/commons/classify-content.d.ts +40 -0
- package/dist/commons/classify-content.js +32 -0
- package/dist/commons/index.d.ts +271 -0
- package/dist/commons/index.js +276 -0
- package/dist/conformance/index.d.ts +115 -0
- package/dist/conformance/index.js +218 -0
- package/dist/conformance/run-vehicle.d.ts +19 -0
- package/dist/conformance/run-vehicle.js +104 -0
- package/dist/core/authoring-error.d.ts +116 -0
- package/dist/core/authoring-error.js +205 -0
- package/dist/core/base-handle.d.ts +17 -0
- package/dist/core/base-handle.js +4 -0
- package/dist/core/context.d.ts +85 -0
- package/dist/core/context.js +356 -0
- package/dist/core/deep-equal.d.ts +1 -0
- package/dist/core/deep-equal.js +29 -0
- package/dist/core/define-dialect.d.ts +94 -0
- package/dist/core/define-dialect.js +121 -0
- package/dist/core/dialect-error.d.ts +8 -0
- package/dist/core/dialect-error.js +26 -0
- package/dist/core/dialect-handle.d.ts +21 -0
- package/dist/core/dialect-handle.js +382 -0
- package/dist/core/directive-factory.d.ts +47 -0
- package/dist/core/directive-factory.js +154 -0
- package/dist/core/emit-rejection.d.ts +19 -0
- package/dist/core/emit-rejection.js +31 -0
- package/dist/core/engine-client.d.ts +7 -0
- package/dist/core/engine-client.js +5 -0
- package/dist/core/fs-errors.d.ts +1 -0
- package/dist/core/fs-errors.js +5 -0
- package/dist/core/handle-state.d.ts +24 -0
- package/dist/core/handle-state.js +4 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.js +6 -0
- package/dist/core/jsx-name-validator.d.ts +19 -0
- package/dist/core/jsx-name-validator.js +104 -0
- package/dist/core/reject-tail.d.ts +23 -0
- package/dist/core/reject-tail.js +63 -0
- package/dist/core/schema/input-rejection.d.ts +4 -0
- package/dist/core/schema/input-rejection.js +32 -0
- package/dist/core/schema/schema-digest.d.ts +1 -0
- package/dist/core/schema/schema-digest.js +7 -0
- package/dist/core/schema/schema-discovery.d.ts +13 -0
- package/dist/core/schema/schema-discovery.js +51 -0
- package/dist/core/schema/schema-locate.d.ts +10 -0
- package/dist/core/schema/schema-locate.js +192 -0
- package/dist/core/schema/schema-model.d.ts +18 -0
- package/dist/core/schema/schema-model.js +15 -0
- package/dist/core/schema/schema-parse.d.ts +20 -0
- package/dist/core/schema/schema-parse.js +80 -0
- package/dist/core/schema/schema-sufficiency.d.ts +17 -0
- package/dist/core/schema/schema-sufficiency.js +54 -0
- package/dist/core/schema/schema-validate.d.ts +14 -0
- package/dist/core/schema/schema-validate.js +64 -0
- package/dist/core/session.d.ts +13 -0
- package/dist/core/session.js +72 -0
- package/dist/core/wire.d.ts +87 -0
- package/dist/core/wire.js +49 -0
- package/dist/dialects/react/ast.d.ts +25 -0
- package/dist/dialects/react/ast.js +80 -0
- package/dist/dialects/react/index.d.ts +39 -0
- package/dist/dialects/react/index.js +71 -0
- package/dist/dialects/react/ops.d.ts +56 -0
- package/dist/dialects/react/ops.js +186 -0
- package/dist/dialects/typescript/ast.d.ts +42 -0
- package/dist/dialects/typescript/ast.js +93 -0
- package/dist/dialects/typescript/index.d.ts +32 -0
- package/dist/dialects/typescript/index.js +40 -0
- package/dist/dialects/typescript/ops.d.ts +123 -0
- package/dist/dialects/typescript/ops.js +365 -0
- package/dist/dry-run/index.d.ts +2 -0
- package/dist/dry-run/index.js +1 -0
- package/dist/dry-run/plan.d.ts +69 -0
- package/dist/dry-run/plan.js +44 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/runner-manifest.json +104 -0
- package/dist/scaffold/classify-transport.d.ts +57 -0
- package/dist/scaffold/classify-transport.js +123 -0
- package/dist/scaffold/containment.d.ts +54 -0
- package/dist/scaffold/containment.js +242 -0
- package/dist/scaffold/expander.d.ts +30 -0
- package/dist/scaffold/expander.js +161 -0
- package/dist/scaffold/filename-pipeline.d.ts +39 -0
- package/dist/scaffold/filename-pipeline.js +125 -0
- package/dist/scaffold/index.d.ts +45 -0
- package/dist/scaffold/index.js +99 -0
- package/dist/scaffold/walk.d.ts +25 -0
- package/dist/scaffold/walk.js +124 -0
- package/dist/testing/contract-fake.d.ts +17 -0
- package/dist/testing/contract-fake.js +221 -0
- package/dist/testing/index.d.ts +84 -0
- package/dist/testing/index.js +82 -0
- package/dist/testing/rejection-messages.d.ts +7 -0
- package/dist/testing/rejection-messages.js +12 -0
- package/dist/transport/bootstrap-bridge.d.ts +33 -0
- package/dist/transport/bootstrap-bridge.js +65 -0
- package/dist/transport/error-text.d.ts +8 -0
- package/dist/transport/error-text.js +44 -0
- package/dist/transport/exit-codes.d.ts +2 -0
- package/dist/transport/exit-codes.js +35 -0
- package/dist/transport/factory-pointer.d.ts +32 -0
- package/dist/transport/factory-pointer.js +63 -0
- package/dist/transport/frame-reader.d.ts +10 -0
- package/dist/transport/frame-reader.js +81 -0
- package/dist/transport/framing.d.ts +20 -0
- package/dist/transport/framing.js +98 -0
- package/dist/transport/runner.d.ts +25 -0
- package/dist/transport/runner.js +308 -0
- package/dist/transport/single-instance-probe.d.ts +23 -0
- package/dist/transport/single-instance-probe.js +119 -0
- package/dist/transport/stdio-engine-client.d.ts +27 -0
- package/dist/transport/stdio-engine-client.js +243 -0
- package/dist/transport/wire-protocol.d.ts +33 -0
- package/dist/transport/wire-protocol.js +22 -0
- package/package.json +93 -0
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
4
|
+
|
|
5
|
+
// bin/pbuilder-codegen.ts
|
|
6
|
+
import { readFileSync, writeFileSync, existsSync, realpathSync } from "node:fs";
|
|
7
|
+
import { join as join2, dirname, basename, resolve, sep } from "node:path";
|
|
8
|
+
|
|
9
|
+
// src/core/schema/schema-model.ts
|
|
10
|
+
var FORBIDDEN_KEYS = new Set(["__proto__", "constructor", "prototype"]);
|
|
11
|
+
function isPlainObject(value) {
|
|
12
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/core/schema/schema-locate.ts
|
|
16
|
+
function ok(next) {
|
|
17
|
+
return { ok: true, next };
|
|
18
|
+
}
|
|
19
|
+
function fail(offset) {
|
|
20
|
+
return { ok: false, offset };
|
|
21
|
+
}
|
|
22
|
+
function isWhitespace(ch) {
|
|
23
|
+
return ch === " " || ch === "\t" || ch === `
|
|
24
|
+
` || ch === "\r";
|
|
25
|
+
}
|
|
26
|
+
function isDigit(ch) {
|
|
27
|
+
return ch !== undefined && ch >= "0" && ch <= "9";
|
|
28
|
+
}
|
|
29
|
+
function skipWhitespace(raw, i) {
|
|
30
|
+
let pos = i;
|
|
31
|
+
while (pos < raw.length && isWhitespace(raw[pos]))
|
|
32
|
+
pos++;
|
|
33
|
+
return pos;
|
|
34
|
+
}
|
|
35
|
+
function scanString(raw, i) {
|
|
36
|
+
let pos = i + 1;
|
|
37
|
+
while (pos < raw.length) {
|
|
38
|
+
const ch = raw[pos];
|
|
39
|
+
if (ch === '"')
|
|
40
|
+
return ok(pos + 1);
|
|
41
|
+
if (ch === "\\") {
|
|
42
|
+
pos += 2;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
pos++;
|
|
46
|
+
}
|
|
47
|
+
return fail(pos);
|
|
48
|
+
}
|
|
49
|
+
function scanNumber(raw, i) {
|
|
50
|
+
let pos = i;
|
|
51
|
+
if (raw[pos] === "-")
|
|
52
|
+
pos++;
|
|
53
|
+
if (!isDigit(raw[pos]))
|
|
54
|
+
return fail(pos);
|
|
55
|
+
if (raw[pos] === "0") {
|
|
56
|
+
pos++;
|
|
57
|
+
} else {
|
|
58
|
+
while (isDigit(raw[pos]))
|
|
59
|
+
pos++;
|
|
60
|
+
}
|
|
61
|
+
if (raw[pos] === ".") {
|
|
62
|
+
pos++;
|
|
63
|
+
if (!isDigit(raw[pos]))
|
|
64
|
+
return fail(pos);
|
|
65
|
+
while (isDigit(raw[pos]))
|
|
66
|
+
pos++;
|
|
67
|
+
}
|
|
68
|
+
if (raw[pos] === "e" || raw[pos] === "E") {
|
|
69
|
+
pos++;
|
|
70
|
+
if (raw[pos] === "+" || raw[pos] === "-")
|
|
71
|
+
pos++;
|
|
72
|
+
if (!isDigit(raw[pos]))
|
|
73
|
+
return fail(pos);
|
|
74
|
+
while (isDigit(raw[pos]))
|
|
75
|
+
pos++;
|
|
76
|
+
}
|
|
77
|
+
return ok(pos);
|
|
78
|
+
}
|
|
79
|
+
function scanObject(raw, i) {
|
|
80
|
+
let pos = skipWhitespace(raw, i + 1);
|
|
81
|
+
if (pos >= raw.length)
|
|
82
|
+
return fail(pos);
|
|
83
|
+
if (raw[pos] === "}")
|
|
84
|
+
return ok(pos + 1);
|
|
85
|
+
for (;; ) {
|
|
86
|
+
pos = skipWhitespace(raw, pos);
|
|
87
|
+
if (pos >= raw.length)
|
|
88
|
+
return fail(pos);
|
|
89
|
+
if (raw[pos] !== '"')
|
|
90
|
+
return fail(pos);
|
|
91
|
+
const key = scanString(raw, pos);
|
|
92
|
+
if (!key.ok)
|
|
93
|
+
return key;
|
|
94
|
+
pos = skipWhitespace(raw, key.next);
|
|
95
|
+
if (pos >= raw.length)
|
|
96
|
+
return fail(pos);
|
|
97
|
+
if (raw[pos] !== ":")
|
|
98
|
+
return fail(pos);
|
|
99
|
+
const value = scanValue(raw, pos + 1);
|
|
100
|
+
if (!value.ok)
|
|
101
|
+
return value;
|
|
102
|
+
pos = skipWhitespace(raw, value.next);
|
|
103
|
+
if (pos >= raw.length)
|
|
104
|
+
return fail(pos);
|
|
105
|
+
if (raw[pos] === ",") {
|
|
106
|
+
pos = skipWhitespace(raw, pos + 1);
|
|
107
|
+
if (pos >= raw.length)
|
|
108
|
+
return fail(pos);
|
|
109
|
+
if (raw[pos] === "}")
|
|
110
|
+
return fail(pos);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (raw[pos] === "}")
|
|
114
|
+
return ok(pos + 1);
|
|
115
|
+
return fail(pos);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function scanArray(raw, i) {
|
|
119
|
+
let pos = skipWhitespace(raw, i + 1);
|
|
120
|
+
if (pos >= raw.length)
|
|
121
|
+
return fail(pos);
|
|
122
|
+
if (raw[pos] === "]")
|
|
123
|
+
return ok(pos + 1);
|
|
124
|
+
for (;; ) {
|
|
125
|
+
const value = scanValue(raw, pos);
|
|
126
|
+
if (!value.ok)
|
|
127
|
+
return value;
|
|
128
|
+
pos = skipWhitespace(raw, value.next);
|
|
129
|
+
if (pos >= raw.length)
|
|
130
|
+
return fail(pos);
|
|
131
|
+
if (raw[pos] === ",") {
|
|
132
|
+
pos = skipWhitespace(raw, pos + 1);
|
|
133
|
+
if (pos >= raw.length)
|
|
134
|
+
return fail(pos);
|
|
135
|
+
if (raw[pos] === "]")
|
|
136
|
+
return fail(pos);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (raw[pos] === "]")
|
|
140
|
+
return ok(pos + 1);
|
|
141
|
+
return fail(pos);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function scanValue(raw, i0) {
|
|
145
|
+
const i = skipWhitespace(raw, i0);
|
|
146
|
+
if (i >= raw.length)
|
|
147
|
+
return fail(i);
|
|
148
|
+
const ch = raw[i];
|
|
149
|
+
if (ch === "{")
|
|
150
|
+
return scanObject(raw, i);
|
|
151
|
+
if (ch === "[")
|
|
152
|
+
return scanArray(raw, i);
|
|
153
|
+
if (ch === '"')
|
|
154
|
+
return scanString(raw, i);
|
|
155
|
+
if (ch === "-" || isDigit(ch))
|
|
156
|
+
return scanNumber(raw, i);
|
|
157
|
+
if (raw.startsWith("true", i))
|
|
158
|
+
return ok(i + 4);
|
|
159
|
+
if (raw.startsWith("false", i))
|
|
160
|
+
return ok(i + 5);
|
|
161
|
+
if (raw.startsWith("null", i))
|
|
162
|
+
return ok(i + 4);
|
|
163
|
+
return fail(i);
|
|
164
|
+
}
|
|
165
|
+
function offsetToLineColumn(raw, offset) {
|
|
166
|
+
let line = 1;
|
|
167
|
+
let lastNewlineAt = -1;
|
|
168
|
+
for (let i = 0;i < offset && i < raw.length; i++) {
|
|
169
|
+
if (raw[i] === `
|
|
170
|
+
`) {
|
|
171
|
+
line++;
|
|
172
|
+
lastNewlineAt = i;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return { line, column: offset - lastNewlineAt };
|
|
176
|
+
}
|
|
177
|
+
function locateFirstJsonSyntaxError(raw) {
|
|
178
|
+
const result = scanValue(raw, 0);
|
|
179
|
+
if (!result.ok) {
|
|
180
|
+
return offsetToLineColumn(raw, result.offset);
|
|
181
|
+
}
|
|
182
|
+
const trailing = skipWhitespace(raw, result.next);
|
|
183
|
+
if (trailing < raw.length) {
|
|
184
|
+
return offsetToLineColumn(raw, trailing);
|
|
185
|
+
}
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// src/core/schema/schema-parse.ts
|
|
190
|
+
class SchemaParseFailure extends Error {
|
|
191
|
+
problem;
|
|
192
|
+
line;
|
|
193
|
+
column;
|
|
194
|
+
constructor(info) {
|
|
195
|
+
super(`schema parse failure: ${info.problem}`);
|
|
196
|
+
this.name = "SchemaParseFailure";
|
|
197
|
+
this.problem = info.problem;
|
|
198
|
+
this.line = info.line;
|
|
199
|
+
this.column = info.column;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function formatLocator(line, column) {
|
|
203
|
+
return line !== undefined && column !== undefined ? `(line ${line}, column ${column})` : "(position unknown)";
|
|
204
|
+
}
|
|
205
|
+
function parseSchema(raw) {
|
|
206
|
+
let parsed;
|
|
207
|
+
try {
|
|
208
|
+
parsed = JSON.parse(raw);
|
|
209
|
+
} catch (err) {
|
|
210
|
+
let locator;
|
|
211
|
+
if (err instanceof SyntaxError) {
|
|
212
|
+
try {
|
|
213
|
+
locator = locateFirstJsonSyntaxError(raw);
|
|
214
|
+
} catch {
|
|
215
|
+
locator = undefined;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
throw new SchemaParseFailure({
|
|
219
|
+
problem: "invalid JSON",
|
|
220
|
+
line: locator?.line,
|
|
221
|
+
column: locator?.column
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
if (!isPlainObject(parsed) || !isPlainObject(parsed.properties)) {
|
|
225
|
+
throw new SchemaParseFailure({
|
|
226
|
+
problem: 'missing "properties" object',
|
|
227
|
+
line: undefined,
|
|
228
|
+
column: undefined
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
const propertiesMap = parsed.properties;
|
|
232
|
+
const properties = [];
|
|
233
|
+
for (const key of Object.keys(propertiesMap)) {
|
|
234
|
+
const value = propertiesMap[key];
|
|
235
|
+
if (!isPlainObject(value))
|
|
236
|
+
continue;
|
|
237
|
+
properties.push({
|
|
238
|
+
key,
|
|
239
|
+
type: value.type,
|
|
240
|
+
label: typeof value.label === "string" ? value.label : undefined,
|
|
241
|
+
choices: Array.isArray(value.choices) ? value.choices : undefined,
|
|
242
|
+
default: value.default,
|
|
243
|
+
required: typeof value.required === "boolean" ? value.required : undefined,
|
|
244
|
+
description: typeof value.description === "string" ? value.description : undefined
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return { properties };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// src/core/schema/schema-digest.ts
|
|
251
|
+
import { createHash } from "node:crypto";
|
|
252
|
+
function computeSchemaDigest(raw) {
|
|
253
|
+
return createHash("sha256").update(raw, "utf8").digest("hex");
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// src/core/schema/schema-discovery.ts
|
|
257
|
+
import { join } from "node:path";
|
|
258
|
+
|
|
259
|
+
// src/core/fs-errors.ts
|
|
260
|
+
function isErrnoException(err) {
|
|
261
|
+
return err instanceof Error && "code" in err;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// src/core/schema/schema-discovery.ts
|
|
265
|
+
var SCHEMA_FILENAME = "schema.json";
|
|
266
|
+
function schemaPathFor(packageDir) {
|
|
267
|
+
return join(packageDir, SCHEMA_FILENAME);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// src/core/schema/schema-sufficiency.ts
|
|
271
|
+
var RECOGNIZED_KINDS = new Set(["string", "number", "boolean", "enum"]);
|
|
272
|
+
function checkSufficiency(raw) {
|
|
273
|
+
const parsed = JSON.parse(raw);
|
|
274
|
+
const findings = [];
|
|
275
|
+
if (!isPlainObject(parsed) || !isPlainObject(parsed.properties)) {
|
|
276
|
+
return findings;
|
|
277
|
+
}
|
|
278
|
+
const propertiesMap = parsed.properties;
|
|
279
|
+
for (const key of Object.keys(propertiesMap)) {
|
|
280
|
+
if (FORBIDDEN_KEYS.has(key)) {
|
|
281
|
+
findings.push({ key, reason: "forbidden-key" });
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
const value = propertiesMap[key];
|
|
285
|
+
if (!isPlainObject(value))
|
|
286
|
+
continue;
|
|
287
|
+
const type = value.type;
|
|
288
|
+
if (type === undefined) {
|
|
289
|
+
findings.push({ key, reason: "missing-type" });
|
|
290
|
+
} else if (typeof type !== "string" || !RECOGNIZED_KINDS.has(type)) {
|
|
291
|
+
findings.push({ key, reason: "nonsensical-type", detail: String(type) });
|
|
292
|
+
} else if (type === "enum" && (!Array.isArray(value.choices) || value.choices.length === 0)) {
|
|
293
|
+
findings.push({ key, reason: "enum-missing-choices" });
|
|
294
|
+
}
|
|
295
|
+
if (typeof value.label !== "string") {
|
|
296
|
+
findings.push({ key, reason: "missing-label" });
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return findings;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// bin/emit-type.ts
|
|
303
|
+
var GENERATED_HEADER = "// AUTO-GENERATED by pbuilder-codegen — do not edit. Regenerate: pbuilder-codegen <package-dir>";
|
|
304
|
+
var IDENTIFIER_RE = /^[A-Za-z_$][\w$]*$/;
|
|
305
|
+
|
|
306
|
+
class UnrecognizedPropertyTypeError extends Error {
|
|
307
|
+
field;
|
|
308
|
+
constructor(field) {
|
|
309
|
+
super(`unrecognized property type for "${field}"`);
|
|
310
|
+
this.name = "UnrecognizedPropertyTypeError";
|
|
311
|
+
this.field = field;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
function emitPropertyKey(key) {
|
|
315
|
+
return IDENTIFIER_RE.test(key) ? key : JSON.stringify(key);
|
|
316
|
+
}
|
|
317
|
+
function emitPropertyType(property) {
|
|
318
|
+
if (property.type === "enum") {
|
|
319
|
+
const choices = property.choices ?? [];
|
|
320
|
+
return choices.map((choice) => JSON.stringify(choice)).join(" | ");
|
|
321
|
+
}
|
|
322
|
+
if (!RECOGNIZED_KINDS.has(property.type)) {
|
|
323
|
+
throw new UnrecognizedPropertyTypeError(property.key);
|
|
324
|
+
}
|
|
325
|
+
return property.type;
|
|
326
|
+
}
|
|
327
|
+
function sanitizeLabelForComment(label) {
|
|
328
|
+
return label.replace(/\*\//g, "*\\/").replace(/[\r\n]+/g, " ");
|
|
329
|
+
}
|
|
330
|
+
function emitProperty(property) {
|
|
331
|
+
const key = emitPropertyKey(property.key);
|
|
332
|
+
const optional = property.required === false ? "?" : "";
|
|
333
|
+
const type = emitPropertyType(property);
|
|
334
|
+
const doc = property.label !== undefined ? ` /** ${sanitizeLabelForComment(property.label)} */
|
|
335
|
+
` : "";
|
|
336
|
+
return `${doc} ${key}${optional}: ${type};`;
|
|
337
|
+
}
|
|
338
|
+
function emitInputType(schema, digest) {
|
|
339
|
+
const members = schema.properties.map(emitProperty).join(`
|
|
340
|
+
`);
|
|
341
|
+
return [
|
|
342
|
+
GENERATED_HEADER,
|
|
343
|
+
`// @schema-digest sha256:${digest}`,
|
|
344
|
+
"",
|
|
345
|
+
"export type Input = {",
|
|
346
|
+
members,
|
|
347
|
+
"};",
|
|
348
|
+
""
|
|
349
|
+
].join(`
|
|
350
|
+
`);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// bin/pbuilder-codegen.ts
|
|
354
|
+
var GENERATED_FILENAME = "schema.generated.ts";
|
|
355
|
+
var SUCCESS_LINE = "pbuilder-codegen: wrote schema.generated.ts";
|
|
356
|
+
var USAGE = [
|
|
357
|
+
"pbuilder-codegen: derive a TypeScript Input type from a factory's schema.json",
|
|
358
|
+
"",
|
|
359
|
+
"Usage: pbuilder-codegen <package-dir>"
|
|
360
|
+
].join(`
|
|
361
|
+
`);
|
|
362
|
+
|
|
363
|
+
class SchemaSufficiencyFailure extends Error {
|
|
364
|
+
findings;
|
|
365
|
+
constructor(findings) {
|
|
366
|
+
super(`schema sufficiency failure: ${findings.length} finding(s)`);
|
|
367
|
+
this.name = "SchemaSufficiencyFailure";
|
|
368
|
+
this.findings = findings;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
function generateSchema(packageDir) {
|
|
372
|
+
const schemaPath = schemaPathFor(packageDir);
|
|
373
|
+
const raw = readFileSync(schemaPath, "utf-8");
|
|
374
|
+
const schema = parseSchema(raw);
|
|
375
|
+
const sufficiencyFindings = checkSufficiency(raw);
|
|
376
|
+
if (sufficiencyFindings.length > 0) {
|
|
377
|
+
throw new SchemaSufficiencyFailure(sufficiencyFindings);
|
|
378
|
+
}
|
|
379
|
+
const digest = computeSchemaDigest(raw);
|
|
380
|
+
const output = emitInputType(schema, digest);
|
|
381
|
+
const outputPath = join2(packageDir, GENERATED_FILENAME);
|
|
382
|
+
writeFileSync(outputPath, output, "utf-8");
|
|
383
|
+
return { outputPath, digest };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
class WriteContainmentRefusal extends Error {
|
|
387
|
+
constructor(message) {
|
|
388
|
+
super(message);
|
|
389
|
+
this.name = "WriteContainmentRefusal";
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
function findProjectRoot(startDir) {
|
|
393
|
+
let dir = startDir;
|
|
394
|
+
for (;; ) {
|
|
395
|
+
if (existsSync(join2(dir, "package.json")))
|
|
396
|
+
return dir;
|
|
397
|
+
const parent = dirname(dir);
|
|
398
|
+
if (parent === dir)
|
|
399
|
+
return startDir;
|
|
400
|
+
dir = parent;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
function realpathNearestExisting(p) {
|
|
404
|
+
let current = p;
|
|
405
|
+
const suffix = [];
|
|
406
|
+
while (!existsSync(current)) {
|
|
407
|
+
const parent = dirname(current);
|
|
408
|
+
if (parent === current)
|
|
409
|
+
break;
|
|
410
|
+
suffix.unshift(basename(current));
|
|
411
|
+
current = parent;
|
|
412
|
+
}
|
|
413
|
+
const real = realpathSync(current);
|
|
414
|
+
return suffix.length > 0 ? join2(real, ...suffix) : real;
|
|
415
|
+
}
|
|
416
|
+
function isWithin(anchor, target) {
|
|
417
|
+
return target === anchor || target.startsWith(anchor + sep);
|
|
418
|
+
}
|
|
419
|
+
function assertWriteContained(packageDirArg, outputPath) {
|
|
420
|
+
const anchor = realpathSync(findProjectRoot(process.cwd()));
|
|
421
|
+
const resolvedPackageDir = realpathNearestExisting(resolve(packageDirArg));
|
|
422
|
+
const resolvedOutputPath = realpathNearestExisting(resolve(outputPath));
|
|
423
|
+
if (!isWithin(anchor, resolvedPackageDir) || !isWithin(anchor, resolvedOutputPath)) {
|
|
424
|
+
throw new WriteContainmentRefusal(`pbuilder-codegen: refusing to write outside the project root: "${packageDirArg}"`);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
function formatParseError(schemaPath, err) {
|
|
428
|
+
return `pbuilder-codegen: ${schemaPath}: ${err.problem} ${formatLocator(err.line, err.column)}`;
|
|
429
|
+
}
|
|
430
|
+
function describeSufficiencyFinding(finding) {
|
|
431
|
+
switch (finding.reason) {
|
|
432
|
+
case "forbidden-key":
|
|
433
|
+
return `property "${finding.key}" uses a forbidden key`;
|
|
434
|
+
case "missing-type":
|
|
435
|
+
return `property "${finding.key}" is missing a type`;
|
|
436
|
+
case "nonsensical-type":
|
|
437
|
+
return `unrecognized property type for "${finding.key}"`;
|
|
438
|
+
case "enum-missing-choices":
|
|
439
|
+
return `enum property "${finding.key}" is missing choices`;
|
|
440
|
+
case "missing-label":
|
|
441
|
+
return `property "${finding.key}" is missing a label`;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
function formatSufficiencyError(schemaPath, err) {
|
|
445
|
+
const reasons = err.findings.map(describeSufficiencyFinding).join("; ");
|
|
446
|
+
return `pbuilder-codegen: ${schemaPath}: ${reasons}`;
|
|
447
|
+
}
|
|
448
|
+
function runCli(argv) {
|
|
449
|
+
const [first] = argv;
|
|
450
|
+
if (first === undefined) {
|
|
451
|
+
console.error(USAGE);
|
|
452
|
+
return 1;
|
|
453
|
+
}
|
|
454
|
+
if (first === "--help" || first === "-h") {
|
|
455
|
+
console.log(USAGE);
|
|
456
|
+
return 0;
|
|
457
|
+
}
|
|
458
|
+
if (first.startsWith("-")) {
|
|
459
|
+
console.error(USAGE);
|
|
460
|
+
return 1;
|
|
461
|
+
}
|
|
462
|
+
const packageDir = first;
|
|
463
|
+
const outputPath = join2(packageDir, GENERATED_FILENAME);
|
|
464
|
+
const schemaPath = schemaPathFor(packageDir);
|
|
465
|
+
try {
|
|
466
|
+
assertWriteContained(packageDir, outputPath);
|
|
467
|
+
} catch (err) {
|
|
468
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
469
|
+
return 1;
|
|
470
|
+
}
|
|
471
|
+
try {
|
|
472
|
+
generateSchema(packageDir);
|
|
473
|
+
} catch (err) {
|
|
474
|
+
if (err instanceof SchemaParseFailure) {
|
|
475
|
+
console.error(formatParseError(schemaPath, err));
|
|
476
|
+
return 1;
|
|
477
|
+
}
|
|
478
|
+
if (err instanceof SchemaSufficiencyFailure) {
|
|
479
|
+
console.error(formatSufficiencyError(schemaPath, err));
|
|
480
|
+
return 1;
|
|
481
|
+
}
|
|
482
|
+
if (err instanceof UnrecognizedPropertyTypeError) {
|
|
483
|
+
console.error(`pbuilder-codegen: ${schemaPath}: ${err.message}`);
|
|
484
|
+
return 1;
|
|
485
|
+
}
|
|
486
|
+
if (isErrnoException(err) && err.code === "ENOENT") {
|
|
487
|
+
console.error(`pbuilder-codegen: ${schemaPath}: schema.json not found`);
|
|
488
|
+
return 1;
|
|
489
|
+
}
|
|
490
|
+
throw err;
|
|
491
|
+
}
|
|
492
|
+
console.log(SUCCESS_LINE);
|
|
493
|
+
return 0;
|
|
494
|
+
}
|
|
495
|
+
if (__require.main == __require.module) {
|
|
496
|
+
process.exitCode = runCli(process.argv.slice(2));
|
|
497
|
+
}
|
|
498
|
+
export {
|
|
499
|
+
runCli,
|
|
500
|
+
generateSchema,
|
|
501
|
+
WriteContainmentRefusal,
|
|
502
|
+
USAGE,
|
|
503
|
+
SchemaSufficiencyFailure,
|
|
504
|
+
SUCCESS_LINE,
|
|
505
|
+
GENERATED_FILENAME
|
|
506
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Thin argv entry for the engine-spawned runner (design § 4.2). Lives under src/ so tsc
|
|
2
|
+
// emits it to dist/bin/pbuilder-runner.js as a THIN module whose relative imports resolve
|
|
3
|
+
// against the sibling dist/transport/*.js files — never a self-contained bundle, which
|
|
4
|
+
// would ship a second copy of the runner.ts composition root (ADR-01's one-chokepoint
|
|
5
|
+
// invariant holds at the artifact level, not just in source). Provisional/unmapped
|
|
6
|
+
// (ADR-0058): deliberately NO `package.json#bin` entry, in contrast with the mapped
|
|
7
|
+
// `pbuilder-codegen` — the engine spawns this file by absolute path.
|
|
8
|
+
//
|
|
9
|
+
// REQ-RUN-08: the fd-1 frame writer is captured AND console.* is redirected to stderr
|
|
10
|
+
// FIRST, before the composition root can import any factory code — the SAME defence-in-
|
|
11
|
+
// depth guarantee the bridge path gives (BRB-02/03), belt-and-suspenders alongside the
|
|
12
|
+
// engine's own pre-import fd dup (A2). All real work lives in src/transport/runner.ts, the
|
|
13
|
+
// ONE validation-gate chokepoint both this entry and the bridge share (FIT-15 bars
|
|
14
|
+
// src/ -> bin/ imports, so the shared root cannot live here).
|
|
15
|
+
import { runRunner } from "../transport/runner.js";
|
|
16
|
+
import { captureFd1FrameWriter, redirectConsoleToStderr } from "../transport/framing.js";
|
|
17
|
+
import { classifyExitCode } from "../transport/exit-codes.js";
|
|
18
|
+
import { TransportFault } from "../transport/stdio-engine-client.js";
|
|
19
|
+
import { boundMessage } from "../transport/error-text.js";
|
|
20
|
+
if (import.meta.main) {
|
|
21
|
+
const writeFrame = captureFd1FrameWriter();
|
|
22
|
+
redirectConsoleToStderr();
|
|
23
|
+
let exitCode;
|
|
24
|
+
try {
|
|
25
|
+
exitCode = await runRunner(process.argv.slice(2), {
|
|
26
|
+
input: process.stdin,
|
|
27
|
+
writeFrame,
|
|
28
|
+
writeStderr: (text) => {
|
|
29
|
+
process.stderr.write(text);
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
// Terminal catch (judgment-day F4, EXC-01/WPS-07): nothing escaping runRunner may reach
|
|
35
|
+
// Bun's default handler — that prints a raw stack trace with absolute paths and exits 1.
|
|
36
|
+
// Classified + bounded instead; only a TransportFault's own (safe, runner-authored)
|
|
37
|
+
// message is surfaced, anything else gets fixed text.
|
|
38
|
+
const label = err instanceof TransportFault ? err.message : "run failed before completing";
|
|
39
|
+
process.stderr.write(`${boundMessage(`pbuilder-runner: ${label}`)}\n`);
|
|
40
|
+
exitCode = classifyExitCode(err);
|
|
41
|
+
}
|
|
42
|
+
// Explicit exit, not exitCode + natural drain: the host keeps our stdin pipe open, so the
|
|
43
|
+
// still-live stdin reader would otherwise hold the event loop forever. Safe: every
|
|
44
|
+
// outbound frame was written BEFORE its response was awaited, so nothing is left buffered
|
|
45
|
+
// when runRunner resolves.
|
|
46
|
+
process.exit(exitCode);
|
|
47
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The trichotomy of a `find(path).read()` result: `"absent"` (the path does not exist),
|
|
3
|
+
* `"empty"` (the file exists but its content is the exact empty string), or `"present"`
|
|
4
|
+
* (any other string — including falsy-looking ones like `"0"`, `"false"`, or
|
|
5
|
+
* whitespace-only content).
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* switch (classifyContent(await find("src/config.ts").read())) {
|
|
9
|
+
* case "absent":
|
|
10
|
+
* create("src/config.ts", { template, options });
|
|
11
|
+
* break;
|
|
12
|
+
* case "empty":
|
|
13
|
+
* replaceContent("src/config.ts", seedContent);
|
|
14
|
+
* break;
|
|
15
|
+
* case "present":
|
|
16
|
+
* replaceContent("src/config.ts", patch(content));
|
|
17
|
+
* break;
|
|
18
|
+
* }
|
|
19
|
+
*/
|
|
20
|
+
export type ContentState = "absent" | "empty" | "present";
|
|
21
|
+
/**
|
|
22
|
+
* Classifies a `find(path).read()` result into the {@link ContentState} trichotomy.
|
|
23
|
+
* Never throws. Uses strict equality only — `"0"`, `"false"`, and whitespace-only
|
|
24
|
+
* strings all classify as `"present"`; a truthiness check (`if (!content)`) would wrongly
|
|
25
|
+
* merge them with `"absent"`/`"empty"` (ADR-0016).
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* switch (classifyContent(await find("src/config.ts").read())) {
|
|
29
|
+
* case "absent":
|
|
30
|
+
* console.log("no such file yet");
|
|
31
|
+
* break;
|
|
32
|
+
* case "empty":
|
|
33
|
+
* console.log("file exists, no content");
|
|
34
|
+
* break;
|
|
35
|
+
* case "present":
|
|
36
|
+
* console.log("has content");
|
|
37
|
+
* break;
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
export declare function classifyContent(content: string | undefined): ContentState;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// REQ-RT-01/-02/-03 (droppable, CQ-1): `find(path).read()` resolves `string | undefined`
|
|
2
|
+
// and authors branch manually with strict `=== undefined` / `=== ""` comparisons —
|
|
3
|
+
// truthiness-coalescing (`if (!content)`) silently merges `undefined`, `""`, `"0"`, and
|
|
4
|
+
// `"false"` and reintroduces the absent-vs-empty bug ADR-0016 fixed at the port level.
|
|
5
|
+
// This is a success-path helper only: it never throws and is not part of the
|
|
6
|
+
// error-attribution contract (REQ-ERM/-AEC families).
|
|
7
|
+
/**
|
|
8
|
+
* Classifies a `find(path).read()` result into the {@link ContentState} trichotomy.
|
|
9
|
+
* Never throws. Uses strict equality only — `"0"`, `"false"`, and whitespace-only
|
|
10
|
+
* strings all classify as `"present"`; a truthiness check (`if (!content)`) would wrongly
|
|
11
|
+
* merge them with `"absent"`/`"empty"` (ADR-0016).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* switch (classifyContent(await find("src/config.ts").read())) {
|
|
15
|
+
* case "absent":
|
|
16
|
+
* console.log("no such file yet");
|
|
17
|
+
* break;
|
|
18
|
+
* case "empty":
|
|
19
|
+
* console.log("file exists, no content");
|
|
20
|
+
* break;
|
|
21
|
+
* case "present":
|
|
22
|
+
* console.log("has content");
|
|
23
|
+
* break;
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
export function classifyContent(content) {
|
|
27
|
+
if (content === undefined)
|
|
28
|
+
return "absent";
|
|
29
|
+
if (content === "")
|
|
30
|
+
return "empty";
|
|
31
|
+
return "present";
|
|
32
|
+
}
|