1
1
import {
2
2
addProjectConfiguration ,
3
+ ensurePackage ,
4
+ GeneratorCallback ,
3
5
getWorkspaceLayout ,
4
6
joinPathFragments ,
5
7
ProjectConfiguration ,
6
8
readProjectConfiguration ,
9
+ readWorkspaceConfiguration ,
7
10
Tree ,
8
11
updateProjectConfiguration ,
12
+ updateWorkspaceConfiguration ,
9
13
} from '@nrwl/devkit' ;
10
14
import { libraryGenerator } from '@nrwl/js/src/generators/library/library' ;
11
15
@@ -16,6 +20,7 @@ export default async function generateSwaggerSetup(
16
20
host : Tree ,
17
21
options : AddSwaggerJsonExecutorSchema ,
18
22
) {
23
+ const tasks : GeneratorCallback [ ] = [ ] ;
19
24
const project = readProjectConfiguration ( host , options . project ) ;
20
25
project . targets ??= { } ;
21
26
if ( ! options . output ) {
@@ -25,6 +30,7 @@ export default async function generateSwaggerSetup(
25
30
'swagger.json' ,
26
31
) ;
27
32
generateShellProject ( host , {
33
+ ...options ,
28
34
swaggerProject : options . swaggerProject ,
29
35
project : options . project ,
30
36
codegenProject : options . codegenProject ,
@@ -33,19 +39,14 @@ export default async function generateSwaggerSetup(
33
39
throw new Error ( 'Either specify --output or --swagger-project' ) ;
34
40
}
35
41
} else {
36
- if ( options . codegenProject ) {
42
+ if ( options . codegenProject && ! options . useNxPluginOpenAPI ) {
37
43
project . targets . codegen = {
38
44
executor : '@nx-dotnet/core:openapi-codegen' ,
39
45
options : {
40
46
openapiJsonPath : options . output ,
41
47
outputProject : options . codegenProject ,
42
48
} ,
43
- dependsOn : [
44
- {
45
- target : options . target || 'swagger' ,
46
- projects : 'self' ,
47
- } ,
48
- ] ,
49
+ dependsOn : [ 'swagger' ] ,
49
50
} ;
50
51
}
51
52
}
@@ -54,22 +55,16 @@ export default async function generateSwaggerSetup(
54
55
} ;
55
56
56
57
if ( options . codegenProject ) {
57
- await libraryGenerator ( host , {
58
- name : options . codegenProject ,
59
- directory : 'generated' ,
60
- buildable : true ,
61
- } ) ;
62
- const codegenProjectConfiguration = readProjectConfiguration (
63
- host ,
64
- `generated-${ options . codegenProject } ` ,
65
- ) ;
66
- codegenProjectConfiguration . implicitDependencies ??= [ ] ;
67
- codegenProjectConfiguration . implicitDependencies . push (
68
- options . swaggerProject ? options . swaggerProject : options . project ,
69
- ) ;
58
+ tasks . push ( ...( await generateCodegenProject ( host , options ) ) ) ;
70
59
}
71
60
72
61
updateProjectConfiguration ( host , options . project , project ) ;
62
+
63
+ return async ( ) => {
64
+ for ( const task of tasks ) {
65
+ await task ( ) ;
66
+ }
67
+ } ;
73
68
}
74
69
75
70
function swaggerProjectRoot ( host : Tree , swaggerProject : string ) {
@@ -82,7 +77,7 @@ function swaggerProjectRoot(host: Tree, swaggerProject: string) {
82
77
83
78
function generateShellProject (
84
79
host : Tree ,
85
- options : { project : string ; swaggerProject : string ; codegenProject ? : string } ,
80
+ options : AddSwaggerJsonExecutorSchema & { swaggerProject : string } ,
86
81
) {
87
82
const root = swaggerProjectRoot ( host , options . swaggerProject ) ;
88
83
const targets : ProjectConfiguration [ 'targets' ] = { } ;
@@ -94,26 +89,108 @@ function generateShellProject(
94
89
executor : 'nx:noop' ,
95
90
outputs : [ root ] ,
96
91
} ;
97
- targets . codegen = {
98
- executor : '@nx-dotnet/core:openapi-codegen' ,
99
- options : {
100
- openapiJsonPath : `${ swaggerProjectRoot (
101
- host ,
102
- options . swaggerProject ,
103
- ) } /swagger.json`,
104
- outputProject : `generated-${ options . codegenProject } ` ,
105
- } ,
106
- dependsOn : [
107
- {
108
- projects : 'dependencies' ,
109
- target : 'swagger' ,
92
+ if ( ! options . useNxPluginOpenAPI ) {
93
+ targets . codegen = {
94
+ executor : '@nx-dotnet/core:openapi-codegen' ,
95
+ options : {
96
+ openapiJsonPath : `${ swaggerProjectRoot (
97
+ host ,
98
+ options . swaggerProject ,
99
+ ) } /swagger.json`,
100
+ outputProject : `generated-${ options . codegenProject } ` ,
110
101
} ,
111
- ] ,
112
- } ;
102
+ dependsOn : [ '^swagger' ] ,
103
+ } ;
104
+ }
113
105
}
114
106
addProjectConfiguration ( host , options . swaggerProject , {
115
107
root,
116
108
targets,
117
109
implicitDependencies : [ options . project ] ,
118
110
} ) ;
119
111
}
112
+
113
+ async function generateCodegenProject (
114
+ host : Tree ,
115
+ options : AddSwaggerJsonExecutorSchema ,
116
+ ) : Promise < GeneratorCallback [ ] > {
117
+ const tasks : GeneratorCallback [ ] = [ ] ;
118
+ const nameWithDirectory = `generated-${ options . codegenProject } ` ;
119
+ if ( options . useNxPluginOpenAPI ) {
120
+ ensurePackage ( host , '@trumbitta/nx-plugin-openapi' , '^1.12.1' ) ;
121
+ const {
122
+ default : nxPluginOpenAPIGenerator ,
123
+ } : // eslint-disable-next-line @typescript-eslint/no-var-requires
124
+ typeof import ( '@trumbitta/nx-plugin-openapi/src/generators/api-lib/generator' ) = require ( '@trumbitta/nx-plugin-openapi/src/generators/api-lib/generator' ) ;
125
+ const {
126
+ default : nxPluginOpenAPIInitGenerator ,
127
+ } : // eslint-disable-next-line @typescript-eslint/no-var-requires
128
+ typeof import ( '@trumbitta/nx-plugin-openapi/src/generators/init/generator' ) = require ( '@trumbitta/nx-plugin-openapi/src/generators/init/generator' ) ;
129
+
130
+ tasks . push ( await nxPluginOpenAPIInitGenerator ( host ) ) ;
131
+
132
+ tasks . push (
133
+ await nxPluginOpenAPIGenerator ( host , {
134
+ isRemoteSpec : false ,
135
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
136
+ name : options . codegenProject ! ,
137
+ directory : 'generated' ,
138
+ generator : 'typescript-fetch' ,
139
+ sourceSpecLib : options . swaggerProject ,
140
+ } ) ,
141
+ ) ;
142
+
143
+ const configuration = readProjectConfiguration ( host , nameWithDirectory ) ;
144
+ configuration . targets ??= { } ;
145
+ const targetConfiguration = configuration . targets ?. [ 'generate-sources' ] ;
146
+ targetConfiguration . options [ 'sourceSpecPathOrUrl' ] = joinPathFragments (
147
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
148
+ readProjectConfiguration ( host , options . swaggerProject ! ) . root ,
149
+ 'swagger.json' ,
150
+ ) ;
151
+ targetConfiguration . dependsOn = [ '^swagger' ] ;
152
+ configuration . targets [ 'codegen' ] = targetConfiguration ;
153
+ delete configuration . targets [ 'generate-sources' ] ;
154
+ updateProjectConfiguration ( host , nameWithDirectory , configuration ) ;
155
+ } else {
156
+ tasks . push (
157
+ await libraryGenerator ( host , {
158
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
159
+ name : options . codegenProject ! ,
160
+ directory : 'generated' ,
161
+ buildable : true ,
162
+ } ) ,
163
+ ) ;
164
+ const codegenProjectConfiguration = readProjectConfiguration (
165
+ host ,
166
+ nameWithDirectory ,
167
+ ) ;
168
+ codegenProjectConfiguration . implicitDependencies ??= [ ] ;
169
+ codegenProjectConfiguration . implicitDependencies . push (
170
+ options . swaggerProject ? options . swaggerProject : options . project ,
171
+ ) ;
172
+ updateProjectConfiguration (
173
+ host ,
174
+ nameWithDirectory ,
175
+ codegenProjectConfiguration ,
176
+ ) ;
177
+ }
178
+
179
+ const wc = readWorkspaceConfiguration ( host ) ;
180
+
181
+ const cacheableOperations : string [ ] | null =
182
+ wc . tasksRunnerOptions ?. default ?. options ?. cacheableOperations ;
183
+ if ( cacheableOperations ) {
184
+ cacheableOperations . push ( 'codegen' , options . target ?? 'swagger' ) ;
185
+ }
186
+
187
+ const newBuildDeps = [ 'codegen' , '^codegen' ] ;
188
+ wc . targetDefaults ??= { } ;
189
+ wc . targetDefaults [ 'build' ] ??= { } ;
190
+ wc . targetDefaults [ 'build' ] . dependsOn ??= [ ] ;
191
+ wc . targetDefaults [ 'build' ] . dependsOn . push ( ...newBuildDeps ) ;
192
+
193
+ updateWorkspaceConfiguration ( host , wc ) ;
194
+
195
+ return tasks ;
196
+ }
0 commit comments