1
1
import {
2
2
CreateNodes ,
3
3
CreateNodesContext ,
4
+ CreateNodesContextV2 ,
4
5
createNodesFromFiles ,
5
6
CreateNodesV2 ,
6
7
getPackageManagerCommand ,
@@ -13,7 +14,10 @@ import {
13
14
TargetConfiguration ,
14
15
writeJsonFile ,
15
16
} from '@nx/devkit' ;
16
- import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes' ;
17
+ import {
18
+ calculateHashesForCreateNodes ,
19
+ calculateHashForCreateNodes ,
20
+ } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes' ;
17
21
import {
18
22
clearRequireCache ,
19
23
loadConfigFile ,
@@ -72,17 +76,70 @@ export const createNodesV2: CreateNodesV2<JestPluginOptions> = [
72
76
// Cache jest preset(s) to avoid penalties of module load times. Most of jest configs will use the same preset.
73
77
const presetCache : Record < string , unknown > = { } ;
74
78
79
+ const packageManagerWorkspacesGlob = combineGlobPatterns (
80
+ getGlobPatternsFromPackageManagerWorkspaces ( context . workspaceRoot )
81
+ ) ;
82
+ options = normalizeOptions ( options ) ;
83
+
84
+ const { roots : projectRoots , configFiles : validConfigFiles } =
85
+ configFiles . reduce (
86
+ ( acc , configFile ) => {
87
+ const potentialRoot = dirname ( configFile ) ;
88
+ if (
89
+ checkIfConfigFileShouldBeProject (
90
+ configFile ,
91
+ potentialRoot ,
92
+ packageManagerWorkspacesGlob ,
93
+ context
94
+ )
95
+ ) {
96
+ acc . roots . push ( potentialRoot ) ;
97
+ acc . configFiles . push ( configFile ) ;
98
+ }
99
+ return acc ;
100
+ } ,
101
+ {
102
+ roots : [ ] ,
103
+ configFiles : [ ] ,
104
+ } as {
105
+ roots : string [ ] ;
106
+ configFiles : string [ ] ;
107
+ }
108
+ ) ;
109
+
110
+ const hashes = await calculateHashesForCreateNodes (
111
+ projectRoots ,
112
+ options ,
113
+ context
114
+ ) ;
115
+
75
116
try {
76
117
return await createNodesFromFiles (
77
- ( configFile , options , context ) =>
78
- createNodesInternal (
79
- configFile ,
118
+ async ( configFilePath , options , context , idx ) => {
119
+ const projectRoot = projectRoots [ idx ] ;
120
+ const hash = hashes [ idx ] ;
121
+
122
+ targetsCache [ hash ] ??= await buildJestTargets (
123
+ configFilePath ,
124
+ projectRoot ,
80
125
options ,
81
126
context ,
82
- targetsCache ,
83
127
presetCache
84
- ) ,
85
- configFiles ,
128
+ ) ;
129
+
130
+ const { targets, metadata } = targetsCache [ hash ] ;
131
+
132
+ return {
133
+ projects : {
134
+ [ projectRoot ] : {
135
+ root : projectRoot ,
136
+ targets,
137
+ metadata,
138
+ } ,
139
+ } ,
140
+ } ;
141
+ } ,
142
+ validConfigFiles ,
86
143
options ,
87
144
context
88
145
) ;
@@ -98,35 +155,63 @@ export const createNodesV2: CreateNodesV2<JestPluginOptions> = [
98
155
*/
99
156
export const createNodes : CreateNodes < JestPluginOptions > = [
100
157
jestConfigGlob ,
101
- ( ... args ) => {
158
+ async ( configFilePath , options , context ) => {
102
159
logger . warn (
103
160
'`createNodes` is deprecated. Update your plugin to utilize createNodesV2 instead. In Nx 20, this will change to the createNodesV2 API.'
104
161
) ;
105
162
106
- return createNodesInternal ( ...args , { } , { } ) ;
163
+ const projectRoot = dirname ( configFilePath ) ;
164
+
165
+ const packageManagerWorkspacesGlob = combineGlobPatterns (
166
+ getGlobPatternsFromPackageManagerWorkspaces ( context . workspaceRoot )
167
+ ) ;
168
+
169
+ if (
170
+ ! checkIfConfigFileShouldBeProject (
171
+ configFilePath ,
172
+ projectRoot ,
173
+ packageManagerWorkspacesGlob ,
174
+ context
175
+ )
176
+ ) {
177
+ return { } ;
178
+ }
179
+
180
+ options = normalizeOptions ( options ) ;
181
+
182
+ const { targets, metadata } = await buildJestTargets (
183
+ configFilePath ,
184
+ projectRoot ,
185
+ options ,
186
+ context ,
187
+ { }
188
+ ) ;
189
+
190
+ return {
191
+ projects : {
192
+ [ projectRoot ] : {
193
+ root : projectRoot ,
194
+ targets,
195
+ metadata,
196
+ } ,
197
+ } ,
198
+ } ;
107
199
} ,
108
200
] ;
109
201
110
- async function createNodesInternal (
202
+ function checkIfConfigFileShouldBeProject (
111
203
configFilePath : string ,
112
- options : JestPluginOptions ,
113
- context : CreateNodesContext ,
114
- targetsCache : Record < string , JestTargets > ,
115
- presetCache : Record < string , unknown >
116
- ) {
117
- const projectRoot = dirname ( configFilePath ) ;
118
-
119
- const packageManagerWorkspacesGlob = combineGlobPatterns (
120
- getGlobPatternsFromPackageManagerWorkspaces ( context . workspaceRoot )
121
- ) ;
122
-
204
+ projectRoot : string ,
205
+ packageManagerWorkspacesGlob : string ,
206
+ context : CreateNodesContext | CreateNodesContextV2
207
+ ) : boolean {
123
208
// Do not create a project if package.json and project.json isn't there.
124
209
const siblingFiles = readdirSync ( join ( context . workspaceRoot , projectRoot ) ) ;
125
210
if (
126
211
! siblingFiles . includes ( 'package.json' ) &&
127
212
! siblingFiles . includes ( 'project.json' )
128
213
) {
129
- return { } ;
214
+ return false ;
130
215
} else if (
131
216
! siblingFiles . includes ( 'project.json' ) &&
132
217
siblingFiles . includes ( 'package.json' )
@@ -136,7 +221,7 @@ async function createNodesInternal(
136
221
const isPackageJsonProject = minimatch ( path , packageManagerWorkspacesGlob ) ;
137
222
138
223
if ( ! isPackageJsonProject ) {
139
- return { } ;
224
+ return false ;
140
225
}
141
226
}
142
227
@@ -148,31 +233,9 @@ async function createNodesInternal(
148
233
// The `getJestProjectsAsync` function uses the project graph, which leads to a
149
234
// circular dependency. We can skip this since it's no intended to be used for
150
235
// an Nx project.
151
- return { } ;
236
+ return false ;
152
237
}
153
-
154
- options = normalizeOptions ( options ) ;
155
-
156
- const hash = await calculateHashForCreateNodes ( projectRoot , options , context ) ;
157
- targetsCache [ hash ] ??= await buildJestTargets (
158
- configFilePath ,
159
- projectRoot ,
160
- options ,
161
- context ,
162
- presetCache
163
- ) ;
164
-
165
- const { targets, metadata } = targetsCache [ hash ] ;
166
-
167
- return {
168
- projects : {
169
- [ projectRoot ] : {
170
- root : projectRoot ,
171
- targets,
172
- metadata,
173
- } ,
174
- } ,
175
- } ;
238
+ return true ;
176
239
}
177
240
178
241
async function buildJestTargets (
0 commit comments