-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-default-properties.mjs
45 lines (37 loc) · 1.2 KB
/
extract-default-properties.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { createGenerator } from "ts-json-schema-generator";
import { writeFileSync } from "fs";
/** @type {import('ts-json-schema-generator/dist/src/Config').Config} */
const config = {
tsconfig: "./tsconfig.json",
type: "InSchema",
sortProps: false,
};
const schema = createGenerator(config).createSchema(config.type);
function generateDefaults(schema) {
function generateProps(properties) {
const lines = [];
for (const [key, value] of Object.entries(properties)) {
if (value.default !== undefined) {
lines.push(` ${key}: ${JSON.stringify(value.default)}`);
} else {
console.warn(`Warning: No default defined for '${key}'`);
}
}
return lines;
}
const jsCode = `
// Automatically generated by extract-default-properties.mjs
import {
type LayoutProperties,
type CostWeights
} from "./layout.js";
export const DEFAULT_PROPERTIES = {
${generateProps(schema.definitions["LayoutProperties"].properties).join(",\n")}
} as LayoutProperties;
export const DEFAULT_COST_WEIGHTS = {
${generateProps(schema.definitions["CostWeights"].properties).join(",\n")}
} as CostWeights;
`;
return jsCode;
}
writeFileSync("src/defaultProperties.ts", generateDefaults(schema));