@@ -3,29 +3,7 @@ import * as fs from 'fs';
3
3
import * as path from 'path' ;
4
4
import * as _ from 'lodash' ;
5
5
6
- export default function parseLockFile ( root , targetFilePath , lockFilePath , options ) {
7
- if ( ! root || ! lockFilePath || ! lockFilePath ) {
8
- throw new Error ( 'Missing required parameters for parseLockFile()' ) ;
9
- }
10
- // TODO: validate only valid options were passed in
11
-
12
- const targetFileFullPath = path . resolve ( root , targetFilePath ) ;
13
- const lockFileFullPath = path . resolve ( root , lockFilePath ) ;
14
-
15
- if ( ! fs . existsSync ( targetFilePath ) ) {
16
- throw new Error ( `Target file package.json not found at location: ${ targetFileFullPath } ` ) ;
17
- }
18
- if ( ! fs . existsSync ( lockFilePath ) ) {
19
- throw new Error ( `LockFile package-lock.json not found at location: ${ lockFileFullPath } ` ) ;
20
- }
21
-
22
- const targetFile = fs . readFileSync ( targetFileFullPath ) ;
23
- const lockFile = fs . readFileSync ( lockFileFullPath ) ;
24
-
25
- return buildDepTree ( targetFile , lockFile , options ) ;
26
- }
27
-
28
- async function buildDepTree ( targetFileRaw , lockFileRaw , options ) {
6
+ export async function buildDepTree ( targetFileRaw , lockFileRaw , options ) {
29
7
30
8
const lockFile = JSON . parse ( lockFileRaw ) ;
31
9
const targetFile = JSON . parse ( targetFileRaw ) ;
@@ -43,49 +21,48 @@ async function buildDepTree(targetFileRaw, lockFileRaw, options) {
43
21
version : targetFile . version || undefined ,
44
22
} ;
45
23
46
- const fullDepList = lockFile . dependencies ;
47
24
const topLevelDeps = Object . keys ( targetFile . dependencies ) ;
48
25
49
26
await Promise . all ( topLevelDeps . map ( async ( dep ) => {
50
- depTree . dependencies [ dep ] = await buildSubTreeRecursive ( dep , [ ] ) ;
27
+ depTree . dependencies [ dep ] = await buildSubTreeRecursive ( dep , [ ] , lockFile ) ;
51
28
} ) ) ;
52
29
53
30
return depTree ;
31
+ }
32
+
33
+ async function buildSubTreeRecursive ( dep : string , depKeys : string [ ] , lockFile : object ) {
54
34
55
- async function buildSubTreeRecursive ( dep : string , depKeys : string [ ] ) {
56
-
57
- const depSubTree = {
58
- dependencies : { } ,
59
- name : dep ,
60
- version : undefined ,
61
- } ;
62
-
63
- // Get path to the nested dependencies from list ['package1', 'package2']
64
- // to ['dependencies', 'package1', 'dependencies', 'package2', 'dependencies']
65
- const depPath = getDepPath ( depKeys ) ;
66
- // try to get list of deps on the path
67
- const deps = _ . get ( lockFile , depPath ) ;
68
-
69
- // If exists and looked-up dep is there
70
- if ( deps && deps [ dep ] ) {
71
- // update the tree
72
- depSubTree . version = deps [ dep ] . version ;
73
- // repeat the process for dependencies of looked-up dep
74
- const newDeps = deps [ dep ] . requires && Object . keys ( deps [ dep ] . requires ) || [ ] ;
75
- await Promise . all ( newDeps . map ( async ( subDep ) => {
76
- depSubTree . dependencies [ subDep ] = await buildSubTreeRecursive ( subDep , [ ...depKeys , dep ] ) ;
77
- } ) ) ;
78
- return depSubTree ;
79
- } else {
80
- // tree was walked to the root and dependency was not found
81
- if ( ! depKeys . length ) {
82
- throw new Error ( `Dependency ${ dep } was not found in package-lock.json.
83
- Your package.json and package-lock.json are probably out of sync.
84
- Please run npm install and try to parse the log again.` ) ;
85
- }
86
- // dependency was not found on a current path, remove last key (move closer to the root) and try again
87
- return buildSubTreeRecursive ( dep , depKeys . slice ( 0 , - 1 ) ) ;
35
+ const depSubTree = {
36
+ dependencies : { } ,
37
+ name : dep ,
38
+ version : undefined ,
39
+ } ;
40
+
41
+ // Get path to the nested dependencies from list ['package1', 'package2']
42
+ // to ['dependencies', 'package1', 'dependencies', 'package2', 'dependencies']
43
+ const depPath = getDepPath ( depKeys ) ;
44
+ // try to get list of deps on the path
45
+ const deps = _ . get ( lockFile , depPath ) ;
46
+
47
+ // If exists and looked-up dep is there
48
+ if ( deps && deps [ dep ] ) {
49
+ // update the tree
50
+ depSubTree . version = deps [ dep ] . version ;
51
+ // repeat the process for dependencies of looked-up dep
52
+ const newDeps = deps [ dep ] . requires ? Object . keys ( deps [ dep ] . requires ) : [ ] ;
53
+ await Promise . all ( newDeps . map ( async ( subDep ) => {
54
+ depSubTree . dependencies [ subDep ] = await buildSubTreeRecursive ( subDep , [ ...depKeys , dep ] , lockFile ) ;
55
+ } ) ) ;
56
+ return depSubTree ;
57
+ } else {
58
+ // tree was walked to the root and dependency was not found
59
+ if ( ! depKeys . length ) {
60
+ throw new Error ( `Dependency ${ dep } was not found in package-lock.json.
61
+ Your package.json and package-lock.json are probably out of sync.
62
+ Please run npm install and try to parse the log again.` ) ;
88
63
}
64
+ // dependency was not found on a current path, remove last key (move closer to the root) and try again
65
+ return buildSubTreeRecursive ( dep , depKeys . slice ( 0 , - 1 ) , lockFile ) ;
89
66
}
90
67
}
91
68
@@ -96,3 +73,25 @@ function getDepPath(depKeys: string[]) {
96
73
97
74
return depPath ;
98
75
}
76
+
77
+ export function buildDepTreeFromFiles ( root , targetFilePath , lockFilePath , options ) {
78
+ if ( ! root || ! lockFilePath || ! lockFilePath ) {
79
+ throw new Error ( 'Missing required parameters for parseLockFile()' ) ;
80
+ }
81
+ // TODO: validate only valid options were passed in
82
+
83
+ const targetFileFullPath = path . resolve ( root , targetFilePath ) ;
84
+ const lockFileFullPath = path . resolve ( root , lockFilePath ) ;
85
+
86
+ if ( ! fs . existsSync ( targetFilePath ) ) {
87
+ throw new Error ( `Target file package.json not found at location: ${ targetFileFullPath } ` ) ;
88
+ }
89
+ if ( ! fs . existsSync ( lockFilePath ) ) {
90
+ throw new Error ( `LockFile package-lock.json not found at location: ${ lockFileFullPath } ` ) ;
91
+ }
92
+
93
+ const targetFile = fs . readFileSync ( targetFileFullPath ) ;
94
+ const lockFile = fs . readFileSync ( lockFileFullPath ) ;
95
+
96
+ return buildDepTree ( targetFile , lockFile , options ) ;
97
+ }
0 commit comments