-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerate-schema.mjs
117 lines (97 loc) · 3.87 KB
/
generate-schema.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { readFileSync, writeFileSync, unlinkSync } from "fs";
import * as tjs from "ts-json-schema-generator";
const deviceFile = './src/device.ts';
const individualSchemas = {
'notification': {
type: 'WebpushNotification',
path: './src/notification.ts',
},
'camera-result': {
type: 'CameraStreamResult',
path: './src/device.ts',
},
'object-detection-notification': {
type: 'ObjectDetectionNotification',
path: './src/device-notification.ts',
},
'runcycle-notification': {
type: 'RunCycleNotification',
path: './src/device-notification.ts',
},
};
const PROPS_TO_REMOVE = ['required', 'minProperties'];
console.log(`loading all traits`);
const traitSchema = tjs.createGenerator({ path: deviceFile }).createSchema('Trait');
const traitNames = traitSchema.definitions['Trait'].enum.map(trait => trait.substring(trait.lastIndexOf('.') + 1));
console.log(`${traitNames.join(', ')}`);
console.log(`creating temp source file`);
let file = readFileSync(deviceFile).toString();
traitNames.forEach(traitName => file += `\nexport type ${traitName}State = ${traitName}Device['state'];\n`);
const tempFile = 'device-temp.ts';
writeFileSync(tempFile, file);
console.log(`created`);
try {
const generator = tjs.createGenerator({ path: tempFile, topRef: false });
const schemas = { device: {}, state: {}, 'state-update': {} };
for (const traitName of traitNames) {
console.log(`generating schema for trait: ${traitName}`)
const deviceSchema = deepClone(generator.createSchema(`${traitName}Device`));
deviceSchema.definitions['Trait'].enum = deviceSchema.definitions['Trait'].enum.filter(f => f.endsWith(traitName));
schemas.device[traitName.toLowerCase()] = removePropertiesFromSchema(deviceSchema);
const stateSchema = deepClone(generator.createSchema(`${traitName}State`));
const updateStateSchema = removePropertiesFromStateUpdateSchema(deepClone(stateSchema));
schemas.state[traitName.toLowerCase()] = removePropertiesFromSchema(stateSchema);
schemas["state-update"][traitName.toLowerCase()] = updateStateSchema;
}
const individualSchema = Object.entries(individualSchemas).reduce((dict, [key, value]) => {
dict[key] = tjs
.createGenerator({ path: value.path, topRef: false })
.createSchema(value.type);
return dict;
}, {});
const schema = `/* This file is auto-generated during build. Don't modify it! any changes will be overwritten. */
/* tslint:disable */
export type SchemaType = keyof typeof Schema;
export type IndividualSchemaType = keyof typeof IndividualSchema;
export type TraitName = keyof typeof Schema['device'];
export const Schema = ${JSON.stringify(schemas, undefined, 2)} as const;
export const IndividualSchema = ${JSON.stringify(individualSchema, undefined, 2)} as const;
`;
writeFileSync('./src/schema.ts', schema);
} finally {
unlinkSync(tempFile)
}
function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
function removeRecursive(obj, remover) {
if (typeof obj === 'object') {
if (Array.isArray(obj)) {
for (const child of obj) {
removeRecursive(child, remover);
}
} else {
remover(obj);
for (const [_, child] of Object.entries(obj)) {
removeRecursive(child, remover);
}
}
}
return obj;
}
function removePropertiesFromSchema(obj) {
return removeRecursive(obj, item => {
if (item.type !== 'object' && 'minProperties' in item) {
delete item.minProperties;
}
});
}
function removePropertiesFromStateUpdateSchema(obj) {
return removeRecursive(obj, item => {
for (const toRemove of PROPS_TO_REMOVE) {
if (toRemove in item) {
delete item[toRemove];
}
}
});
}