@genroc/eval-node 0.0.0-edge.e4b0e52 → 0.0.0-edge.e5b183fc
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/dist/import.js +32 -52
- package/dist/worker.js +3 -3
- package/import.ts +42 -60
- package/package.json +1 -1
- package/worker.ts +5 -5
package/dist/import.js
CHANGED
|
@@ -6,12 +6,10 @@
|
|
|
6
6
|
// Two modes, one binary: "types" writes the declarations an editor needs and returns no
|
|
7
7
|
// code; "build" typechecks and bundles. A separate types hook would mean a second `tsc`
|
|
8
8
|
// over the same project.
|
|
9
|
-
import { spawn } from "node:child_process";
|
|
10
9
|
import { existsSync } from "node:fs";
|
|
11
|
-
import { access,
|
|
10
|
+
import { access, writeFile } from "node:fs/promises";
|
|
12
11
|
import { builtinModules } from "node:module";
|
|
13
|
-
import { dirname, join,
|
|
14
|
-
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { dirname, join, resolve } from "node:path";
|
|
15
13
|
import commonjs from "@rollup/plugin-commonjs";
|
|
16
14
|
import json from "@rollup/plugin-json";
|
|
17
15
|
import { nodeResolve } from "@rollup/plugin-node-resolve";
|
|
@@ -30,11 +28,6 @@ async function exists(path) {
|
|
|
30
28
|
return false;
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
|
-
/** Creates the parent directory, which `.genroc-cache/` relies on: nothing else makes it. */
|
|
34
|
-
async function write(path, content) {
|
|
35
|
-
await mkdir(dirname(path), { recursive: true });
|
|
36
|
-
await writeFile(path, content);
|
|
37
|
-
}
|
|
38
31
|
// ── JSON Schema → TypeScript ───────────────────────────────────────────────────
|
|
39
32
|
/** Only genroc's keyword set is handled; anything else its strict decoder would have
|
|
40
33
|
* refused before this ran (internal/schema, allowedKeywords). */
|
|
@@ -90,15 +83,17 @@ function objectType(s, used) {
|
|
|
90
83
|
const doc = typeof sub.description === "string"
|
|
91
84
|
? ` /** ${sub.description} */\n`
|
|
92
85
|
: "";
|
|
93
|
-
lines.push(`${doc} ${propKey(key)}${required.has(key) ? "" : "?"}: ${tsType(sub, used)};`);
|
|
86
|
+
lines.push(`${doc} ${propKey(key)}${required.has(key) ? "" : "?"}: ${nest(tsType(sub, used))};`);
|
|
94
87
|
}
|
|
95
88
|
if (s.additionalProperties && typeof s.additionalProperties === "object") {
|
|
96
|
-
lines.push(` [key: string]: ${tsType(s.additionalProperties, used)};`);
|
|
89
|
+
lines.push(` [key: string]: ${nest(tsType(s.additionalProperties, used))};`);
|
|
97
90
|
}
|
|
98
91
|
if (lines.length === 0)
|
|
99
92
|
return "Record<string, unknown>";
|
|
100
93
|
return `{\n${lines.join("\n")}\n}`;
|
|
101
94
|
}
|
|
95
|
+
/** Every line but the first, which is already placed by the property that opens it. */
|
|
96
|
+
const nest = (t) => t.replace(/\n/g, "\n ");
|
|
102
97
|
function union(parts) {
|
|
103
98
|
const seen = [...new Set(parts)];
|
|
104
99
|
return seen.length === 0 ? "unknown" : seen.join(" | ");
|
|
@@ -106,13 +101,6 @@ function union(parts) {
|
|
|
106
101
|
const IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
107
102
|
const propKey = (k) => (IDENT.test(k) ? k : JSON.stringify(k));
|
|
108
103
|
const identifier = (n) => IDENT.test(n) ? n : `Def_${n.replace(/[^A-Za-z0-9_$]/g, "_")}`;
|
|
109
|
-
function deref(s, defs) {
|
|
110
|
-
let cur = s;
|
|
111
|
-
for (let i = 0; cur && typeof cur.$ref === "string" && i < 16; i++) {
|
|
112
|
-
cur = defs[cur.$ref.replace(/^#\/\$defs\//, "")];
|
|
113
|
-
}
|
|
114
|
-
return cur;
|
|
115
|
-
}
|
|
116
104
|
/** Emits one named type per reachable $def rather than inlining: a task output may
|
|
117
105
|
* reference itself (specs/recursive-type-inference.md) and inlining would not terminate. */
|
|
118
106
|
function declarations(at) {
|
|
@@ -162,11 +150,7 @@ async function nearestTsconfig(from, root) {
|
|
|
162
150
|
}
|
|
163
151
|
}
|
|
164
152
|
async function typecheck(sites) {
|
|
165
|
-
//
|
|
166
|
-
// coexist with it. The suffix is what keeps the scratch area out of its way.
|
|
167
|
-
const dir = join(root, ".genroc-cache");
|
|
168
|
-
await write(join(dir, ".gitignore"), "*\n");
|
|
169
|
-
// One tsc per distinct base config: `extends` takes a single base, so merging two would
|
|
153
|
+
// One program per distinct base config: `extends` takes a single base, so merging two would
|
|
170
154
|
// check each script under the other author's options.
|
|
171
155
|
const groups = new Map();
|
|
172
156
|
for (const at of sites) {
|
|
@@ -177,10 +161,10 @@ async function typecheck(sites) {
|
|
|
177
161
|
else
|
|
178
162
|
groups.set(base, [at]);
|
|
179
163
|
}
|
|
180
|
-
|
|
164
|
+
const diagnostics = [];
|
|
181
165
|
for (const [base, group] of groups) {
|
|
182
166
|
const config = {
|
|
183
|
-
...(base ? { extends:
|
|
167
|
+
...(base ? { extends: base } : {}),
|
|
184
168
|
compilerOptions: {
|
|
185
169
|
noEmit: true,
|
|
186
170
|
strict: true,
|
|
@@ -196,37 +180,33 @@ async function typecheck(sites) {
|
|
|
196
180
|
// base config there is nothing to opt in with, so the default stays none.
|
|
197
181
|
...(base ? {} : { types: [] }),
|
|
198
182
|
},
|
|
199
|
-
files: group.flatMap((s) => [
|
|
200
|
-
relative(dir, s.file),
|
|
201
|
-
relative(dir, typesPathFor(s.file)),
|
|
202
|
-
]),
|
|
183
|
+
files: group.flatMap((s) => [s.file, typesPathFor(s.file)]),
|
|
203
184
|
// `files` overrides the base's, but a base `include` survives beside it and would
|
|
204
185
|
// drag the author's whole tree in, to be checked under the worker lib.
|
|
205
186
|
include: [],
|
|
206
187
|
};
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
188
|
+
// Never written: the name only anchors `@types` lookup at the root. It must not be
|
|
189
|
+
// `tsconfig.json`: that is the base the config `extends` whenever the author keeps one at
|
|
190
|
+
// the root, and tsc refuses a config that extends its own path as circular.
|
|
191
|
+
const parsed = ts.parseJsonConfigFileContent(config, ts.sys, root, undefined, join(root, "tsconfig.genroc.json"));
|
|
192
|
+
const program = ts.createProgram({
|
|
193
|
+
rootNames: parsed.fileNames,
|
|
194
|
+
options: parsed.options,
|
|
195
|
+
projectReferences: parsed.projectReferences,
|
|
196
|
+
configFileParsingDiagnostics: parsed.errors,
|
|
197
|
+
});
|
|
198
|
+
diagnostics.push(...parsed.errors, ...ts.getPreEmitDiagnostics(program));
|
|
210
199
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
proc.stderr.on("data", (c) => (err += c));
|
|
222
|
-
const code = await new Promise((resolve, reject) => {
|
|
223
|
-
proc.on("error", reject);
|
|
224
|
-
proc.on("close", (c) => resolve(c ?? 1));
|
|
225
|
-
});
|
|
226
|
-
if (code !== 0) {
|
|
227
|
-
// tsc reports on stdout; the exit code IS the type check, so this is the diagnostic
|
|
228
|
-
// genctl surfaces and the reason a failed import never produces a string.
|
|
229
|
-
die([out, err].filter(Boolean).join("\n").trimEnd());
|
|
200
|
+
if (diagnostics.length > 0) {
|
|
201
|
+
// The diagnostics are the whole type check: this is what genctl surfaces, and the reason a
|
|
202
|
+
// failed import never produces a string.
|
|
203
|
+
die(ts
|
|
204
|
+
.formatDiagnostics(diagnostics, {
|
|
205
|
+
getCanonicalFileName: (f) => f,
|
|
206
|
+
getCurrentDirectory: () => root,
|
|
207
|
+
getNewLine: () => "\n",
|
|
208
|
+
})
|
|
209
|
+
.trimEnd());
|
|
230
210
|
}
|
|
231
211
|
}
|
|
232
212
|
// ── bundle ─────────────────────────────────────────────────────────────────────
|
|
@@ -383,7 +363,7 @@ for (const at of located) {
|
|
|
383
363
|
byPath.set(at.file, at);
|
|
384
364
|
}
|
|
385
365
|
for (const at of byPath.values()) {
|
|
386
|
-
await
|
|
366
|
+
await writeFile(typesPathFor(at.file), declarations(at));
|
|
387
367
|
}
|
|
388
368
|
if (manifest.mode === "types") {
|
|
389
369
|
console.log(JSON.stringify(manifest, null, 2));
|
package/dist/worker.js
CHANGED
|
@@ -62,15 +62,15 @@ async function fetchObject(ref) {
|
|
|
62
62
|
/** Put each listed value back where its path says it belongs. Paths are arrays of keys, so this
|
|
63
63
|
* needs no parser: the whole reason they are not JSON Pointer strings. */
|
|
64
64
|
async function resolveObjects(job) {
|
|
65
|
-
let input = job.
|
|
65
|
+
let input = job.external_input;
|
|
66
66
|
for (const e of job.objects ?? []) {
|
|
67
67
|
const value = await fetchObject(e.ref);
|
|
68
68
|
if (e.path.length === 0) {
|
|
69
69
|
input = value;
|
|
70
70
|
continue;
|
|
71
71
|
}
|
|
72
|
-
// The path is rooted at the entry and starts with "
|
|
73
|
-
const rest = e.path[0] === "
|
|
72
|
+
// The path is rooted at the entry and starts with "external_input", the value being rebuilt.
|
|
73
|
+
const rest = e.path[0] === "external_input" ? e.path.slice(1) : e.path;
|
|
74
74
|
if (rest.length === 0) {
|
|
75
75
|
input = value;
|
|
76
76
|
continue;
|
package/import.ts
CHANGED
|
@@ -7,12 +7,10 @@
|
|
|
7
7
|
// code; "build" typechecks and bundles. A separate types hook would mean a second `tsc`
|
|
8
8
|
// over the same project.
|
|
9
9
|
|
|
10
|
-
import { spawn } from "node:child_process";
|
|
11
10
|
import { existsSync } from "node:fs";
|
|
12
|
-
import { access,
|
|
11
|
+
import { access, writeFile } from "node:fs/promises";
|
|
13
12
|
import { builtinModules } from "node:module";
|
|
14
|
-
import { dirname, join,
|
|
15
|
-
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { dirname, join, resolve } from "node:path";
|
|
16
14
|
|
|
17
15
|
import commonjs from "@rollup/plugin-commonjs";
|
|
18
16
|
import json from "@rollup/plugin-json";
|
|
@@ -70,12 +68,6 @@ async function exists(path: string): Promise<boolean> {
|
|
|
70
68
|
}
|
|
71
69
|
}
|
|
72
70
|
|
|
73
|
-
/** Creates the parent directory, which `.genroc-cache/` relies on: nothing else makes it. */
|
|
74
|
-
async function write(path: string, content: string): Promise<void> {
|
|
75
|
-
await mkdir(dirname(path), { recursive: true });
|
|
76
|
-
await writeFile(path, content);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
71
|
// ── JSON Schema → TypeScript ───────────────────────────────────────────────────
|
|
80
72
|
|
|
81
73
|
/** Only genroc's keyword set is handled; anything else its strict decoder would have
|
|
@@ -137,16 +129,21 @@ function objectType(s: Schema, used: Set<string>): string {
|
|
|
137
129
|
? ` /** ${sub.description} */\n`
|
|
138
130
|
: "";
|
|
139
131
|
lines.push(
|
|
140
|
-
`${doc} ${propKey(key)}${required.has(key) ? "" : "?"}: ${tsType(sub, used)};`,
|
|
132
|
+
`${doc} ${propKey(key)}${required.has(key) ? "" : "?"}: ${nest(tsType(sub, used))};`,
|
|
141
133
|
);
|
|
142
134
|
}
|
|
143
135
|
if (s.additionalProperties && typeof s.additionalProperties === "object") {
|
|
144
|
-
lines.push(
|
|
136
|
+
lines.push(
|
|
137
|
+
` [key: string]: ${nest(tsType(s.additionalProperties, used))};`,
|
|
138
|
+
);
|
|
145
139
|
}
|
|
146
140
|
if (lines.length === 0) return "Record<string, unknown>";
|
|
147
141
|
return `{\n${lines.join("\n")}\n}`;
|
|
148
142
|
}
|
|
149
143
|
|
|
144
|
+
/** Every line but the first, which is already placed by the property that opens it. */
|
|
145
|
+
const nest = (t: string) => t.replace(/\n/g, "\n ");
|
|
146
|
+
|
|
150
147
|
function union(parts: string[]): string {
|
|
151
148
|
const seen = [...new Set(parts)];
|
|
152
149
|
return seen.length === 0 ? "unknown" : seen.join(" | ");
|
|
@@ -157,17 +154,6 @@ const propKey = (k: string) => (IDENT.test(k) ? k : JSON.stringify(k));
|
|
|
157
154
|
const identifier = (n: string) =>
|
|
158
155
|
IDENT.test(n) ? n : `Def_${n.replace(/[^A-Za-z0-9_$]/g, "_")}`;
|
|
159
156
|
|
|
160
|
-
function deref(
|
|
161
|
-
s: Schema | undefined,
|
|
162
|
-
defs: Record<string, Schema>,
|
|
163
|
-
): Schema | undefined {
|
|
164
|
-
let cur = s;
|
|
165
|
-
for (let i = 0; cur && typeof cur.$ref === "string" && i < 16; i++) {
|
|
166
|
-
cur = defs[cur.$ref.replace(/^#\/\$defs\//, "")];
|
|
167
|
-
}
|
|
168
|
-
return cur;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
157
|
/** Emits one named type per reachable $def rather than inlining: a task output may
|
|
172
158
|
* reference itself (specs/recursive-type-inference.md) and inlining would not terminate. */
|
|
173
159
|
function declarations(at: Located): string {
|
|
@@ -223,12 +209,7 @@ async function nearestTsconfig(
|
|
|
223
209
|
}
|
|
224
210
|
|
|
225
211
|
async function typecheck(sites: Located[]): Promise<void> {
|
|
226
|
-
//
|
|
227
|
-
// coexist with it. The suffix is what keeps the scratch area out of its way.
|
|
228
|
-
const dir = join(root, ".genroc-cache");
|
|
229
|
-
await write(join(dir, ".gitignore"), "*\n");
|
|
230
|
-
|
|
231
|
-
// One tsc per distinct base config: `extends` takes a single base, so merging two would
|
|
212
|
+
// One program per distinct base config: `extends` takes a single base, so merging two would
|
|
232
213
|
// check each script under the other author's options.
|
|
233
214
|
const groups = new Map<string, Located[]>();
|
|
234
215
|
for (const at of sites) {
|
|
@@ -238,10 +219,10 @@ async function typecheck(sites: Located[]): Promise<void> {
|
|
|
238
219
|
else groups.set(base, [at]);
|
|
239
220
|
}
|
|
240
221
|
|
|
241
|
-
|
|
222
|
+
const diagnostics: ts.Diagnostic[] = [];
|
|
242
223
|
for (const [base, group] of groups) {
|
|
243
224
|
const config: Record<string, unknown> = {
|
|
244
|
-
...(base ? { extends:
|
|
225
|
+
...(base ? { extends: base } : {}),
|
|
245
226
|
compilerOptions: {
|
|
246
227
|
noEmit: true,
|
|
247
228
|
strict: true,
|
|
@@ -257,41 +238,42 @@ async function typecheck(sites: Located[]): Promise<void> {
|
|
|
257
238
|
// base config there is nothing to opt in with, so the default stays none.
|
|
258
239
|
...(base ? {} : { types: [] }),
|
|
259
240
|
},
|
|
260
|
-
files: group.flatMap((s) => [
|
|
261
|
-
relative(dir, s.file),
|
|
262
|
-
relative(dir, typesPathFor(s.file)),
|
|
263
|
-
]),
|
|
241
|
+
files: group.flatMap((s) => [s.file, typesPathFor(s.file)]),
|
|
264
242
|
// `files` overrides the base's, but a base `include` survives beside it and would
|
|
265
243
|
// drag the author's whole tree in, to be checked under the worker lib.
|
|
266
244
|
include: [],
|
|
267
245
|
};
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
246
|
+
// Never written: the name only anchors `@types` lookup at the root. It must not be
|
|
247
|
+
// `tsconfig.json`: that is the base the config `extends` whenever the author keeps one at
|
|
248
|
+
// the root, and tsc refuses a config that extends its own path as circular.
|
|
249
|
+
const parsed = ts.parseJsonConfigFileContent(
|
|
250
|
+
config,
|
|
251
|
+
ts.sys,
|
|
252
|
+
root,
|
|
253
|
+
undefined,
|
|
254
|
+
join(root, "tsconfig.genroc.json"),
|
|
271
255
|
);
|
|
272
|
-
|
|
273
|
-
|
|
256
|
+
const program = ts.createProgram({
|
|
257
|
+
rootNames: parsed.fileNames,
|
|
258
|
+
options: parsed.options,
|
|
259
|
+
projectReferences: parsed.projectReferences,
|
|
260
|
+
configFileParsingDiagnostics: parsed.errors,
|
|
261
|
+
});
|
|
262
|
+
diagnostics.push(...parsed.errors, ...ts.getPreEmitDiagnostics(program));
|
|
274
263
|
}
|
|
275
|
-
}
|
|
276
264
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
proc.on("close", (c) => resolve(c ?? 1));
|
|
290
|
-
});
|
|
291
|
-
if (code !== 0) {
|
|
292
|
-
// tsc reports on stdout; the exit code IS the type check, so this is the diagnostic
|
|
293
|
-
// genctl surfaces and the reason a failed import never produces a string.
|
|
294
|
-
die([out, err].filter(Boolean).join("\n").trimEnd());
|
|
265
|
+
if (diagnostics.length > 0) {
|
|
266
|
+
// The diagnostics are the whole type check: this is what genctl surfaces, and the reason a
|
|
267
|
+
// failed import never produces a string.
|
|
268
|
+
die(
|
|
269
|
+
ts
|
|
270
|
+
.formatDiagnostics(diagnostics, {
|
|
271
|
+
getCanonicalFileName: (f) => f,
|
|
272
|
+
getCurrentDirectory: () => root,
|
|
273
|
+
getNewLine: () => "\n",
|
|
274
|
+
})
|
|
275
|
+
.trimEnd(),
|
|
276
|
+
);
|
|
295
277
|
}
|
|
296
278
|
}
|
|
297
279
|
|
|
@@ -490,7 +472,7 @@ for (const at of located) {
|
|
|
490
472
|
}
|
|
491
473
|
|
|
492
474
|
for (const at of byPath.values()) {
|
|
493
|
-
await
|
|
475
|
+
await writeFile(typesPathFor(at.file), declarations(at));
|
|
494
476
|
}
|
|
495
477
|
|
|
496
478
|
if (manifest.mode === "types") {
|
package/package.json
CHANGED
package/worker.ts
CHANGED
|
@@ -43,8 +43,8 @@ type ObjectEntry = { path: (string | number)[]; ref: string; size: number };
|
|
|
43
43
|
type QueueTask = {
|
|
44
44
|
token: string;
|
|
45
45
|
process: string;
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
task: string;
|
|
47
|
+
external_input: unknown;
|
|
48
48
|
objects?: ObjectEntry[];
|
|
49
49
|
raises?: Record<string, unknown>;
|
|
50
50
|
};
|
|
@@ -76,15 +76,15 @@ async function fetchObject(ref: string): Promise<unknown> {
|
|
|
76
76
|
/** Put each listed value back where its path says it belongs. Paths are arrays of keys, so this
|
|
77
77
|
* needs no parser: the whole reason they are not JSON Pointer strings. */
|
|
78
78
|
async function resolveObjects(job: QueueTask): Promise<unknown> {
|
|
79
|
-
let input: any = job.
|
|
79
|
+
let input: any = job.external_input;
|
|
80
80
|
for (const e of job.objects ?? []) {
|
|
81
81
|
const value = await fetchObject(e.ref);
|
|
82
82
|
if (e.path.length === 0) {
|
|
83
83
|
input = value;
|
|
84
84
|
continue;
|
|
85
85
|
}
|
|
86
|
-
// The path is rooted at the entry and starts with "
|
|
87
|
-
const rest = e.path[0] === "
|
|
86
|
+
// The path is rooted at the entry and starts with "external_input", the value being rebuilt.
|
|
87
|
+
const rest = e.path[0] === "external_input" ? e.path.slice(1) : e.path;
|
|
88
88
|
if (rest.length === 0) {
|
|
89
89
|
input = value;
|
|
90
90
|
continue;
|