@@ -5,6 +5,7 @@ const Fs = require('fs');
55const Path = require ( 'path' ) ;
66const Tempy = require ( 'tempy' ) ;
77
8+ const Package = require ( './package' ) ;
89
910const internals = { } ;
1011
@@ -24,7 +25,10 @@ exports.resolve = async ({ packageJson, lockfile }) => {
2425
2526 const path = Tempy . directory ( ) ;
2627 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+ }
2832
2933 const arborist = new Arborist ( { path } ) ;
3034
@@ -50,3 +54,36 @@ exports.resolve = async ({ packageJson, lockfile }) => {
5054
5155 return result ;
5256} ;
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