From 80687090dc58068875c521a703a0ea8d5fba5f97 Mon Sep 17 00:00:00 2001 From: JulienChampagnol Date: Wed, 12 Aug 2026 14:04:03 +0200 Subject: [PATCH 1/3] fix(SchemasGeneration): names args --- generate_schemas.js | 158 +++++++++++++++++++------------------------- 1 file changed, 68 insertions(+), 90 deletions(-) diff --git a/generate_schemas.js b/generate_schemas.js index f0055ef..cbd0697 100755 --- a/generate_schemas.js +++ b/generate_schemas.js @@ -1,56 +1,35 @@ #!/usr/bin/env node -import fs from "fs"; -import path from "path"; +// Node imports +import fs from "node:fs"; +import path from "node:path"; +import { parseArgs } from "node:util" import { glob } from "glob"; -import process from "process"; -import { - quicktype, - InputData, - JSONSchemaInput, - FetchingJSONSchemaStore, -} from "quicktype-core"; - -console.log("process.argv", process.argv); - -var projectName = process.argv[2]; -console.log("projectName", projectName); -var folderName = process.argv[3]; -console.log("folderName", folderName); -var key = process.argv[4]; -console.log("key", key); -var separator = process.argv[5]; -console.log("separator", separator); - -const findDirectoryPath = (targetDirectoryName, folderName) => { - const pathToCheck = path.join( - process.cwd(), - "/src", - "/", - targetDirectoryName - ); - console.log("pathToCheck", pathToCheck); +import process from "node:process"; +import { quicktype, InputData, JSONSchemaInput, FetchingJSONSchemaStore } from "quicktype-core"; - const folders = fs - .readdirSync(pathToCheck, { withFileTypes: true }) - .filter( - (folder) => - folder.isDirectory() && - !folder.name.endsWith(".egg-info") && - folder.name != "tests" && - folder.name != "__pycache__" && - folder.name.includes(folderName) - ) - .map((folder) => ({ - name: folder.name, - path: path.join(pathToCheck, folder.name), - })); - console.log("folders", folders); - const routesDirectory = path.join(folders[0].path); - return routesDirectory; -}; +const projectName = path.basename(process.cwd()).toLowerCase().replaceAll("-", "_"); + +const args = parseArgs({ + options: { + startDir: { type: "string" }, + key: { type: "string" }, + separator: { type: "string" }, + prefix: { type: "string", default: projectName }, + }, +}); + +console.log({ args }); +const startDir = args.values.startDir; +const key = args.values.key; +const separator = args.values.separator; +const prefix = args.values.prefix; -const directoryPath = findDirectoryPath(projectName, folderName); +const generatePython = startDir.split(path.sep).includes("src"); +console.log("generatePython", generatePython); + +const directoryPath = path.resolve(process.cwd(), startDir); +console.log("directoryPath", directoryPath); const outputFile = path.join(process.cwd(), `${projectName}_schemas.json`); @@ -66,8 +45,7 @@ async function quicktypeJSONSchema(filename, jsonSchemaString) { }); } -function return_json_schema(directoryPath, folder_path, projectName) { - console.log("return_json_schema", directoryPath, folder_path, projectName); +async function return_json_schema(directoryPath, folder_path, prefix) { const folders = fs .readdirSync(path.normalize(directoryPath), { withFileTypes: true }) @@ -77,59 +55,59 @@ function return_json_schema(directoryPath, folder_path, projectName) { path: path.join(directoryPath, folder.name), })); var folders_schemas = {}; - folders.forEach((folder) => { + for (const folder of folders) { if (folder.name == "schemas") { - fs.readdirSync(folder.path) - .filter((f) => path.extname(f).toLowerCase() === ".py") - .forEach((f) => fs.unlinkSync(path.join(folder.path, f))); + if (generatePython) { + fs.readdirSync(folder.path) + .filter((file) => path.extname(file).toLowerCase() === ".py") + .forEach((file) => fs.unlinkSync(path.join(folder.path, file))); + } + const jsonFiles = glob.sync(path.join(folder.path, "**/*.json")); var schemas = {}; let initContent = ""; - jsonFiles.forEach(async (filePath) => { + for (const filePath of jsonFiles) { try { const fileContent = fs.readFileSync(filePath, "utf8"); var jsonData = JSON.parse(fileContent); - var filename = filePath - .replace(/^.*[\\/]/, "") - .replace(/\.[^/.]+$/, ""); + var filename = filePath.replace(/^.*[\\/]/, "").replace(/\.[^/.]+$/, ""); var route = jsonData[key]; - var values = [projectName, folder_path, route]; - values = values.map(function (x) { - return x.replace("/", "").replace(".", ""); - }); // first replace first . / by empty string - values = values.map(function (x) { - return x.replaceAll("/", separator).replaceAll(".", separator); - }); // then replace all . / by separator - console.log("values", values); + var values = [prefix, folder_path, route]; + values = values.map(function (value) { + return value.replace("/", "").replace(".", ""); + }); + values = values.map(function (value) { + return value.replaceAll("/", separator).replaceAll(".", separator); + }); jsonData["$id"] = values .filter(function (val) { return val; }) .join(separator); schemas[filename] = jsonData; - initContent += "from ." + filename + " import *\n"; - const { lines: jsonTypes } = await quicktypeJSONSchema( - filename, - fileContent - ); - let pythonContent = - "from dataclasses_json import DataClassJsonMixin\n" + - jsonTypes.join("\n"); - pythonContent = pythonContent.replace( - /@dataclass\nclass (\w+)(?:\s*\([^)]*\))?\s*:/g, - "@dataclass\nclass $1(DataClassJsonMixin):\n def __post_init__(self) -> None:\n print(self, flush=True)\n" - ); - const pythonFile = path.join(folder.path, filename + ".py"); - const initFile = path.join(folder.path, "__init__.py"); - fs.writeFileSync(pythonFile, pythonContent); - fs.writeFileSync(initFile, initContent); + + if (generatePython) { + initContent += "from ." + filename + " import *\n"; + const { lines: jsonTypes } = await quicktypeJSONSchema(filename, fileContent); + let pythonContent = + "from dataclasses_json import DataClassJsonMixin\n" + jsonTypes.join("\n"); + pythonContent = pythonContent.replace( + /@dataclass\nclass (\w+)(?:\s*\([^)]*\))?\s*:/g, + "@dataclass\nclass $1(DataClassJsonMixin):\n def __post_init__(self) -> None:\n print(self, flush=True)\n", + ); + const pythonFile = path.join(folder.path, filename + ".py"); + fs.writeFileSync(pythonFile, pythonContent); + } } catch (error) { - console.error( - `Erreur lors de la lecture du fichier ${filePath}:`, - error - ); + console.error(`Erreur lors de la lecture du fichier ${filePath}:`, error); } - }); + } + + if (generatePython) { + const initFile = path.join(folder.path, "__init__.py"); + fs.writeFileSync(initFile, initContent); + } + folders_schemas = Object.keys(schemas).reduce((acc, key) => { const currentSchema = schemas[key]; const modifiedSchema = { @@ -141,10 +119,10 @@ function return_json_schema(directoryPath, folder_path, projectName) { }, folders_schemas); } else { var new_folder_path = folder_path + "/" + folder.name; - var test = return_json_schema(folder.path, new_folder_path, projectName); + var test = await return_json_schema(folder.path, new_folder_path, prefix); folders_schemas[folder.name] = test; } - }); + } return folders_schemas; } @@ -154,7 +132,7 @@ if (fs.existsSync(outputFile)) { async function main() { const finalJson = {}; - finalJson[projectName] = return_json_schema(directoryPath, "", projectName); + finalJson[prefix] = await return_json_schema(directoryPath, "", prefix); console.log("FINAL", outputFile, finalJson); fs.writeFileSync(outputFile, JSON.stringify(finalJson, null, 2)); } From 1bc0ccabb9b8c318c78804e769dc847cbb3a2796 Mon Sep 17 00:00:00 2001 From: JulienChampagnol Date: Wed, 12 Aug 2026 14:24:51 +0200 Subject: [PATCH 2/3] imports --- generate_schemas.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/generate_schemas.js b/generate_schemas.js index cbd0697..2867da9 100755 --- a/generate_schemas.js +++ b/generate_schemas.js @@ -4,9 +4,11 @@ import fs from "node:fs"; import path from "node:path"; import { parseArgs } from "node:util" -import { glob } from "glob"; import process from "node:process"; + +// Third party imports import { quicktype, InputData, JSONSchemaInput, FetchingJSONSchemaStore } from "quicktype-core"; +import { glob } from "glob"; const projectName = path.basename(process.cwd()).toLowerCase().replaceAll("-", "_"); From 99985b88ca9575c592b9af30c3d17971a4125e72 Mon Sep 17 00:00:00 2001 From: JulienChampagnol <91873154+JulienChampagnol@users.noreply.github.com> Date: Wed, 12 Aug 2026 12:25:44 +0000 Subject: [PATCH 3/3] Apply prepare changes --- requirements.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index 9d095f2..e76aa5e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,7 @@ # # pip-compile --output-file=./requirements.txt ./requirements.in # -dataclasses-json>=0 +dataclasses-json==0.6.7 # via -r requirements.in fastjsonschema==2.21.1 # via -r requirements.in @@ -14,9 +14,9 @@ marshmallow>=3 # via dataclasses-json mypy-extensions>=1 # via typing-inspect -packaging==26.3 +packaging>=26 # via marshmallow -sqlalchemy>=2 +sqlalchemy==2.0.43 # via -r requirements.in typing-extensions>=4 # via