1- const fs = require ( "fs" ) ;
2- const path = require ( "path" ) ;
3- const glob = require ( "glob" ) ;
4- const process = require ( "process" ) ;
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" ;
511
612console . log ( "process.argv" , process . argv ) ;
713
@@ -46,6 +52,18 @@ const directoryPath = findDirectoryPath(projectName, folderName);
4652
4753const outputFile = path . join ( process . cwd ( ) , `${ projectName } _schemas.json` ) ;
4854
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+
4967function return_json_schema ( directoryPath , folder_path , projectName ) {
5068 console . log ( "return_json_schema" , directoryPath , folder_path , projectName ) ;
5169
@@ -61,23 +79,20 @@ function return_json_schema(directoryPath, folder_path, projectName) {
6179 if ( folder . name == "schemas" ) {
6280 const jsonFiles = glob . sync ( path . join ( folder . path , "**/*.json" ) ) ;
6381 var schemas = { } ;
64- jsonFiles . forEach ( ( filePath ) => {
82+ let initContent = "" ;
83+ jsonFiles . forEach ( async ( filePath ) => {
6584 try {
6685 const fileContent = fs . readFileSync ( filePath , "utf8" ) ;
6786 var jsonData = JSON . parse ( fileContent ) ;
6887 var filename = filePath
6988 . replace ( / ^ .* [ \\ / ] / , "" )
7089 . replace ( / \. [ ^ / . ] + $ / , "" ) ;
7190 var route = jsonData [ key ] ;
72- console . log ( "FOLDER PATH" , projectName ) ;
7391 var values = [ projectName , folder_path , route ] ;
74- console . log ( "values" , values ) ;
7592 values = values . map ( function ( x ) {
76- console . log ( "x" , x ) ;
7793 return x . replace ( "/" , "" ) . replace ( "." , "" ) ;
7894 } ) ; // first replace first . / by empty string
7995 values = values . map ( function ( x ) {
80- console . log ( "x" , x ) ;
8196 return x . replaceAll ( "/" , separator ) . replaceAll ( "." , separator ) ;
8297 } ) ; // then replace all . / by separator
8398 console . log ( "values" , values ) ;
@@ -87,6 +102,22 @@ function return_json_schema(directoryPath, folder_path, projectName) {
87102 } )
88103 . join ( separator ) ;
89104 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+ / @ d a t a c l a s s \n c l a s s ( \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 ) ;
90121 } catch ( error ) {
91122 console . error (
92123 `Erreur lors de la lecture du fichier ${ filePath } :` ,
@@ -116,7 +147,11 @@ if (fs.existsSync(outputFile)) {
116147 fs . unlinkSync ( outputFile ) ;
117148}
118149
119- const finalJson = { } ;
120- finalJson [ projectName ] = return_json_schema ( directoryPath , "" , projectName ) ;
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+ }
121156
122- fs . writeFileSync ( outputFile , JSON . stringify ( finalJson , null , 2 ) ) ;
157+ main ( ) ;
0 commit comments