@@ -5,6 +5,7 @@ const Fs = require('fs');
5
5
const Path = require ( 'path' ) ;
6
6
const Tempy = require ( 'tempy' ) ;
7
7
8
+ const Package = require ( './package' ) ;
8
9
9
10
const internals = { } ;
10
11
@@ -24,7 +25,10 @@ exports.resolve = async ({ packageJson, lockfile }) => {
24
25
25
26
const path = Tempy . directory ( ) ;
26
27
Fs . writeFileSync ( Path . join ( path , 'package.json' ) , JSON . stringify ( packageJson , null , ' ' ) ) ;
27
- Fs . writeFileSync ( Path . join ( path , 'package-lock.json' ) , JSON . stringify ( lockfile , null , ' ' ) ) ;
28
+
29
+ if ( lockfile ) {
30
+ Fs . writeFileSync ( Path . join ( path , 'package-lock.json' ) , JSON . stringify ( lockfile , null , ' ' ) ) ;
31
+ }
28
32
29
33
const arborist = new Arborist ( { path } ) ;
30
34
@@ -50,3 +54,36 @@ exports.resolve = async ({ packageJson, lockfile }) => {
50
54
51
55
return result ;
52
56
} ;
57
+
58
+ internals . tryLoad = ( loadFile , filename ) => {
59
+
60
+ try {
61
+ return loadFile ( filename , { json : 'force' } ) ;
62
+ }
63
+ catch ( err ) {
64
+ if ( err . code !== 'ENOENT' ) {
65
+ throw err ;
66
+ }
67
+ }
68
+ } ;
69
+
70
+ exports . detect = async ( { packageJson, loadFile } ) => {
71
+
72
+ const lockfile = ( await internals . tryLoad ( loadFile , 'package-lock.json' ) ) || ( await internals . tryLoad ( loadFile , 'npm-shrinkwrap.json' ) ) ;
73
+
74
+ const versions = await exports . resolve ( { packageJson, lockfile } ) ;
75
+
76
+ const support = [ ] ;
77
+
78
+ for ( const packageName of Object . keys ( versions ) ) {
79
+ try {
80
+ const { result } = await Package . detect ( { packageName } ) ;
81
+ support . push ( result ) ;
82
+ }
83
+ catch ( err ) {
84
+ console . warn ( `Failed to detect support for ${ packageName } : ${ err && err . message } ` ) ;
85
+ }
86
+ }
87
+
88
+ return { support, versions } ;
89
+ } ;
0 commit comments