@genroc/eval-node 0.0.0-edge.f8a2edb → 0.0.0-edge.f8ffa72f
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 +202 -0
- package/README.md +1 -1
- 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/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ resolver's exit code, so a stored definition cannot hold code that failed to typ
|
|
|
26
26
|
|
|
27
27
|
## Building
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
pnpm run build # tsc -p tsconfig.build.json -> dist/
|
|
30
30
|
|
|
31
31
|
The published package is JavaScript, not TypeScript. **Node refuses to strip types for files
|
|
32
32
|
under `node_modules`**, so shipping `.ts` works from a checkout and fails for every consumer —
|
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;
|