@toolproof-core/schema 1.0.8 → 1.0.9

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.
@@ -1,73 +1,73 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import { getConfig } from "./_lib/config.js";
4
-
5
- type DependencyMap = Record<string, string[]>;
6
-
7
- // PURE: Validate + normalize a dependency map loaded from JSON.
8
- function normalizeDependencyMap(value: unknown): DependencyMap {
9
- if (!value || typeof value !== "object" || Array.isArray(value)) {
10
- throw new Error("Invalid dependencyMap.json: expected an object");
11
- }
12
-
13
- const input = value as Record<string, unknown>;
14
- const out: DependencyMap = {};
15
-
16
- // Preserve insertion order from the parsed JSON object.
17
- for (const [key, rawDeps] of Object.entries(input)) {
18
- if (rawDeps == null) {
19
- out[key] = [];
20
- continue;
21
- }
22
- if (!Array.isArray(rawDeps)) {
23
- throw new Error(`Invalid dependencyMap.json: value for ${key} must be an array`);
24
- }
25
- out[key] = rawDeps.filter((d): d is string => typeof d === "string");
26
- }
27
-
28
- return out;
29
- }
30
-
31
- // PURE: Compute terminals (defs that are not depended-upon by any other def).
32
- function computeTerminalsInKeyOrder(dependencyMap: DependencyMap): string[] {
33
- const keys = Object.keys(dependencyMap);
34
- const dependedUpon = new Set<string>();
35
-
36
- for (const key of keys) {
37
- for (const dep of dependencyMap[key] ?? []) {
38
- dependedUpon.add(dep);
39
- }
40
- }
41
-
42
- // Preserve key order from dependencyMap.json.
43
- return keys.filter((k) => !dependedUpon.has(k));
44
- }
45
-
46
- // IMPURE: Script entrypoint (config + filesystem I/O + console + process exit code).
47
- function main() {
48
- try {
49
- const config = getConfig();
50
-
51
- const inPath = config.getDependencyMapPath();
52
- const outPath = path.join(path.dirname(inPath), "terminals.json");
53
-
54
- if (!fs.existsSync(inPath)) {
55
- throw new Error(`Dependency map not found at ${inPath}. Run generateDependencies first.`);
56
- }
57
-
58
- const raw = fs.readFileSync(inPath, "utf8");
59
- const parsed = JSON.parse(raw) as unknown;
60
-
61
- const dependencyMap = normalizeDependencyMap(parsed);
62
- const terminals = computeTerminalsInKeyOrder(dependencyMap);
63
-
64
- fs.mkdirSync(path.dirname(outPath), { recursive: true });
65
- fs.writeFileSync(outPath, JSON.stringify(terminals, null, 4), "utf8");
66
- console.log(`Wrote terminals to ${outPath}`);
67
- } catch (error: any) {
68
- console.error(`Error generating terminals: ${error?.message ?? error}`);
69
- process.exitCode = 1;
70
- }
71
- }
72
-
73
- main();
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { getConfig } from "./_lib/config.js";
4
+
5
+ type DependencyMap = Record<string, string[]>;
6
+
7
+ // PURE: Validate + normalize a dependency map loaded from JSON.
8
+ function normalizeDependencyMap(value: unknown): DependencyMap {
9
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
10
+ throw new Error("Invalid dependencyMap.json: expected an object");
11
+ }
12
+
13
+ const input = value as Record<string, unknown>;
14
+ const out: DependencyMap = {};
15
+
16
+ // Preserve insertion order from the parsed JSON object.
17
+ for (const [key, rawDeps] of Object.entries(input)) {
18
+ if (rawDeps == null) {
19
+ out[key] = [];
20
+ continue;
21
+ }
22
+ if (!Array.isArray(rawDeps)) {
23
+ throw new Error(`Invalid dependencyMap.json: value for ${key} must be an array`);
24
+ }
25
+ out[key] = rawDeps.filter((d): d is string => typeof d === "string");
26
+ }
27
+
28
+ return out;
29
+ }
30
+
31
+ // PURE: Compute terminals (defs that are not depended-upon by any other def).
32
+ function computeTerminalsInKeyOrder(dependencyMap: DependencyMap): string[] {
33
+ const keys = Object.keys(dependencyMap);
34
+ const dependedUpon = new Set<string>();
35
+
36
+ for (const key of keys) {
37
+ for (const dep of dependencyMap[key] ?? []) {
38
+ dependedUpon.add(dep);
39
+ }
40
+ }
41
+
42
+ // Preserve key order from dependencyMap.json.
43
+ return keys.filter((k) => !dependedUpon.has(k));
44
+ }
45
+
46
+ // IMPURE: Script entrypoint (config + filesystem I/O + console + process exit code).
47
+ function main() {
48
+ try {
49
+ const config = getConfig();
50
+
51
+ const inPath = config.getDependencyMapPath();
52
+ const outPath = path.join(path.dirname(inPath), "terminals.json");
53
+
54
+ if (!fs.existsSync(inPath)) {
55
+ throw new Error(`Dependency map not found at ${inPath}. Run generateDependencies first.`);
56
+ }
57
+
58
+ const raw = fs.readFileSync(inPath, "utf8");
59
+ const parsed = JSON.parse(raw) as unknown;
60
+
61
+ const dependencyMap = normalizeDependencyMap(parsed);
62
+ const terminals = computeTerminalsInKeyOrder(dependencyMap);
63
+
64
+ fs.mkdirSync(path.dirname(outPath), { recursive: true });
65
+ fs.writeFileSync(outPath, JSON.stringify(terminals, null, 4), "utf8");
66
+ console.log(`Wrote terminals to ${outPath}`);
67
+ } catch (error: any) {
68
+ console.error(`Error generating terminals: ${error?.message ?? error}`);
69
+ process.exitCode = 1;
70
+ }
71
+ }
72
+
73
+ main();