@toolproof-core/schema 1.0.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/dist/generated/types/Resource_Genesis.d.ts +3 -0
- package/dist/generated/types/Resource_Genesis.js +1 -0
- package/dist/generated/types/Resource_Job.d.ts +3 -0
- package/dist/generated/types/Resource_Job.js +1 -0
- package/dist/generated/types/Resource_RawStrategy.d.ts +3 -0
- package/dist/generated/types/Resource_RawStrategy.js +1 -0
- package/dist/generated/types/Resource_ResourceType.d.ts +3 -0
- package/dist/generated/types/Resource_ResourceType.js +1 -0
- package/dist/generated/types/Resource_RunnableStrategy.d.ts +3 -0
- package/dist/generated/types/Resource_RunnableStrategy.js +1 -0
- package/dist/generated/types/types.d.ts +1784 -0
- package/dist/generated/types/types.js +1 -0
- package/dist/scripts/_lib/config.d.ts +53 -0
- package/dist/scripts/_lib/config.js +138 -0
- package/dist/scripts/extractSchemas.d.ts +1 -0
- package/dist/scripts/extractSchemas.js +210 -0
- package/dist/scripts/extractSubSchemaWithDefs.d.ts +1 -0
- package/dist/scripts/extractSubSchemaWithDefs.js +187 -0
- package/dist/scripts/generateDependencies.d.ts +1 -0
- package/dist/scripts/generateDependencies.js +106 -0
- package/dist/scripts/generateResourceShells.d.ts +1 -0
- package/dist/scripts/generateResourceShells.js +91 -0
- package/dist/scripts/generateResourceTypeType.d.ts +1 -0
- package/dist/scripts/generateResourceTypeType.js +93 -0
- package/dist/scripts/generateSchemaShims.d.ts +1 -0
- package/dist/scripts/generateSchemaShims.js +105 -0
- package/dist/scripts/generateTypes.d.ts +1 -0
- package/dist/scripts/generateTypes.js +550 -0
- package/dist/scripts/rewriteAnchors.d.ts +1 -0
- package/dist/scripts/rewriteAnchors.js +96 -0
- package/package.json +45 -0
- package/src/Genesis.json +2043 -0
- package/src/Roadmap.json +102 -0
- package/src/generated/dependencies.json +299 -0
- package/src/generated/resourceTypes/Genesis.json +2043 -0
- package/src/generated/resourceTypes/Genesis.ts +2 -0
- package/src/generated/resources/Genesis.json +2962 -0
- package/src/generated/resources/Genesis.ts +2 -0
- package/src/generated/schemas/Genesis.json +1489 -0
- package/src/generated/schemas/Genesis.ts +2 -0
- package/src/generated/schemas/Goal.json +86 -0
- package/src/generated/schemas/Goal.ts +2 -0
- package/src/generated/schemas/Job.json +236 -0
- package/src/generated/schemas/Job.ts +2 -0
- package/src/generated/schemas/RawStrategy.json +667 -0
- package/src/generated/schemas/RawStrategy.ts +2 -0
- package/src/generated/schemas/ResourceType.json +140 -0
- package/src/generated/schemas/ResourceType.ts +2 -0
- package/src/generated/schemas/RunnableStrategy.json +737 -0
- package/src/generated/schemas/RunnableStrategy.ts +2 -0
- package/src/generated/schemas/StrategyRun.json +1025 -0
- package/src/generated/schemas/StrategyRun.ts +2 -0
- package/src/generated/types/Resource_Genesis.d.ts +3 -0
- package/src/generated/types/Resource_Genesis.js +1 -0
- package/src/generated/types/Resource_Job.d.ts +3 -0
- package/src/generated/types/Resource_Job.js +1 -0
- package/src/generated/types/Resource_RawStrategy.d.ts +3 -0
- package/src/generated/types/Resource_RawStrategy.js +1 -0
- package/src/generated/types/Resource_ResourceType.d.ts +3 -0
- package/src/generated/types/Resource_ResourceType.js +1 -0
- package/src/generated/types/Resource_RunnableStrategy.d.ts +3 -0
- package/src/generated/types/Resource_RunnableStrategy.js +1 -0
- package/src/generated/types/types.d.ts +1784 -0
- package/src/generated/types/types.js +1 -0
- package/src/index.ts +1 -0
- package/src/scripts/_lib/config.ts +181 -0
- package/src/scripts/extractSchemas.ts +229 -0
- package/src/scripts/extractSubSchemaWithDefs.ts +196 -0
- package/src/scripts/generateDependencies.ts +120 -0
- package/src/scripts/generateResourceShells.ts +105 -0
- package/src/scripts/generateResourceTypeType.ts +110 -0
- package/src/scripts/generateSchemaShims.ts +115 -0
- package/src/scripts/generateTypes.ts +615 -0
- package/src/scripts/rewriteAnchors.ts +123 -0
|
@@ -0,0 +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.getOutputPath("Genesis.json");
|
|
95
|
+
const outPath = config.getDependencyMapPath();
|
|
96
|
+
|
|
97
|
+
if (!fs.existsSync(inPath)) {
|
|
98
|
+
console.error(`Genesis schema not found at ${inPath}. Run extractSchemas 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
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { getConfig } from './_lib/config.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generate Resource envelopes for all ResourceTypes defined in Genesis.json
|
|
7
|
+
*
|
|
8
|
+
* This script wraps each ResourceType definition (from $defs) with a Resource envelope
|
|
9
|
+
* that conforms to the Resource.nucleusSchema pattern defined in Genesis.json.
|
|
10
|
+
*
|
|
11
|
+
* For the top-level Genesis ResourceType, extractedData is set to {} to avoid
|
|
12
|
+
* duplicating the entire $defs object.
|
|
13
|
+
*
|
|
14
|
+
* Resource identities follow the pattern RESOURCE-{Name} where Name is the key
|
|
15
|
+
* from the $defs object. Genesis itself uses RESOURCE-Genesis.
|
|
16
|
+
*
|
|
17
|
+
* Usage: node ./dist/scripts/generateResourceEnvelopes.js
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Pure function to generate resource envelopes from a Genesis schema.
|
|
21
|
+
*
|
|
22
|
+
* @param genesis The Genesis schema object
|
|
23
|
+
* @returns A record mapping resource names to their envelopes
|
|
24
|
+
*/
|
|
25
|
+
function generateResourceEnvelopesLogic(genesis: any): Record<string, any> {
|
|
26
|
+
if (!genesis.nucleusSchema || !genesis.nucleusSchema.$defs) {
|
|
27
|
+
throw new Error('Genesis.json must have nucleusSchema.$defs');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const defs = genesis.nucleusSchema.$defs;
|
|
31
|
+
const defKeys = Object.keys(defs);
|
|
32
|
+
|
|
33
|
+
// Generate Resource envelopes
|
|
34
|
+
const resources: Record<string, any> = {};
|
|
35
|
+
|
|
36
|
+
// Genesis timestamp: 2025-11-30T00:00:00.000Z marks the genesis of ToolProof
|
|
37
|
+
const genesisTimestamp = '2025-11-30T00:00:00.000Z';
|
|
38
|
+
|
|
39
|
+
// First entry is Genesis itself with empty extractedData
|
|
40
|
+
resources['Genesis'] = {
|
|
41
|
+
identity: 'RESOURCE-Genesis',
|
|
42
|
+
resourceTypeRef: 'TYPE-ResourceType',
|
|
43
|
+
creationContext: {
|
|
44
|
+
resourceRoleRef: 'ROLE-Genesis',
|
|
45
|
+
executionRef: 'EXECUTION-Genesis'
|
|
46
|
+
},
|
|
47
|
+
kind: 'materialized',
|
|
48
|
+
timestamp: genesisTimestamp,
|
|
49
|
+
extractedData: {}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Generate resources for all other $defs
|
|
53
|
+
defKeys.forEach((defName) => {
|
|
54
|
+
const defValue = defs[defName];
|
|
55
|
+
|
|
56
|
+
resources[defName] = {
|
|
57
|
+
identity: `RESOURCE-${defName}`,
|
|
58
|
+
resourceTypeRef: 'TYPE-ResourceType',
|
|
59
|
+
creationContext: {
|
|
60
|
+
resourceRoleRef: 'ROLE-Genesis',
|
|
61
|
+
executionRef: `EXECUTION-${defName}`
|
|
62
|
+
},
|
|
63
|
+
kind: 'materialized',
|
|
64
|
+
timestamp: genesisTimestamp,
|
|
65
|
+
extractedData: defValue
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return resources;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function main() {
|
|
73
|
+
const config = getConfig();
|
|
74
|
+
// Use normalized version with anchor refs rewritten to pointers
|
|
75
|
+
const genesisSourcePath = config.getNormalizedSourcePath();
|
|
76
|
+
const outputPath = path.join(config.getGeneratedResourcesDir(), 'Genesis.json');
|
|
77
|
+
|
|
78
|
+
if (!fs.existsSync(genesisSourcePath)) {
|
|
79
|
+
console.error(`Genesis source file not found at ${genesisSourcePath}`);
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const raw = fs.readFileSync(genesisSourcePath, 'utf-8');
|
|
84
|
+
const genesis = JSON.parse(raw);
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
const resources = generateResourceEnvelopesLogic(genesis);
|
|
88
|
+
|
|
89
|
+
// Ensure output directory exists
|
|
90
|
+
const outputDir = path.dirname(outputPath);
|
|
91
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
92
|
+
|
|
93
|
+
// Write the generated resources file
|
|
94
|
+
fs.writeFileSync(outputPath, JSON.stringify(resources, null, 4) + '\n', 'utf-8');
|
|
95
|
+
console.log(`Generated ${Object.keys(resources).length} Resource envelopes -> ${outputPath}`);
|
|
96
|
+
} catch (error: any) {
|
|
97
|
+
console.error(error.message);
|
|
98
|
+
process.exit(1);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
main().catch((e) => {
|
|
103
|
+
console.error(e);
|
|
104
|
+
process.exit(1);
|
|
105
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { getConfig } from './_lib/config.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generate a typed Resource variant where `extractedData` is typed to a specific schema
|
|
7
|
+
* extracted under the configured output directory.
|
|
8
|
+
*
|
|
9
|
+
* Usage: node ./dist/scripts/generateResourceTypeType.js --name Job
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Pure function to generate the typed Resource D.TS content.
|
|
13
|
+
*
|
|
14
|
+
* @param name The name of the schema
|
|
15
|
+
* @returns The TypeScript D.TS file content
|
|
16
|
+
*/
|
|
17
|
+
function generateResourceTypeTypeLogic(name: string): string {
|
|
18
|
+
const header = '// Auto-generated strict composite type. Do not edit.\n';
|
|
19
|
+
const ts =
|
|
20
|
+
`import type { ResourceMetaBase, ${name} as ExtractedData } from './types.js';\n` +
|
|
21
|
+
`export type Resource_${name} = ResourceMetaBase & { extractedData: ExtractedData };\n`;
|
|
22
|
+
return header + ts;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function main() {
|
|
26
|
+
const config = getConfig();
|
|
27
|
+
const { name } = parseArgs(process.argv.slice(2));
|
|
28
|
+
if (!name) {
|
|
29
|
+
console.error('Missing --name <SchemaBasename> argument');
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const inPath = config.getOutputPath(`${name}.json`);
|
|
34
|
+
|
|
35
|
+
if (!fs.existsSync(inPath)) {
|
|
36
|
+
console.error(`Schema file not found: ${inPath}`);
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Basic validation against the expected shape of nucleusSchema.
|
|
41
|
+
const raw = fs.readFileSync(inPath, 'utf8');
|
|
42
|
+
let parsed: any = null;
|
|
43
|
+
try {
|
|
44
|
+
parsed = JSON.parse(raw);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
console.error(`Failed to parse JSON schema ${inPath}:`, e);
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Minimal checks that roughly match the nucleusSchema constraints used elsewhere.
|
|
51
|
+
if (parsed.$schema && parsed.$schema !== 'https://json-schema.org/draft/2020-12/schema') {
|
|
52
|
+
console.warn(`Warning: schema $schema is '${parsed.$schema}', expected draft 2020-12. Proceeding anyway.`);
|
|
53
|
+
}
|
|
54
|
+
if (parsed.type && parsed.type !== 'object') {
|
|
55
|
+
console.warn(`Warning: nucleusSchema usually has type: 'object' but this schema has type: '${parsed.type}'. Proceeding.`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const tsContent = generateResourceTypeTypeLogic(name);
|
|
59
|
+
const jsContent = 'export {}\n';
|
|
60
|
+
|
|
61
|
+
// Output setup
|
|
62
|
+
const outName = `Resource_${name}.d.ts`;
|
|
63
|
+
const outJsName = `Resource_${name}.js`;
|
|
64
|
+
|
|
65
|
+
// Process src output
|
|
66
|
+
const outDir = config.getTypesSrcDir();
|
|
67
|
+
fs.mkdirSync(outDir, { recursive: true });
|
|
68
|
+
|
|
69
|
+
const outPath = config.getTypesSrcPath(outName);
|
|
70
|
+
const outJsPath = config.getTypesSrcPath(outJsName);
|
|
71
|
+
|
|
72
|
+
fs.writeFileSync(outPath, tsContent, 'utf8');
|
|
73
|
+
console.log(`Wrote ${outPath}`);
|
|
74
|
+
|
|
75
|
+
if (!fs.existsSync(outJsPath)) {
|
|
76
|
+
fs.writeFileSync(outJsPath, jsContent, 'utf8');
|
|
77
|
+
console.log(`Wrote ${outJsPath}`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Process dist output
|
|
81
|
+
const distLibDir = config.getTypesDistDir();
|
|
82
|
+
fs.mkdirSync(distLibDir, { recursive: true });
|
|
83
|
+
|
|
84
|
+
const distDtsPath = config.getTypesDistPath(outName);
|
|
85
|
+
const distJsPath = config.getTypesDistPath(outJsName);
|
|
86
|
+
|
|
87
|
+
fs.writeFileSync(distDtsPath, tsContent, 'utf8');
|
|
88
|
+
fs.writeFileSync(distJsPath, jsContent, 'utf8');
|
|
89
|
+
console.log(`Wrote ${distDtsPath}`);
|
|
90
|
+
console.log(`Wrote ${distJsPath}`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function parseArgs(args: string[]): { name?: string } {
|
|
94
|
+
let name: string | undefined;
|
|
95
|
+
for (let i = 0; i < args.length; i++) {
|
|
96
|
+
const a = args[i];
|
|
97
|
+
if (a === '--name') {
|
|
98
|
+
name = args[i + 1];
|
|
99
|
+
i++;
|
|
100
|
+
} else if (a.startsWith('--name=')) {
|
|
101
|
+
name = a.split('=')[1];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return { name };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
main().catch((e) => {
|
|
108
|
+
console.error(e);
|
|
109
|
+
process.exit(1);
|
|
110
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
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
|
+
* - src/genesis/generated/schemas/*.json (schema files)
|
|
14
|
+
* - src/genesis/generated/resources/*.json (resource envelope files)
|
|
15
|
+
*
|
|
16
|
+
* Usage: node ./dist/scripts/generateSchemaShims.js
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Pure function to generate the content of a TypeScript shim file.
|
|
20
|
+
*
|
|
21
|
+
* @param jsonFile The name of the JSON file to import
|
|
22
|
+
* @param variableName The name of the variable to use for the import
|
|
23
|
+
* @returns The TypeScript file content
|
|
24
|
+
*/
|
|
25
|
+
function generateShimContent(jsonFile: string, variableName: string): string {
|
|
26
|
+
return `import ${variableName} from './${jsonFile}' with { type: 'json' };\nexport default ${variableName};\n`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Pure function to map a list of JSON files to their corresponding TypeScript shim files and contents.
|
|
31
|
+
*
|
|
32
|
+
* @param jsonFiles List of JSON filenames
|
|
33
|
+
* @param variableName The variable name to use in the shim
|
|
34
|
+
* @returns A record mapping TS filenames to their contents
|
|
35
|
+
*/
|
|
36
|
+
function getShimsForFiles(jsonFiles: string[], variableName: string): Record<string, string> {
|
|
37
|
+
const shims: Record<string, string> = {};
|
|
38
|
+
for (const jsonFile of jsonFiles) {
|
|
39
|
+
const baseName = path.basename(jsonFile, '.json');
|
|
40
|
+
const tsFile = `${baseName}.ts`;
|
|
41
|
+
shims[tsFile] = generateShimContent(jsonFile, variableName);
|
|
42
|
+
}
|
|
43
|
+
return shims;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function main() {
|
|
47
|
+
const config = getConfig();
|
|
48
|
+
const schemasDir = config.getOutputDir();
|
|
49
|
+
const resourcesDir = config.getGeneratedResourcesDir();
|
|
50
|
+
const generatedResourceTypesDir = config.getNormalizedDir();
|
|
51
|
+
|
|
52
|
+
let totalCount = 0;
|
|
53
|
+
|
|
54
|
+
// Process schemas directory
|
|
55
|
+
if (fs.existsSync(schemasDir)) {
|
|
56
|
+
const files = fs.readdirSync(schemasDir);
|
|
57
|
+
const jsonFiles = files.filter(f => f.endsWith('.json') && !f.startsWith('.'));
|
|
58
|
+
|
|
59
|
+
const shims = getShimsForFiles(jsonFiles, 'schema');
|
|
60
|
+
|
|
61
|
+
for (const [tsFile, content] of Object.entries(shims)) {
|
|
62
|
+
const tsPath = path.join(schemasDir, tsFile);
|
|
63
|
+
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
64
|
+
console.log(`Generated ${tsFile} in ${schemasDir}`);
|
|
65
|
+
totalCount++;
|
|
66
|
+
}
|
|
67
|
+
console.log(`Generated ${jsonFiles.length} TypeScript schema shims in ${schemasDir}`);
|
|
68
|
+
} else {
|
|
69
|
+
console.warn(`Schemas directory not found at ${schemasDir}`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Process resources directory
|
|
73
|
+
if (fs.existsSync(resourcesDir)) {
|
|
74
|
+
const files = fs.readdirSync(resourcesDir);
|
|
75
|
+
const jsonFiles = files.filter(f => f.endsWith('.json') && !f.startsWith('.'));
|
|
76
|
+
|
|
77
|
+
const shims = getShimsForFiles(jsonFiles, 'resources');
|
|
78
|
+
|
|
79
|
+
for (const [tsFile, content] of Object.entries(shims)) {
|
|
80
|
+
const tsPath = path.join(resourcesDir, tsFile);
|
|
81
|
+
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
82
|
+
console.log(`Generated ${tsFile} in ${resourcesDir}`);
|
|
83
|
+
totalCount++;
|
|
84
|
+
}
|
|
85
|
+
console.log(`Generated ${jsonFiles.length} TypeScript resource shims in ${resourcesDir}`);
|
|
86
|
+
} else {
|
|
87
|
+
console.warn(`Resources directory not found at ${resourcesDir}`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Genesis (normalized) shim
|
|
91
|
+
try {
|
|
92
|
+
const genesisJsonPath = config.getNormalizedSourcePath();
|
|
93
|
+
if (fs.existsSync(genesisJsonPath)) {
|
|
94
|
+
fs.mkdirSync(generatedResourceTypesDir, { recursive: true });
|
|
95
|
+
const tsFile = 'Genesis.ts';
|
|
96
|
+
const content = generateShimContent('Genesis.json', 'schema');
|
|
97
|
+
const tsPath = path.join(generatedResourceTypesDir, tsFile);
|
|
98
|
+
|
|
99
|
+
fs.writeFileSync(tsPath, content, 'utf-8');
|
|
100
|
+
console.log(`Generated ${tsFile} in ${generatedResourceTypesDir}`);
|
|
101
|
+
totalCount++;
|
|
102
|
+
} else {
|
|
103
|
+
console.warn(`Genesis source JSON not found at ${genesisJsonPath}; skipping Genesis.ts shim`);
|
|
104
|
+
}
|
|
105
|
+
} catch (e) {
|
|
106
|
+
console.warn('Failed to generate Genesis.ts shim:', e);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
console.log(`Generated ${totalCount} total TypeScript shims`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
main().catch((e) => {
|
|
113
|
+
console.error(e);
|
|
114
|
+
process.exit(1);
|
|
115
|
+
});
|