@geode/opengeodeweb-microservice 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/generate_schemas.js +47 -12
- package/package.json +5 -6
package/generate_schemas.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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";
|
|
5
11
|
|
|
6
12
|
console.log("process.argv", process.argv);
|
|
7
13
|
|
|
@@ -46,6 +52,18 @@ const directoryPath = findDirectoryPath(projectName, folderName);
|
|
|
46
52
|
|
|
47
53
|
const outputFile = path.join(process.cwd(), `${projectName}_schemas.json`);
|
|
48
54
|
|
|
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
|
+
rendererOptions: { "just-types": true, "python-version": "3.7" },
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
49
67
|
function return_json_schema(directoryPath, folder_path, projectName) {
|
|
50
68
|
console.log("return_json_schema", directoryPath, folder_path, projectName);
|
|
51
69
|
|
|
@@ -61,7 +79,8 @@ function return_json_schema(directoryPath, folder_path, projectName) {
|
|
|
61
79
|
if (folder.name == "schemas") {
|
|
62
80
|
const jsonFiles = glob.sync(path.join(folder.path, "**/*.json"));
|
|
63
81
|
var schemas = {};
|
|
64
|
-
|
|
82
|
+
let initContent = "";
|
|
83
|
+
jsonFiles.forEach(async (filePath) => {
|
|
65
84
|
try {
|
|
66
85
|
const fileContent = fs.readFileSync(filePath, "utf8");
|
|
67
86
|
var jsonData = JSON.parse(fileContent);
|
|
@@ -69,15 +88,11 @@ function return_json_schema(directoryPath, folder_path, projectName) {
|
|
|
69
88
|
.replace(/^.*[\\/]/, "")
|
|
70
89
|
.replace(/\.[^/.]+$/, "");
|
|
71
90
|
var route = jsonData[key];
|
|
72
|
-
console.log("FOLDER PATH", projectName);
|
|
73
91
|
var values = [projectName, folder_path, route];
|
|
74
|
-
console.log("values", values);
|
|
75
92
|
values = values.map(function (x) {
|
|
76
|
-
console.log("x", x);
|
|
77
93
|
return x.replace("/", "").replace(".", "");
|
|
78
94
|
}); // first replace first . / by empty string
|
|
79
95
|
values = values.map(function (x) {
|
|
80
|
-
console.log("x", x);
|
|
81
96
|
return x.replaceAll("/", separator).replaceAll(".", separator);
|
|
82
97
|
}); // then replace all . / by separator
|
|
83
98
|
console.log("values", values);
|
|
@@ -87,6 +102,22 @@ function return_json_schema(directoryPath, folder_path, projectName) {
|
|
|
87
102
|
})
|
|
88
103
|
.join(separator);
|
|
89
104
|
schemas[filename] = jsonData;
|
|
105
|
+
initContent += "from ." + filename + " import *\n";
|
|
106
|
+
const { lines: jsonTypes } = await quicktypeJSONSchema(
|
|
107
|
+
filename,
|
|
108
|
+
fileContent
|
|
109
|
+
);
|
|
110
|
+
let pythonContent =
|
|
111
|
+
"from dataclasses_json import DataClassJsonMixin\n" +
|
|
112
|
+
jsonTypes.join("\n");
|
|
113
|
+
pythonContent = pythonContent.replace(
|
|
114
|
+
/@dataclass\nclass (\w+)(?:\s*\([^)]*\))?\s*:/g,
|
|
115
|
+
"@dataclass\nclass $1(DataClassJsonMixin):"
|
|
116
|
+
);
|
|
117
|
+
const pythonFile = path.join(folder.path, filename + ".py");
|
|
118
|
+
const initFile = path.join(folder.path, "__init__.py");
|
|
119
|
+
fs.writeFileSync(pythonFile, pythonContent);
|
|
120
|
+
fs.writeFileSync(initFile, initContent);
|
|
90
121
|
} catch (error) {
|
|
91
122
|
console.error(
|
|
92
123
|
`Erreur lors de la lecture du fichier ${filePath}:`,
|
|
@@ -116,7 +147,11 @@ if (fs.existsSync(outputFile)) {
|
|
|
116
147
|
fs.unlinkSync(outputFile);
|
|
117
148
|
}
|
|
118
149
|
|
|
119
|
-
|
|
120
|
-
finalJson
|
|
150
|
+
async function main() {
|
|
151
|
+
const finalJson = {};
|
|
152
|
+
finalJson[projectName] = return_json_schema(directoryPath, "", projectName);
|
|
153
|
+
console.log("FINAL", outputFile, finalJson);
|
|
154
|
+
fs.writeFileSync(outputFile, JSON.stringify(finalJson, null, 2));
|
|
155
|
+
}
|
|
121
156
|
|
|
122
|
-
|
|
157
|
+
main();
|
package/package.json
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geode/opengeodeweb-microservice",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Shared utilities and schema generator for OpenGeodeWeb ecosystem",
|
|
5
5
|
"main": "generate_schemas.js",
|
|
6
|
-
"bin": {
|
|
7
|
-
"ogw-generate-schemas": "./generate_schemas.js"
|
|
8
|
-
},
|
|
9
6
|
"scripts": {
|
|
10
7
|
"test": "echo \"Error: no test specified\" && exit 0",
|
|
11
8
|
"build": "echo \"Error: no test specified\" && exit 0"
|
|
12
9
|
},
|
|
13
10
|
"dependencies": {
|
|
14
|
-
"glob": "^11.0.3"
|
|
11
|
+
"glob": "^11.0.3",
|
|
12
|
+
"quicktype-core": "^23.2.6"
|
|
15
13
|
},
|
|
16
14
|
"repository": {
|
|
17
15
|
"type": "git",
|
|
@@ -32,5 +30,6 @@
|
|
|
32
30
|
},
|
|
33
31
|
"files": [
|
|
34
32
|
"generate_schemas.js"
|
|
35
|
-
]
|
|
33
|
+
],
|
|
34
|
+
"type": "module"
|
|
36
35
|
}
|