1
1
import { mkdirSync , writeFileSync } from 'fs' ;
2
2
import { globSync } from 'glob' ;
3
- import { dirname } from 'path' ;
4
- import { InputData , JSONSchemaInput , JSONSchemaSourceData , JSONSchemaStore , RendererOptions , SerializedRenderResult , quicktype } from 'quicktype-core' ;
3
+ import { basename , dirname , extname } from 'path' ;
4
+ import { InputData , JSONSchemaInput , JSONSchemaSourceData , JSONSchemaStore , MultiFileRenderResult , RendererOptions , SerializedRenderResult , quicktypeMultiFile } from 'quicktype-core' ;
5
5
import { schemaForTypeScriptSources } from 'quicktype-typescript-input' ;
6
6
7
7
interface Config {
@@ -12,19 +12,41 @@ interface Config {
12
12
13
13
async function load ( globPath : string , configs : Config [ ] ) {
14
14
const filePaths : string [ ] = globSync ( globPath ) ;
15
+ const filePathMap : any = createFilePathMap ( filePaths ) ;
16
+ const inputData : InputData = await createJSONSchema ( filePaths ) ;
15
17
console . log ( `📁 ${ filePaths } ` ) ;
18
+ console . log ( filePathMap ) ;
19
+ for ( let i = 0 ; i < configs . length ; i ++ ) {
20
+ const { lang, ext, rendererOptions } = configs [ i ] ;
21
+ const packagePath : string = typeof rendererOptions . package === 'string' ? rendererOptions . package . replace ( '.' , '/' ) + '/' : '' ;
22
+ const results : MultiFileRenderResult = await quicktypeMultiFile ( { inputData, rendererOptions, lang } ) ;
23
+ results . forEach ( ( result : SerializedRenderResult , name : string ) => {
24
+ const fileKey : string = name . replace ( extname ( name ) , '' ) ;
25
+ const filePath : string = filePathMap [ fileKey ] || fileKey ;
26
+ createFile ( `./dist/${ lang } /${ packagePath } ${ filePath } .${ ext } ` , result . lines . join ( '\n' ) ) ;
27
+ } ) ;
28
+ }
29
+ }
30
+
31
+ function createFilePathMap ( filePaths : string [ ] ) {
32
+ const filePathMap : any = { } ;
33
+ filePaths . forEach ( ( filePath ) => {
34
+ const fileExt : string = extname ( filePath ) ;
35
+ const fileKey : string = basename ( filePath , fileExt ) ;
36
+ filePathMap [ fileKey ] = filePath . replace ( 'src/' , '' ) . replace ( fileExt , '' ) ;
37
+ } ) ;
38
+ return filePathMap ;
39
+ }
40
+
41
+ async function createJSONSchema ( filePaths : string [ ] ) {
16
42
const jsonSchema : JSONSchemaSourceData = schemaForTypeScriptSources ( filePaths ) ;
17
43
// @ts -ignore
18
44
const jsonStore : JSONSchemaStore = new JSONSchemaStore ( ) ;
19
45
const jsonInput : JSONSchemaInput = new JSONSchemaInput ( jsonStore ) ;
20
46
await jsonInput . addSource ( { name : '#/definitions/' , schema : jsonSchema . schema } ) ;
21
47
const inputData : InputData = new InputData ( ) ;
22
48
inputData . addInput ( jsonInput ) ;
23
- for ( let i = 0 ; i < configs . length ; i ++ ) {
24
- const { lang, ext, rendererOptions } = configs [ i ] ;
25
- const result : SerializedRenderResult = await quicktype ( { inputData, rendererOptions, lang } ) ;
26
- createFile ( `./dist/${ lang } /types.${ ext } ` , result . lines . join ( '\n' ) ) ;
27
- }
49
+ return inputData ;
28
50
}
29
51
30
52
function createFile ( filePath : string , fileContents : string ) {
@@ -34,7 +56,7 @@ function createFile(filePath: string, fileContents: string) {
34
56
}
35
57
36
58
load ( './src/**/*.ts' , [
37
- { lang : 'java' , ext : 'java' , rendererOptions : { "just-types" : true } } ,
59
+ { lang : 'java' , ext : 'java' , rendererOptions : { "just-types" : true , package : 'com.example' } } ,
38
60
{ lang : 'schema' , ext : 'json' , rendererOptions : { "just-types" : true } } ,
39
61
{ lang : 'ts' , ext : 'ts' , rendererOptions : { "just-types" : true } }
40
62
] ) ;
0 commit comments