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,46 @@ const getSchema = ({
35
35
return file ;
36
36
} ;
37
37
38
+ const getFactoryMethod = ( {
39
+ schema : { name, 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 += `import { ${ name } } from \'./${ camel ( name ) } \';` ;
72
+ file += '\n\n' ;
73
+
74
+ file += factoryMethod ;
75
+ return file ;
76
+ } ;
77
+
38
78
const getPath = ( path : string , name : string , fileExtension : string ) : string =>
39
79
upath . join ( path , `/${ name } ${ fileExtension } ` ) ;
40
80
@@ -53,6 +93,9 @@ export const writeSchema = async ({
53
93
isRootKey,
54
94
specsName,
55
95
header,
96
+ factoryMethodInclude,
97
+ factoryMethodOutput,
98
+ factoryMethodPrefix
56
99
} : {
57
100
path : string ;
58
101
schema : GeneratorSchema ;
@@ -62,6 +105,10 @@ export const writeSchema = async ({
62
105
isRootKey : boolean ;
63
106
specsName : Record < string , string > ;
64
107
header : string ;
108
+ factoryMethodInclude : boolean ;
109
+ factoryMethodOutput ?: ( typeof OutputModelFactoryMethodsMode ) [ keyof typeof OutputModelFactoryMethodsMode ] ;
110
+ factoryMethodPrefix ?: string ;
111
+
65
112
} ) => {
66
113
const name = camel ( schema . name ) ;
67
114
@@ -70,6 +117,12 @@ export const writeSchema = async ({
70
117
getPath ( path , name , fileExtension ) ,
71
118
getSchema ( { schema, target, isRootKey, specsName, header, specKey } ) ,
72
119
) ;
120
+ if ( factoryMethodInclude && schema . factoryMethod . length > 0 ) {
121
+ await fs . outputFile (
122
+ getPath ( path , name + '.factory' , fileExtension ) ,
123
+ getFactoryMethod ( { schema, target, isRootKey, specsName, header, specKey, factoryMethodOutput, factoryMethodPrefix } ) ,
124
+ ) ;
125
+ }
73
126
} catch ( e ) {
74
127
throw `Oups... 🍻. An Error occurred while writing schema ${ name } => ${ e } ` ;
75
128
}
@@ -85,6 +138,9 @@ export const writeSchemas = async ({
85
138
specsName,
86
139
header,
87
140
indexFiles,
141
+ factoryMethodInclude,
142
+ factoryMethodOutput,
143
+ factoryMethodPrefix
88
144
} : {
89
145
schemaPath : string ;
90
146
schemas : GeneratorSchema [ ] ;
@@ -95,6 +151,9 @@ export const writeSchemas = async ({
95
151
specsName : Record < string , string > ;
96
152
header : string ;
97
153
indexFiles : boolean ;
154
+ factoryMethodInclude : boolean ;
155
+ factoryMethodOutput ?: ( typeof OutputModelFactoryMethodsMode ) [ keyof typeof OutputModelFactoryMethodsMode ] ;
156
+ factoryMethodPrefix ?: string ;
98
157
} ) => {
99
158
await Promise . all (
100
159
schemas . map ( ( schema ) =>
@@ -107,6 +166,9 @@ export const writeSchemas = async ({
107
166
isRootKey,
108
167
specsName,
109
168
header,
169
+ factoryMethodInclude,
170
+ factoryMethodOutput,
171
+ factoryMethodPrefix
110
172
} ) ,
111
173
) ,
112
174
) ;
0 commit comments