From 18cff02a1f33790ac75240143acabde96e2c2105 Mon Sep 17 00:00:00 2001 From: MaxNumerique Date: Fri, 12 Sep 2025 11:27:57 +0200 Subject: [PATCH 1/4] fix(generateSchemas): microservice now generate schemas --- .github/workflows/deploy.yml | 3 +- .github/workflows/test.yml | 2 +- .github/workflows/test_pr.yml | 2 +- generate_schemas.js | 122 ++++++++++++++++++++++++++++++++++ package.json | 35 ++++++++++ requirements.txt | 3 +- 6 files changed, 162 insertions(+), 5 deletions(-) create mode 100644 generate_schemas.js create mode 100644 package.json diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index df0e06a..7d6699d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -7,6 +7,5 @@ jobs: deploy: uses: Geode-solutions/actions/.github/workflows/py-deploy.yml@master with: - npm: false + npm: true secrets: inherit - \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1fe7fd3..22c4bdb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,5 +10,5 @@ jobs: test: uses: Geode-solutions/actions/.github/workflows/py-test.yml@master with: - npm: false + npm: true secrets: inherit diff --git a/.github/workflows/test_pr.yml b/.github/workflows/test_pr.yml index cd54d43..43d7b73 100644 --- a/.github/workflows/test_pr.yml +++ b/.github/workflows/test_pr.yml @@ -8,5 +8,5 @@ jobs: test: uses: Geode-solutions/actions/.github/workflows/py-test-pr.yml@master with: - npm: false + npm: true secrets: inherit diff --git a/generate_schemas.js b/generate_schemas.js new file mode 100644 index 0000000..61b74fb --- /dev/null +++ b/generate_schemas.js @@ -0,0 +1,122 @@ +const fs = require("fs"); +const path = require("path"); +const glob = require("glob"); +const process = require("process"); + +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 directoryPath = findDirectoryPath(projectName, folderName); + +const outputFile = path.join(process.cwd(), `${projectName}_schemas.json`); + +function return_json_schema(directoryPath, folder_path, projectName) { + console.log("return_json_schema", directoryPath, folder_path, projectName); + + const folders = fs + .readdirSync(path.normalize(directoryPath), { withFileTypes: true }) + .filter((folder) => folder.isDirectory() && folder.name != "__pycache__") + .map((folder) => ({ + name: folder.name, + path: path.join(directoryPath, folder.name), + })); + var folders_schemas = {}; + folders.forEach((folder) => { + if (folder.name == "schemas") { + const jsonFiles = glob.sync(path.join(folder.path, "**/*.json")); + var schemas = {}; + jsonFiles.forEach((filePath) => { + try { + const fileContent = fs.readFileSync(filePath, "utf8"); + var jsonData = JSON.parse(fileContent); + var filename = filePath + .replace(/^.*[\\/]/, "") + .replace(/\.[^/.]+$/, ""); + var route = jsonData[key]; + console.log("FOLDER PATH", projectName); + var values = [projectName, folder_path, route]; + console.log("values", values); + values = values.map(function (x) { + console.log("x", x); + return x.replace("/", "").replace(".", ""); + }); // first replace first . / by empty string + values = values.map(function (x) { + console.log("x", x); + return x.replaceAll("/", separator).replaceAll(".", separator); + }); // then replace all . / by separator + console.log("values", values); + jsonData["$id"] = values + .filter(function (val) { + return val; + }) + .join(separator); + schemas[filename] = jsonData; + } catch (error) { + console.error( + `Erreur lors de la lecture du fichier ${filePath}:`, + error + ); + } + }); + folders_schemas = Object.keys(schemas).reduce((acc, key) => { + const currentSchema = schemas[key]; + const modifiedSchema = { + $id: path.join(folder_path, currentSchema["$id"]), + ...currentSchema, + }; + acc[key] = modifiedSchema; + return acc; + }, folders_schemas); + } else { + var new_folder_path = folder_path + "/" + folder.name; + var test = return_json_schema(folder.path, new_folder_path, projectName); + folders_schemas[folder.name] = test; + } + }); + return folders_schemas; +} + +if (fs.existsSync(outputFile)) { + fs.unlinkSync(outputFile); +} + +const finalJson = {}; +finalJson[projectName] = return_json_schema(directoryPath, "", projectName); + +fs.writeFileSync(outputFile, JSON.stringify(finalJson, null, 2)); diff --git a/package.json b/package.json new file mode 100644 index 0000000..3b86be0 --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "@geode/opengeodeweb-microservice", + "version": "0.0.0", + "description": "Shared utilities and schema generator for OpenGeodeWeb ecosystem", + "main": "generate_schemas.js", + "bin": { + "ogw-generate-schemas": "./generate_schemas.js" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "dependencies": { + "glob": "^11.0.3" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Geode-solutions/OpenGeodeWeb-Microservice.git" + }, + "author": { + "name": "Geode-solutions", + "email": "contact@geode-solutions.com", + "url": "https://geode-solutions.com/" + }, + "license": "MIT", + "bugs": { + "url": "https://github.com/Geode-solutions/OpenGeodeWeb-Microservice/issues" + }, + "homepage": "https://github.com/Geode-solutions/OpenGeodeWeb-Microservice", + "publishConfig": { + "access": "public" + }, + "files": [ + "generate_schemas.js" + ] +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 939b65c..1bf3b13 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ SQLAlchemy==2.0.43 -Flask-SQLAlchemy==3.1.1 \ No newline at end of file +Flask-SQLAlchemy==3.1.1 +fastjsonschema==2.16.2 \ No newline at end of file From 60e029f32c2087658480b82ae7d589b8022d8365 Mon Sep 17 00:00:00 2001 From: MaxNumerique Date: Fri, 12 Sep 2025 11:35:31 +0200 Subject: [PATCH 2/4] update package.json --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3b86be0..7ea6d58 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,9 @@ "ogw-generate-schemas": "./generate_schemas.js" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "npm run json", + "build": "npm run json", + "json": "node generate_schemas.js" }, "dependencies": { "glob": "^11.0.3" From fa69669173c498a08fda2c6d7083b1b966d653d6 Mon Sep 17 00:00:00 2001 From: MaxNumerique Date: Fri, 12 Sep 2025 11:40:27 +0200 Subject: [PATCH 3/4] update package.json --- package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 7ea6d58..f91ea0b 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,8 @@ "ogw-generate-schemas": "./generate_schemas.js" }, "scripts": { - "test": "npm run json", - "build": "npm run json", - "json": "node generate_schemas.js" + "test": "echo \"Error: no test specified\" && exit 0", + "build": "echo \"Error: no test specified\" && exit 0" }, "dependencies": { "glob": "^11.0.3" From 5c1f7286d103d94c193c7c105a2a62d082bea2bf Mon Sep 17 00:00:00 2001 From: MaxNumerique Date: Fri, 12 Sep 2025 11:49:51 +0200 Subject: [PATCH 4/4] update fastjsonschema to fit viewer's version --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 1bf3b13..e04ccbd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ SQLAlchemy==2.0.43 Flask-SQLAlchemy==3.1.1 -fastjsonschema==2.16.2 \ No newline at end of file +fastjsonschema==2.21.1 \ No newline at end of file