Skip to content

Commit 18cff02

Browse files
committed
fix(generateSchemas): microservice now generate schemas
1 parent 78c664b commit 18cff02

File tree

6 files changed

+162
-5
lines changed

6 files changed

+162
-5
lines changed

.github/workflows/deploy.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ jobs:
77
deploy:
88
uses: Geode-solutions/actions/.github/workflows/py-deploy.yml@master
99
with:
10-
npm: false
10+
npm: true
1111
secrets: inherit
12-

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ jobs:
1010
test:
1111
uses: Geode-solutions/actions/.github/workflows/py-test.yml@master
1212
with:
13-
npm: false
13+
npm: true
1414
secrets: inherit

.github/workflows/test_pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ jobs:
88
test:
99
uses: Geode-solutions/actions/.github/workflows/py-test-pr.yml@master
1010
with:
11-
npm: false
11+
npm: true
1212
secrets: inherit

generate_schemas.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
const glob = require("glob");
4+
const process = require("process");
5+
6+
console.log("process.argv", process.argv);
7+
8+
var projectName = process.argv[2];
9+
console.log("projectName", projectName);
10+
var folderName = process.argv[3];
11+
console.log("folderName", folderName);
12+
var key = process.argv[4];
13+
console.log("key", key);
14+
var separator = process.argv[5];
15+
console.log("separator", separator);
16+
17+
const findDirectoryPath = (targetDirectoryName, folderName) => {
18+
const pathToCheck = path.join(
19+
process.cwd(),
20+
"/src",
21+
"/",
22+
targetDirectoryName
23+
);
24+
console.log("pathToCheck", pathToCheck);
25+
26+
const folders = fs
27+
.readdirSync(pathToCheck, { withFileTypes: true })
28+
.filter(
29+
(folder) =>
30+
folder.isDirectory() &&
31+
!folder.name.endsWith(".egg-info") &&
32+
folder.name != "tests" &&
33+
folder.name != "__pycache__" &&
34+
folder.name.includes(folderName)
35+
)
36+
.map((folder) => ({
37+
name: folder.name,
38+
path: path.join(pathToCheck, folder.name),
39+
}));
40+
console.log("folders", folders);
41+
const routesDirectory = path.join(folders[0].path);
42+
return routesDirectory;
43+
};
44+
45+
const directoryPath = findDirectoryPath(projectName, folderName);
46+
47+
const outputFile = path.join(process.cwd(), `${projectName}_schemas.json`);
48+
49+
function return_json_schema(directoryPath, folder_path, projectName) {
50+
console.log("return_json_schema", directoryPath, folder_path, projectName);
51+
52+
const folders = fs
53+
.readdirSync(path.normalize(directoryPath), { withFileTypes: true })
54+
.filter((folder) => folder.isDirectory() && folder.name != "__pycache__")
55+
.map((folder) => ({
56+
name: folder.name,
57+
path: path.join(directoryPath, folder.name),
58+
}));
59+
var folders_schemas = {};
60+
folders.forEach((folder) => {
61+
if (folder.name == "schemas") {
62+
const jsonFiles = glob.sync(path.join(folder.path, "**/*.json"));
63+
var schemas = {};
64+
jsonFiles.forEach((filePath) => {
65+
try {
66+
const fileContent = fs.readFileSync(filePath, "utf8");
67+
var jsonData = JSON.parse(fileContent);
68+
var filename = filePath
69+
.replace(/^.*[\\/]/, "")
70+
.replace(/\.[^/.]+$/, "");
71+
var route = jsonData[key];
72+
console.log("FOLDER PATH", projectName);
73+
var values = [projectName, folder_path, route];
74+
console.log("values", values);
75+
values = values.map(function (x) {
76+
console.log("x", x);
77+
return x.replace("/", "").replace(".", "");
78+
}); // first replace first . / by empty string
79+
values = values.map(function (x) {
80+
console.log("x", x);
81+
return x.replaceAll("/", separator).replaceAll(".", separator);
82+
}); // then replace all . / by separator
83+
console.log("values", values);
84+
jsonData["$id"] = values
85+
.filter(function (val) {
86+
return val;
87+
})
88+
.join(separator);
89+
schemas[filename] = jsonData;
90+
} catch (error) {
91+
console.error(
92+
`Erreur lors de la lecture du fichier ${filePath}:`,
93+
error
94+
);
95+
}
96+
});
97+
folders_schemas = Object.keys(schemas).reduce((acc, key) => {
98+
const currentSchema = schemas[key];
99+
const modifiedSchema = {
100+
$id: path.join(folder_path, currentSchema["$id"]),
101+
...currentSchema,
102+
};
103+
acc[key] = modifiedSchema;
104+
return acc;
105+
}, folders_schemas);
106+
} else {
107+
var new_folder_path = folder_path + "/" + folder.name;
108+
var test = return_json_schema(folder.path, new_folder_path, projectName);
109+
folders_schemas[folder.name] = test;
110+
}
111+
});
112+
return folders_schemas;
113+
}
114+
115+
if (fs.existsSync(outputFile)) {
116+
fs.unlinkSync(outputFile);
117+
}
118+
119+
const finalJson = {};
120+
finalJson[projectName] = return_json_schema(directoryPath, "", projectName);
121+
122+
fs.writeFileSync(outputFile, JSON.stringify(finalJson, null, 2));

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@geode/opengeodeweb-microservice",
3+
"version": "0.0.0",
4+
"description": "Shared utilities and schema generator for OpenGeodeWeb ecosystem",
5+
"main": "generate_schemas.js",
6+
"bin": {
7+
"ogw-generate-schemas": "./generate_schemas.js"
8+
},
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"dependencies": {
13+
"glob": "^11.0.3"
14+
},
15+
"repository": {
16+
"type": "git",
17+
"url": "git+https://github.com/Geode-solutions/OpenGeodeWeb-Microservice.git"
18+
},
19+
"author": {
20+
"name": "Geode-solutions",
21+
"email": "[email protected]",
22+
"url": "https://geode-solutions.com/"
23+
},
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/Geode-solutions/OpenGeodeWeb-Microservice/issues"
27+
},
28+
"homepage": "https://github.com/Geode-solutions/OpenGeodeWeb-Microservice",
29+
"publishConfig": {
30+
"access": "public"
31+
},
32+
"files": [
33+
"generate_schemas.js"
34+
]
35+
}

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
SQLAlchemy==2.0.43
2-
Flask-SQLAlchemy==3.1.1
2+
Flask-SQLAlchemy==3.1.1
3+
fastjsonschema==2.16.2

0 commit comments

Comments
 (0)