@@ -18,7 +18,8 @@ import {
18
18
import { LogicalRoot } from "snyk-resolve-deps/dist/types" ;
19
19
import {
20
20
cleanupAppNodeModules ,
21
- groupFilesByDirectory ,
21
+ groupNodeAppFilesByDirectory ,
22
+ groupNodeModulesFilesByDirectory ,
22
23
persistNodeModules ,
23
24
} from "./node-modules-utils" ;
24
25
import {
@@ -35,6 +36,7 @@ interface ManifestLockPathPair {
35
36
36
37
export async function nodeFilesToScannedProjects (
37
38
filePathToContent : FilePathToContent ,
39
+ shouldIncludeNodeModules : boolean ,
38
40
) : Promise < AppDepsScanResultWithoutTarget [ ] > {
39
41
const scanResults : AppDepsScanResultWithoutTarget [ ] = [ ] ;
40
42
/**
@@ -51,8 +53,9 @@ export async function nodeFilesToScannedProjects(
51
53
return [ ] ;
52
54
}
53
55
54
- const fileNamesGroupedByDirectory = groupFilesByDirectory ( filePathToContent ) ;
55
- const [ manifestFilePairs , nodeProjects ] = findProjectsAndManifests (
56
+ const fileNamesGroupedByDirectory =
57
+ groupNodeAppFilesByDirectory ( filePathToContent ) ;
58
+ const manifestFilePairs = findManifestLockPairsInSameDirectory (
56
59
fileNamesGroupedByDirectory ,
57
60
) ;
58
61
@@ -64,14 +67,22 @@ export async function nodeFilesToScannedProjects(
64
67
) ) ,
65
68
) ;
66
69
}
67
- if ( nodeProjects . length !== 0 ) {
68
- scanResults . push (
69
- ...( await depGraphFromNodeModules (
70
- filePathToContent ,
71
- nodeProjects ,
72
- fileNamesGroupedByDirectory ,
73
- ) ) ,
70
+
71
+ if ( shouldIncludeNodeModules ) {
72
+ const appNodeModulesGroupedByDirectory =
73
+ groupNodeModulesFilesByDirectory ( filePathToContent ) ;
74
+ const nodeProjects = findManifestNodeModulesFilesInSameDirectory (
75
+ appNodeModulesGroupedByDirectory ,
74
76
) ;
77
+ if ( nodeProjects . length !== 0 ) {
78
+ scanResults . push (
79
+ ...( await depGraphFromNodeModules (
80
+ filePathToContent ,
81
+ nodeProjects ,
82
+ appNodeModulesGroupedByDirectory ,
83
+ ) ) ,
84
+ ) ;
85
+ }
75
86
}
76
87
77
88
return scanResults ;
@@ -109,7 +120,6 @@ async function depGraphFromNodeModules(
109
120
) ;
110
121
111
122
if ( ( pkgTree as LogicalRoot ) . numDependencies === 0 ) {
112
- await cleanupAppNodeModules ( tempDir ) ;
113
123
continue ;
114
124
}
115
125
@@ -197,13 +207,15 @@ async function depGraphFromManifestFiles(
197
207
return scanResults ;
198
208
}
199
209
200
- function findProjectsAndManifests (
210
+ function findManifestLockPairsInSameDirectory (
201
211
fileNamesGroupedByDirectory : FilesByDirMap ,
202
- ) : [ ManifestLockPathPair [ ] , string [ ] ] {
212
+ ) : ManifestLockPathPair [ ] {
203
213
const manifestLockPathPairs : ManifestLockPathPair [ ] = [ ] ;
204
- const nodeProjects : string [ ] = [ ] ;
205
214
206
215
for ( const directoryPath of fileNamesGroupedByDirectory . keys ( ) ) {
216
+ if ( directoryPath . includes ( "node_modules" ) ) {
217
+ continue ;
218
+ }
207
219
const filesInDirectory = fileNamesGroupedByDirectory . get ( directoryPath ) ;
208
220
if ( ! filesInDirectory || filesInDirectory . size < 1 ) {
209
221
// missing manifest files
@@ -230,12 +242,40 @@ function findProjectsAndManifests(
230
242
? lockFileParser . LockfileType . npm
231
243
: lockFileParser . LockfileType . yarn ,
232
244
} ) ;
245
+ }
246
+ }
247
+
248
+ return manifestLockPathPairs ;
249
+ }
250
+
251
+ function findManifestNodeModulesFilesInSameDirectory (
252
+ fileNamesGroupedByDirectory : FilesByDirMap ,
253
+ ) : string [ ] {
254
+ const nodeProjects : string [ ] = [ ] ;
255
+
256
+ for ( const directoryPath of fileNamesGroupedByDirectory . keys ( ) ) {
257
+ const filesInDirectory = fileNamesGroupedByDirectory . get ( directoryPath ) ;
258
+ if ( ! filesInDirectory || filesInDirectory . size < 1 ) {
259
+ // missing manifest files
260
+ continue ;
261
+ }
262
+
263
+ const expectedManifest = path . join ( directoryPath , "package.json" ) ;
264
+ const expectedNpmLockFile = path . join ( directoryPath , "package-lock.json" ) ;
265
+ const expectedYarnLockFile = path . join ( directoryPath , "yarn.lock" ) ;
266
+
267
+ const hasManifestFile = filesInDirectory . has ( expectedManifest ) ;
268
+ const hasLockFile =
269
+ filesInDirectory . has ( expectedNpmLockFile ) ||
270
+ filesInDirectory . has ( expectedYarnLockFile ) ;
271
+
272
+ if ( hasManifestFile && hasLockFile ) {
233
273
continue ;
234
274
}
235
275
nodeProjects . push ( directoryPath ) ;
236
276
}
237
277
238
- return [ manifestLockPathPairs , nodeProjects ] ;
278
+ return nodeProjects ;
239
279
}
240
280
241
281
function stripUndefinedLabels (
0 commit comments