@toolproof-core/schema 1.0.1 → 1.0.2
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/package.json +8 -7
- package/src/Genesis.json +1999 -1999
- package/src/generated/types/standalone/Resource_Genesis.js +1 -1
- package/src/generated/types/standalone/Resource_Job.js +1 -1
- package/src/generated/types/standalone/Resource_RawStrategy.js +1 -1
- package/src/generated/types/standalone/Resource_ResourceType.js +1 -1
- package/src/generated/types/standalone/Resource_RunnableStrategy.js +1 -1
- package/src/scripts/_lib/config.ts +205 -205
- package/src/scripts/extractSchemasFromResourceTypeShells.ts +218 -218
- package/src/scripts/generateDependencies.ts +120 -120
- package/src/scripts/generateSchemaShims.ts +135 -135
- package/src/scripts/generateStandaloneSchema.ts +175 -175
- package/src/scripts/generateStandaloneType.ts +119 -119
- package/src/scripts/generateTypes.ts +614 -614
- package/src/scripts/normalizeAnchorsToPointers.ts +123 -123
- package/src/scripts/wrapResourceTypesWithResourceShells.ts +84 -84
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { getConfig } from "./_lib/config.js";
|
|
4
|
-
|
|
5
|
-
type JSONValue = null | boolean | number | string | JSONValue[] | { [k: string]: JSONValue };
|
|
6
|
-
|
|
7
|
-
function decodeJsonPointerSegment(segment: string): string {
|
|
8
|
-
// JSON Pointer decoding: ~1 => / and ~0 => ~
|
|
9
|
-
return segment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function collectRefs(node: unknown, out: Set<string>): void {
|
|
13
|
-
if (Array.isArray(node)) {
|
|
14
|
-
for (const item of node) collectRefs(item, out);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
if (!node || typeof node !== "object") return;
|
|
18
|
-
|
|
19
|
-
const obj = node as Record<string, unknown>;
|
|
20
|
-
const ref = obj["$ref"];
|
|
21
|
-
if (typeof ref === "string") out.add(ref);
|
|
22
|
-
|
|
23
|
-
for (const value of Object.values(obj)) {
|
|
24
|
-
collectRefs(value, out);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function resolveInternalRefToDefKey(ref: string, defKeys: Set<string>, anchorToDef: Record<string, string>): string | null {
|
|
29
|
-
if (!ref.startsWith("#")) return null;
|
|
30
|
-
|
|
31
|
-
// JSON Pointer: #/$defs/<Name>(/...)
|
|
32
|
-
const defsPrefix = "#/$defs/";
|
|
33
|
-
if (ref.startsWith(defsPrefix)) {
|
|
34
|
-
const rest = ref.slice(defsPrefix.length);
|
|
35
|
-
const firstSegment = rest.split("/")[0] ?? "";
|
|
36
|
-
const defKey = decodeJsonPointerSegment(firstSegment);
|
|
37
|
-
return defKeys.has(defKey) ? defKey : null;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Anchor ref: #AnchorName
|
|
41
|
-
if (!ref.startsWith("#/")) {
|
|
42
|
-
const anchor = ref.slice(1);
|
|
43
|
-
const mapped = anchorToDef[anchor];
|
|
44
|
-
if (mapped && defKeys.has(mapped)) return mapped;
|
|
45
|
-
if (defKeys.has(anchor)) return anchor;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Pure function that generates a dependency map from a JSON Schema document.
|
|
53
|
-
*
|
|
54
|
-
* @param doc The source JSON Schema document
|
|
55
|
-
* @returns A record mapping definition names to their dependency lists
|
|
56
|
-
*/
|
|
57
|
-
function generateDependencyMapLogic(doc: any): Record<string, string[]> {
|
|
58
|
-
const defs: Record<string, JSONValue> = doc?.$defs && typeof doc.$defs === "object" ? doc.$defs : {};
|
|
59
|
-
const defKeys = new Set(Object.keys(defs));
|
|
60
|
-
|
|
61
|
-
// Map anchors to $defs keys (useful if any anchor-style refs remain)
|
|
62
|
-
const anchorToDef: Record<string, string> = {};
|
|
63
|
-
for (const [defKey, defSchema] of Object.entries(defs)) {
|
|
64
|
-
if (!defSchema || typeof defSchema !== "object" || Array.isArray(defSchema)) continue;
|
|
65
|
-
const anchor = (defSchema as any).$anchor;
|
|
66
|
-
if (typeof anchor === "string" && !(anchor in anchorToDef)) {
|
|
67
|
-
anchorToDef[anchor] = defKey;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const dependencyMap: Record<string, string[]> = {};
|
|
72
|
-
|
|
73
|
-
for (const [defKey, defSchema] of Object.entries(defs)) {
|
|
74
|
-
const refs = new Set<string>();
|
|
75
|
-
collectRefs(defSchema, refs);
|
|
76
|
-
|
|
77
|
-
const deps = new Set<string>();
|
|
78
|
-
for (const ref of refs) {
|
|
79
|
-
const depKey = resolveInternalRefToDefKey(ref, defKeys, anchorToDef);
|
|
80
|
-
if (!depKey) continue;
|
|
81
|
-
if (depKey === defKey) continue;
|
|
82
|
-
deps.add(depKey);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
dependencyMap[defKey] = Array.from(deps);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return dependencyMap;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
async function main() {
|
|
92
|
-
const config = getConfig();
|
|
93
|
-
|
|
94
|
-
const inPath = config.getSchemaPath("Genesis.json");
|
|
95
|
-
const outPath = config.getDependencyMapPath();
|
|
96
|
-
|
|
97
|
-
if (!fs.existsSync(inPath)) {
|
|
98
|
-
console.error(`Genesis schema not found at ${inPath}. Run extractSchemasFromResourceTypeShells first.`);
|
|
99
|
-
process.exit(1);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
try {
|
|
103
|
-
const raw = fs.readFileSync(inPath, "utf8");
|
|
104
|
-
const doc = JSON.parse(raw);
|
|
105
|
-
|
|
106
|
-
const dependencyMap = generateDependencyMapLogic(doc);
|
|
107
|
-
|
|
108
|
-
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
|
109
|
-
fs.writeFileSync(outPath, JSON.stringify(dependencyMap, null, 4), "utf8");
|
|
110
|
-
console.log(`Wrote dependency map to ${outPath}`);
|
|
111
|
-
} catch (error: any) {
|
|
112
|
-
console.error(`Error generating dependency map: ${error.message}`);
|
|
113
|
-
process.exit(1);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
main().catch((e) => {
|
|
118
|
-
console.error(e);
|
|
119
|
-
process.exit(1);
|
|
120
|
-
});
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { getConfig } from "./_lib/config.js";
|
|
4
|
+
|
|
5
|
+
type JSONValue = null | boolean | number | string | JSONValue[] | { [k: string]: JSONValue };
|
|
6
|
+
|
|
7
|
+
function decodeJsonPointerSegment(segment: string): string {
|
|
8
|
+
// JSON Pointer decoding: ~1 => / and ~0 => ~
|
|
9
|
+
return segment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function collectRefs(node: unknown, out: Set<string>): void {
|
|
13
|
+
if (Array.isArray(node)) {
|
|
14
|
+
for (const item of node) collectRefs(item, out);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (!node || typeof node !== "object") return;
|
|
18
|
+
|
|
19
|
+
const obj = node as Record<string, unknown>;
|
|
20
|
+
const ref = obj["$ref"];
|
|
21
|
+
if (typeof ref === "string") out.add(ref);
|
|
22
|
+
|
|
23
|
+
for (const value of Object.values(obj)) {
|
|
24
|
+
collectRefs(value, out);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function resolveInternalRefToDefKey(ref: string, defKeys: Set<string>, anchorToDef: Record<string, string>): string | null {
|
|
29
|
+
if (!ref.startsWith("#")) return null;
|
|
30
|
+
|
|
31
|
+
// JSON Pointer: #/$defs/<Name>(/...)
|
|
32
|
+
const defsPrefix = "#/$defs/";
|
|
33
|
+
if (ref.startsWith(defsPrefix)) {
|
|
34
|
+
const rest = ref.slice(defsPrefix.length);
|
|
35
|
+
const firstSegment = rest.split("/")[0] ?? "";
|
|
36
|
+
const defKey = decodeJsonPointerSegment(firstSegment);
|
|
37
|
+
return defKeys.has(defKey) ? defKey : null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Anchor ref: #AnchorName
|
|
41
|
+
if (!ref.startsWith("#/")) {
|
|
42
|
+
const anchor = ref.slice(1);
|
|
43
|
+
const mapped = anchorToDef[anchor];
|
|
44
|
+
if (mapped && defKeys.has(mapped)) return mapped;
|
|
45
|
+
if (defKeys.has(anchor)) return anchor;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Pure function that generates a dependency map from a JSON Schema document.
|
|
53
|
+
*
|
|
54
|
+
* @param doc The source JSON Schema document
|
|
55
|
+
* @returns A record mapping definition names to their dependency lists
|
|
56
|
+
*/
|
|
57
|
+
function generateDependencyMapLogic(doc: any): Record<string, string[]> {
|
|
58
|
+
const defs: Record<string, JSONValue> = doc?.$defs && typeof doc.$defs === "object" ? doc.$defs : {};
|
|
59
|
+
const defKeys = new Set(Object.keys(defs));
|
|
60
|
+
|
|
61
|
+
// Map anchors to $defs keys (useful if any anchor-style refs remain)
|
|
62
|
+
const anchorToDef: Record<string, string> = {};
|
|
63
|
+
for (const [defKey, defSchema] of Object.entries(defs)) {
|
|
64
|
+
if (!defSchema || typeof defSchema !== "object" || Array.isArray(defSchema)) continue;
|
|
65
|
+
const anchor = (defSchema as any).$anchor;
|
|
66
|
+
if (typeof anchor === "string" && !(anchor in anchorToDef)) {
|
|
67
|
+
anchorToDef[anchor] = defKey;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const dependencyMap: Record<string, string[]> = {};
|
|
72
|
+
|
|
73
|
+
for (const [defKey, defSchema] of Object.entries(defs)) {
|
|
74
|
+
const refs = new Set<string>();
|
|
75
|
+
collectRefs(defSchema, refs);
|
|
76
|
+
|
|
77
|
+
const deps = new Set<string>();
|
|
78
|
+
for (const ref of refs) {
|
|
79
|
+
const depKey = resolveInternalRefToDefKey(ref, defKeys, anchorToDef);
|
|
80
|
+
if (!depKey) continue;
|
|
81
|
+
if (depKey === defKey) continue;
|
|
82
|
+
deps.add(depKey);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
dependencyMap[defKey] = Array.from(deps);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return dependencyMap;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function main() {
|
|
92
|
+
const config = getConfig();
|
|
93
|
+
|
|
94
|
+
const inPath = config.getSchemaPath("Genesis.json");
|
|
95
|
+
const outPath = config.getDependencyMapPath();
|
|
96
|
+
|
|
97
|
+
if (!fs.existsSync(inPath)) {
|
|
98
|
+
console.error(`Genesis schema not found at ${inPath}. Run extractSchemasFromResourceTypeShells first.`);
|
|
99
|
+
process.exit(1);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
const raw = fs.readFileSync(inPath, "utf8");
|
|
104
|
+
const doc = JSON.parse(raw);
|
|
105
|
+
|
|
106
|
+
const dependencyMap = generateDependencyMapLogic(doc);
|
|
107
|
+
|
|
108
|
+
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
|
109
|
+
fs.writeFileSync(outPath, JSON.stringify(dependencyMap, null, 4), "utf8");
|
|
110
|
+
console.log(`Wrote dependency map to ${outPath}`);
|
|
111
|
+
} catch (error: any) {
|
|
112
|
+
console.error(`Error generating dependency map: ${error.message}`);
|
|
113
|
+
process.exit(1);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
main().catch((e) => {
|
|
118
|
+
console.error(e);
|
|
119
|
+
process.exit(1);
|
|
120
|
+
});
|
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { getConfig } from './_lib/config.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Generate TypeScript shim files for all JSON schemas and resources
|
|
7
|
-
*
|
|
8
|
-
* Creates .ts files that import and re-export .json files using
|
|
9
|
-
* the JSON import assertion syntax. This enables proper dependency resolution
|
|
10
|
-
* and type inference when importing schemas in TypeScript.
|
|
11
|
-
*
|
|
12
|
-
* Generates shims for:
|
|
13
|
-
* - schemas dir: config.getSchemasDir() (schema files)
|
|
14
|
-
* - standalone schemas dir: config.getSchemasStandaloneDir() (standalone schema files)
|
|
15
|
-
* - resources dir: config.getResourcesDir() (resource envelope files)
|
|
16
|
-
*
|
|
17
|
-
* Usage: node ./dist/scripts/generateSchemaShims.js
|
|
18
|
-
*/
|
|
19
|
-
/**
|
|
20
|
-
* Pure function to generate the content of a TypeScript shim file.
|
|
21
|
-
*
|
|
22
|
-
* @param jsonFile The name of the JSON file to import
|
|
23
|
-
* @param variableName The name of the variable to use for the import
|
|
24
|
-
* @returns The TypeScript file content
|
|
25
|
-
*/
|
|
26
|
-
function generateShimContent(jsonFile: string, variableName: string): string {
|
|
27
|
-
return `import ${variableName} from './${jsonFile}' with { type: 'json' };\nexport default ${variableName};\n`;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Pure function to map a list of JSON files to their corresponding TypeScript shim files and contents.
|
|
32
|
-
*
|
|
33
|
-
* @param jsonFiles List of JSON filenames
|
|
34
|
-
* @param variableName The variable name to use in the shim
|
|
35
|
-
* @returns A record mapping TS filenames to their contents
|
|
36
|
-
*/
|
|
37
|
-
function getShimsForFiles(jsonFiles: string[], variableName: string): Record<string, string> {
|
|
38
|
-
const shims: Record<string, string> = {};
|
|
39
|
-
for (const jsonFile of jsonFiles) {
|
|
40
|
-
const baseName = path.basename(jsonFile, '.json');
|
|
41
|
-
const tsFile = `${baseName}.ts`;
|
|
42
|
-
shims[tsFile] = generateShimContent(jsonFile, variableName);
|
|
43
|
-
}
|
|
44
|
-
return shims;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async function main() {
|
|
48
|
-
const config = getConfig();
|
|
49
|
-
const schemasDir = config.getSchemasDir();
|
|
50
|
-
const standaloneSchemasDir = config.getSchemasStandaloneDir();
|
|
51
|
-
const resourcesDir = config.getResourcesDir();
|
|
52
|
-
const generatedResourceTypesDir = config.getNormalizedDir();
|
|
53
|
-
|
|
54
|
-
let totalCount = 0;
|
|
55
|
-
|
|
56
|
-
// Process schemas directory
|
|
57
|
-
if (fs.existsSync(schemasDir)) {
|
|
58
|
-
const files = fs.readdirSync(schemasDir);
|
|
59
|
-
const jsonFiles = files.filter(f => f.endsWith('.json') && !f.startsWith('.'));
|
|
60
|
-
|
|
61
|
-
const shims = getShimsForFiles(jsonFiles, 'schema');
|
|
62
|
-
|
|
63
|
-
for (const [tsFile, content] of Object.entries(shims)) {
|
|
64
|
-
const tsPath = path.join(schemasDir, tsFile);
|
|
65
|
-
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
66
|
-
console.log(`Generated ${tsFile} in ${schemasDir}`);
|
|
67
|
-
totalCount++;
|
|
68
|
-
}
|
|
69
|
-
console.log(`Generated ${jsonFiles.length} TypeScript schema shims in ${schemasDir}`);
|
|
70
|
-
} else {
|
|
71
|
-
console.warn(`Schemas directory not found at ${schemasDir}`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Process standalone schemas directory
|
|
75
|
-
if (fs.existsSync(standaloneSchemasDir)) {
|
|
76
|
-
const files = fs.readdirSync(standaloneSchemasDir);
|
|
77
|
-
const jsonFiles = files.filter(f => f.endsWith('.json') && !f.startsWith('.'));
|
|
78
|
-
|
|
79
|
-
const shims = getShimsForFiles(jsonFiles, 'schema');
|
|
80
|
-
|
|
81
|
-
for (const [tsFile, content] of Object.entries(shims)) {
|
|
82
|
-
const tsPath = path.join(standaloneSchemasDir, tsFile);
|
|
83
|
-
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
84
|
-
console.log(`Generated ${tsFile} in ${standaloneSchemasDir}`);
|
|
85
|
-
totalCount++;
|
|
86
|
-
}
|
|
87
|
-
console.log(`Generated ${jsonFiles.length} TypeScript standalone schema shims in ${standaloneSchemasDir}`);
|
|
88
|
-
} else {
|
|
89
|
-
// Standalone schemas are optional
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Process resources directory
|
|
93
|
-
if (fs.existsSync(resourcesDir)) {
|
|
94
|
-
const files = fs.readdirSync(resourcesDir);
|
|
95
|
-
const jsonFiles = files.filter(f => f.endsWith('.json') && !f.startsWith('.'));
|
|
96
|
-
|
|
97
|
-
const shims = getShimsForFiles(jsonFiles, 'resources');
|
|
98
|
-
|
|
99
|
-
for (const [tsFile, content] of Object.entries(shims)) {
|
|
100
|
-
const tsPath = path.join(resourcesDir, tsFile);
|
|
101
|
-
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
102
|
-
console.log(`Generated ${tsFile} in ${resourcesDir}`);
|
|
103
|
-
totalCount++;
|
|
104
|
-
}
|
|
105
|
-
console.log(`Generated ${jsonFiles.length} TypeScript resource shims in ${resourcesDir}`);
|
|
106
|
-
} else {
|
|
107
|
-
console.warn(`Resources directory not found at ${resourcesDir}`);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// Genesis (normalized) shim
|
|
111
|
-
try {
|
|
112
|
-
const genesisJsonPath = config.getNormalizedSourcePath();
|
|
113
|
-
if (fs.existsSync(genesisJsonPath)) {
|
|
114
|
-
fs.mkdirSync(generatedResourceTypesDir, { recursive: true });
|
|
115
|
-
const tsFile = 'Genesis.ts';
|
|
116
|
-
const content = generateShimContent('Genesis.json', 'schema');
|
|
117
|
-
const tsPath = path.join(generatedResourceTypesDir, tsFile);
|
|
118
|
-
|
|
119
|
-
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
120
|
-
console.log(`Generated ${tsFile} in ${generatedResourceTypesDir}`);
|
|
121
|
-
totalCount++;
|
|
122
|
-
} else {
|
|
123
|
-
console.warn(`Genesis source JSON not found at ${genesisJsonPath}; skipping Genesis.ts shim`);
|
|
124
|
-
}
|
|
125
|
-
} catch (e) {
|
|
126
|
-
console.warn('Failed to generate Genesis.ts shim:', e);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
console.log(`Generated ${totalCount} total TypeScript shims`);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
main().catch((e) => {
|
|
133
|
-
console.error(e);
|
|
134
|
-
process.exit(1);
|
|
135
|
-
});
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { getConfig } from './_lib/config.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generate TypeScript shim files for all JSON schemas and resources
|
|
7
|
+
*
|
|
8
|
+
* Creates .ts files that import and re-export .json files using
|
|
9
|
+
* the JSON import assertion syntax. This enables proper dependency resolution
|
|
10
|
+
* and type inference when importing schemas in TypeScript.
|
|
11
|
+
*
|
|
12
|
+
* Generates shims for:
|
|
13
|
+
* - schemas dir: config.getSchemasDir() (schema files)
|
|
14
|
+
* - standalone schemas dir: config.getSchemasStandaloneDir() (standalone schema files)
|
|
15
|
+
* - resources dir: config.getResourcesDir() (resource envelope files)
|
|
16
|
+
*
|
|
17
|
+
* Usage: node ./dist/scripts/generateSchemaShims.js
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Pure function to generate the content of a TypeScript shim file.
|
|
21
|
+
*
|
|
22
|
+
* @param jsonFile The name of the JSON file to import
|
|
23
|
+
* @param variableName The name of the variable to use for the import
|
|
24
|
+
* @returns The TypeScript file content
|
|
25
|
+
*/
|
|
26
|
+
function generateShimContent(jsonFile: string, variableName: string): string {
|
|
27
|
+
return `import ${variableName} from './${jsonFile}' with { type: 'json' };\nexport default ${variableName};\n`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Pure function to map a list of JSON files to their corresponding TypeScript shim files and contents.
|
|
32
|
+
*
|
|
33
|
+
* @param jsonFiles List of JSON filenames
|
|
34
|
+
* @param variableName The variable name to use in the shim
|
|
35
|
+
* @returns A record mapping TS filenames to their contents
|
|
36
|
+
*/
|
|
37
|
+
function getShimsForFiles(jsonFiles: string[], variableName: string): Record<string, string> {
|
|
38
|
+
const shims: Record<string, string> = {};
|
|
39
|
+
for (const jsonFile of jsonFiles) {
|
|
40
|
+
const baseName = path.basename(jsonFile, '.json');
|
|
41
|
+
const tsFile = `${baseName}.ts`;
|
|
42
|
+
shims[tsFile] = generateShimContent(jsonFile, variableName);
|
|
43
|
+
}
|
|
44
|
+
return shims;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function main() {
|
|
48
|
+
const config = getConfig();
|
|
49
|
+
const schemasDir = config.getSchemasDir();
|
|
50
|
+
const standaloneSchemasDir = config.getSchemasStandaloneDir();
|
|
51
|
+
const resourcesDir = config.getResourcesDir();
|
|
52
|
+
const generatedResourceTypesDir = config.getNormalizedDir();
|
|
53
|
+
|
|
54
|
+
let totalCount = 0;
|
|
55
|
+
|
|
56
|
+
// Process schemas directory
|
|
57
|
+
if (fs.existsSync(schemasDir)) {
|
|
58
|
+
const files = fs.readdirSync(schemasDir);
|
|
59
|
+
const jsonFiles = files.filter(f => f.endsWith('.json') && !f.startsWith('.'));
|
|
60
|
+
|
|
61
|
+
const shims = getShimsForFiles(jsonFiles, 'schema');
|
|
62
|
+
|
|
63
|
+
for (const [tsFile, content] of Object.entries(shims)) {
|
|
64
|
+
const tsPath = path.join(schemasDir, tsFile);
|
|
65
|
+
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
66
|
+
console.log(`Generated ${tsFile} in ${schemasDir}`);
|
|
67
|
+
totalCount++;
|
|
68
|
+
}
|
|
69
|
+
console.log(`Generated ${jsonFiles.length} TypeScript schema shims in ${schemasDir}`);
|
|
70
|
+
} else {
|
|
71
|
+
console.warn(`Schemas directory not found at ${schemasDir}`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Process standalone schemas directory
|
|
75
|
+
if (fs.existsSync(standaloneSchemasDir)) {
|
|
76
|
+
const files = fs.readdirSync(standaloneSchemasDir);
|
|
77
|
+
const jsonFiles = files.filter(f => f.endsWith('.json') && !f.startsWith('.'));
|
|
78
|
+
|
|
79
|
+
const shims = getShimsForFiles(jsonFiles, 'schema');
|
|
80
|
+
|
|
81
|
+
for (const [tsFile, content] of Object.entries(shims)) {
|
|
82
|
+
const tsPath = path.join(standaloneSchemasDir, tsFile);
|
|
83
|
+
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
84
|
+
console.log(`Generated ${tsFile} in ${standaloneSchemasDir}`);
|
|
85
|
+
totalCount++;
|
|
86
|
+
}
|
|
87
|
+
console.log(`Generated ${jsonFiles.length} TypeScript standalone schema shims in ${standaloneSchemasDir}`);
|
|
88
|
+
} else {
|
|
89
|
+
// Standalone schemas are optional
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Process resources directory
|
|
93
|
+
if (fs.existsSync(resourcesDir)) {
|
|
94
|
+
const files = fs.readdirSync(resourcesDir);
|
|
95
|
+
const jsonFiles = files.filter(f => f.endsWith('.json') && !f.startsWith('.'));
|
|
96
|
+
|
|
97
|
+
const shims = getShimsForFiles(jsonFiles, 'resources');
|
|
98
|
+
|
|
99
|
+
for (const [tsFile, content] of Object.entries(shims)) {
|
|
100
|
+
const tsPath = path.join(resourcesDir, tsFile);
|
|
101
|
+
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
102
|
+
console.log(`Generated ${tsFile} in ${resourcesDir}`);
|
|
103
|
+
totalCount++;
|
|
104
|
+
}
|
|
105
|
+
console.log(`Generated ${jsonFiles.length} TypeScript resource shims in ${resourcesDir}`);
|
|
106
|
+
} else {
|
|
107
|
+
console.warn(`Resources directory not found at ${resourcesDir}`);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Genesis (normalized) shim
|
|
111
|
+
try {
|
|
112
|
+
const genesisJsonPath = config.getNormalizedSourcePath();
|
|
113
|
+
if (fs.existsSync(genesisJsonPath)) {
|
|
114
|
+
fs.mkdirSync(generatedResourceTypesDir, { recursive: true });
|
|
115
|
+
const tsFile = 'Genesis.ts';
|
|
116
|
+
const content = generateShimContent('Genesis.json', 'schema');
|
|
117
|
+
const tsPath = path.join(generatedResourceTypesDir, tsFile);
|
|
118
|
+
|
|
119
|
+
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
120
|
+
console.log(`Generated ${tsFile} in ${generatedResourceTypesDir}`);
|
|
121
|
+
totalCount++;
|
|
122
|
+
} else {
|
|
123
|
+
console.warn(`Genesis source JSON not found at ${genesisJsonPath}; skipping Genesis.ts shim`);
|
|
124
|
+
}
|
|
125
|
+
} catch (e) {
|
|
126
|
+
console.warn('Failed to generate Genesis.ts shim:', e);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
console.log(`Generated ${totalCount} total TypeScript shims`);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
main().catch((e) => {
|
|
133
|
+
console.error(e);
|
|
134
|
+
process.exit(1);
|
|
135
|
+
});
|