Skip to content

Commit 7e2271d

Browse files
committed
fix(Schema): generate types for schemas
1 parent a7e0897 commit 7e2271d

3 files changed

Lines changed: 863 additions & 18 deletions

File tree

generate_schemas.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
const fs = require("fs");
2-
const path = require("path");
3-
const glob = require("glob");
4-
const process = require("process");
1+
import fs from "fs";
2+
import path from "path";
3+
import { glob } from "glob";
4+
import process from "process";
5+
import {
6+
quicktype,
7+
InputData,
8+
JSONSchemaInput,
9+
FetchingJSONSchemaStore,
10+
} from "quicktype-core";
511

612
console.log("process.argv", process.argv);
713

@@ -46,7 +52,18 @@ const directoryPath = findDirectoryPath(projectName, folderName);
4652

4753
const outputFile = path.join(process.cwd(), `${projectName}_schemas.json`);
4854

49-
function return_json_schema(directoryPath, folder_path, projectName) {
55+
async function quicktypeJSONSchema(filename, jsonSchemaString) {
56+
const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());
57+
await schemaInput.addSource({ name: filename, schema: jsonSchemaString });
58+
const inputData = new InputData();
59+
inputData.addInput(schemaInput);
60+
return await quicktype({
61+
inputData,
62+
lang: "python",
63+
});
64+
}
65+
66+
async function return_json_schema(directoryPath, folder_path, projectName) {
5067
console.log("return_json_schema", directoryPath, folder_path, projectName);
5168

5269
const folders = fs
@@ -61,23 +78,19 @@ function return_json_schema(directoryPath, folder_path, projectName) {
6178
if (folder.name == "schemas") {
6279
const jsonFiles = glob.sync(path.join(folder.path, "**/*.json"));
6380
var schemas = {};
64-
jsonFiles.forEach((filePath) => {
81+
jsonFiles.forEach(async (filePath) => {
6582
try {
6683
const fileContent = fs.readFileSync(filePath, "utf8");
6784
var jsonData = JSON.parse(fileContent);
6885
var filename = filePath
6986
.replace(/^.*[\\/]/, "")
7087
.replace(/\.[^/.]+$/, "");
7188
var route = jsonData[key];
72-
console.log("FOLDER PATH", projectName);
7389
var values = [projectName, folder_path, route];
74-
console.log("values", values);
7590
values = values.map(function (x) {
76-
console.log("x", x);
7791
return x.replace("/", "").replace(".", "");
7892
}); // first replace first . / by empty string
7993
values = values.map(function (x) {
80-
console.log("x", x);
8194
return x.replaceAll("/", separator).replaceAll(".", separator);
8295
}); // then replace all . / by separator
8396
console.log("values", values);
@@ -87,6 +100,13 @@ function return_json_schema(directoryPath, folder_path, projectName) {
87100
})
88101
.join(separator);
89102
schemas[filename] = jsonData;
103+
const { lines: jsonTypes } = await quicktypeJSONSchema(
104+
filename,
105+
fileContent
106+
);
107+
const pythonContent = "# type: ignore\n" + jsonTypes.join("\n");
108+
const pythonFile = path.join(folder.path, filename + ".py");
109+
fs.writeFileSync(pythonFile, pythonContent);
90110
} catch (error) {
91111
console.error(
92112
`Erreur lors de la lecture du fichier ${filePath}:`,
@@ -116,7 +136,11 @@ if (fs.existsSync(outputFile)) {
116136
fs.unlinkSync(outputFile);
117137
}
118138

119-
const finalJson = {};
120-
finalJson[projectName] = return_json_schema(directoryPath, "", projectName);
139+
async function main() {
140+
const finalJson = {};
141+
finalJson[projectName] = return_json_schema(directoryPath, "", projectName);
142+
143+
fs.writeFileSync(outputFile, JSON.stringify(finalJson, null, 2));
144+
}
121145

122-
fs.writeFileSync(outputFile, JSON.stringify(finalJson, null, 2));
146+
main();

0 commit comments

Comments
 (0)