Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 70 additions & 90 deletions generate_schemas.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,37 @@
#!/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 process from "node:process";

// Third party imports
import { quicktype, InputData, JSONSchemaInput, FetchingJSONSchemaStore } from "quicktype-core";
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);

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`);

Expand All @@ -66,8 +47,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 })
Expand All @@ -77,59 +57,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 = {
Expand All @@ -141,10 +121,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;
}

Expand All @@ -154,7 +134,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));
}
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,9 +14,9 @@ marshmallow>=3
# via dataclasses-json
mypy-extensions>=1
# via typing-inspect
packaging==26.3
packaging>=26

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Top, les changements dans les actions marchent. On aura plus le problème de mise à jour qu'on a eu semaine dernière.

# via marshmallow
sqlalchemy>=2
sqlalchemy==2.0.43
# via -r requirements.in
typing-extensions>=4
# via
Expand Down
Loading