-
Notifications
You must be signed in to change notification settings - Fork 1
/
quicktype.js
84 lines (77 loc) · 1.67 KB
/
quicktype.js
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
import {
quicktype,
jsonInputForTargetLanguage,
InputData,
} from "quicktype-core";
import fs from "fs/promises";
import path from "path";
const supportedLanguages = [
"cjson",
"c++",
"cr",
"cs",
"dart",
"elixir",
"elm",
"flow",
"go",
"haskell",
"java",
"javascript-prop-types",
"kotlin",
"objc",
"php",
"pike",
"py",
"rs",
"scala3",
"Smithy",
"swift",
"typescript",
"typescript-zod",
"typescript-effect-schema",
"javascript",
"cpp",
"csharp",
"rust",
"python",
"ruby",
];
async function generateTypes(language, jsonString) {
if (!supportedLanguages.includes(language)) {
throw new Error(
`Unsupported language: ${language}. Supported languages are: ${supportedLanguages.join(", ")}`,
);
}
const jsonInput = jsonInputForTargetLanguage(language);
await jsonInput.addSource({
name: "GeneratedType",
samples: [jsonString],
});
const inputData = new InputData();
inputData.addInput(jsonInput);
const result = await quicktype({
inputData,
lang: language,
rendererOptions: {
"just-types": true,
},
});
return result.lines.join("\n");
}
(async () => {
const language = process.argv[2];
const jsonFilePath = process.argv[3];
if (!language || !jsonFilePath) {
console.error("Usage: node generateTypes.js <language> <jsonFilePath>");
process.exit(1);
}
try {
const jsonFilePathResolved = path.resolve(jsonFilePath);
const jsonString = await fs.readFile(jsonFilePathResolved, "utf-8");
const types = await generateTypes(language, jsonString);
console.log(types);
} catch (error) {
console.error("Error generating types:", error);
}
})();