-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathts.js
More file actions
52 lines (39 loc) · 1.43 KB
/
ts.js
File metadata and controls
52 lines (39 loc) · 1.43 KB
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
const Handlebars = require("handlebars");
const { readFileSync } = require("fs");
const { outputFile } = require("fs-extra");
const OUTPUT_TYPE = "interface"; // "interface" | "class" | "type"
const files = {
interface: "template.hbs",
class: "templateClass.hbs",
type: "templateType.hbs",
}
const template = Handlebars.compile(readFileSync(files[OUTPUT_TYPE]).toString());
const TYPE_CONVERSIONS = {
"String": "string",
"Integer": "number",
"Float": "number",
"Float number": "number",
"Boolean": "boolean",
"True": "true",
"False": "false",
}
let data = readFileSync("./data.json").toString();
// Primitive type conversions
Object.keys(TYPE_CONVERSIONS).forEach((key) => {
const replaceWith = TYPE_CONVERSIONS[key];
data = data.replace(new RegExp(key, "g"), replaceWith);
});
// Array type conversions
data = data.replace(/"Array of (\w+)"/g, `"$1[]"`);
data = data.replace(/"Array of Array of (\w+)"/g, `"$1[][]"`);
// Mixed types
data = data.replace(/"(\w+) or (\w+)"/g, `"$1 | $2"`);
const dataObj = JSON.parse(data);
dataObj.schemas.forEach((schema) => {
// Types found in document get imported
const imports = dataObj.schemas
.filter(impSchema => JSON.stringify(schema.fields.map(f=>f.type)).includes(impSchema.name))
.filter(s => s.name != schema.name)
outputFile(`typescript/${schema.category}/${schema.name}.ts`, template({ schema: schema, imports: imports }))
.catch((err) => console.error(err));
});