1
1
import fs from 'fs-extra' ;
2
2
import { generateImports } from '../generators' ;
3
- import { GeneratorSchema } from '../types' ;
3
+ import { GeneratorSchema , OutputModelFactoryMethodsMode } from '../types' ;
4
4
import { camel , upath } from '../utils' ;
5
5
6
6
const getSchema = ( {
@@ -35,6 +35,44 @@ const getSchema = ({
35
35
return file ;
36
36
} ;
37
37
38
+ const getFactoryMethod = ( {
39
+ schema : { imports, model, factoryMethod } ,
40
+ target,
41
+ isRootKey,
42
+ specsName,
43
+ header,
44
+ specKey,
45
+ factoryMethodOutput,
46
+ factoryMethodPrefix
47
+ } : {
48
+ schema : GeneratorSchema ;
49
+ target : string ;
50
+ isRootKey : boolean ;
51
+ specsName : Record < string , string > ;
52
+ header : string ;
53
+ specKey : string ;
54
+ factoryMethodOutput ?: ( typeof OutputModelFactoryMethodsMode ) [ keyof typeof OutputModelFactoryMethodsMode ] ;
55
+ factoryMethodPrefix ?: string ;
56
+ } ) : string => {
57
+ let file = header ;
58
+ file += generateImports ( {
59
+ imports : imports . filter (
60
+ ( imp ) =>
61
+ ! model . includes ( `type ${ imp . alias || imp . name } =` ) &&
62
+ ! model . includes ( `interface ${ imp . alias || imp . name } {` ) ,
63
+ ) ,
64
+ target,
65
+ isRootKey,
66
+ specsName,
67
+ specKey,
68
+ factoryMethodOutput,
69
+ factoryMethodPrefix
70
+ } ) ;
71
+ file += imports . length ? '\n\n' : '\n' ;
72
+ file += factoryMethod ;
73
+ return file ;
74
+ } ;
75
+
38
76
const getPath = ( path : string , name : string , fileExtension : string ) : string =>
39
77
upath . join ( path , `/${ name } ${ fileExtension } ` ) ;
40
78
@@ -53,6 +91,9 @@ export const writeSchema = async ({
53
91
isRootKey,
54
92
specsName,
55
93
header,
94
+ factoryMethodInclude,
95
+ factoryMethodOutput,
96
+ factoryMethodPrefix
56
97
} : {
57
98
path : string ;
58
99
schema : GeneratorSchema ;
@@ -62,6 +103,10 @@ export const writeSchema = async ({
62
103
isRootKey : boolean ;
63
104
specsName : Record < string , string > ;
64
105
header : string ;
106
+ factoryMethodInclude : boolean ;
107
+ factoryMethodOutput ?: ( typeof OutputModelFactoryMethodsMode ) [ keyof typeof OutputModelFactoryMethodsMode ] ;
108
+ factoryMethodPrefix ?: string ;
109
+
65
110
} ) => {
66
111
const name = camel ( schema . name ) ;
67
112
@@ -70,6 +115,12 @@ export const writeSchema = async ({
70
115
getPath ( path , name , fileExtension ) ,
71
116
getSchema ( { schema, target, isRootKey, specsName, header, specKey } ) ,
72
117
) ;
118
+ if ( factoryMethodInclude && schema . factoryMethod . length > 0 ) {
119
+ await fs . outputFile (
120
+ getPath ( path , name + '.factory' , fileExtension ) ,
121
+ getFactoryMethod ( { schema, target, isRootKey, specsName, header, specKey, factoryMethodOutput, factoryMethodPrefix } ) ,
122
+ ) ;
123
+ }
73
124
} catch ( e ) {
74
125
throw `Oups... 🍻. An Error occurred while writing schema ${ name } => ${ e } ` ;
75
126
}
@@ -85,6 +136,9 @@ export const writeSchemas = async ({
85
136
specsName,
86
137
header,
87
138
indexFiles,
139
+ factoryMethodInclude,
140
+ factoryMethodOutput,
141
+ factoryMethodPrefix
88
142
} : {
89
143
schemaPath : string ;
90
144
schemas : GeneratorSchema [ ] ;
@@ -95,6 +149,9 @@ export const writeSchemas = async ({
95
149
specsName : Record < string , string > ;
96
150
header : string ;
97
151
indexFiles : boolean ;
152
+ factoryMethodInclude : boolean ;
153
+ factoryMethodOutput ?: ( typeof OutputModelFactoryMethodsMode ) [ keyof typeof OutputModelFactoryMethodsMode ] ;
154
+ factoryMethodPrefix ?: string ;
98
155
} ) => {
99
156
await Promise . all (
100
157
schemas . map ( ( schema ) =>
@@ -107,6 +164,9 @@ export const writeSchemas = async ({
107
164
isRootKey,
108
165
specsName,
109
166
header,
167
+ factoryMethodInclude,
168
+ factoryMethodOutput,
169
+ factoryMethodPrefix
110
170
} ) ,
111
171
) ,
112
172
) ;
0 commit comments