@@ -3,25 +3,26 @@ const readJson = require('read-package-json-fast')
3
3
const { cmdList } = require ( './cmd-list.js' )
4
4
5
5
const didYouMean = async ( npm , path , scmd ) => {
6
- const bestCmd = cmdList
6
+ let best = cmdList
7
7
. filter ( cmd => distance ( scmd , cmd ) < scmd . length * 0.4 && scmd !== cmd )
8
8
. map ( str => ` npm ${ str } # ${ npm . commands [ str ] . description } ` )
9
9
10
- const pkg = await readJson ( `${ path } /package.json` )
11
- const { scripts } = pkg
12
10
// We would already be suggesting this in `npm x` so omit them here
13
11
const runScripts = [ 'stop' , 'start' , 'test' , 'restart' ]
14
- const bestRun = Object . keys ( scripts || { } )
15
- . filter ( cmd => distance ( scmd , cmd ) < scmd . length * 0.4 &&
16
- ! runScripts . includes ( cmd ) )
17
- . map ( str => ` npm run ${ str } # run the "${ str } " package script` )
18
-
19
- const { bin } = pkg
20
- const bestBin = Object . keys ( bin || { } )
21
- . filter ( cmd => distance ( scmd , cmd ) < scmd . length * 0.4 )
22
- . map ( str => ` npm exec ${ str } # run the "${ str } " command from either this or a remote npm package` )
23
-
24
- const best = [ ...bestCmd , ...bestRun , ...bestBin ]
12
+ try {
13
+ const { bin, scripts } = await readJson ( `${ path } /package.json` )
14
+ best = best . concat (
15
+ Object . keys ( scripts || { } )
16
+ . filter ( cmd => distance ( scmd , cmd ) < scmd . length * 0.4 &&
17
+ ! runScripts . includes ( cmd ) )
18
+ . map ( str => ` npm run ${ str } # run the "${ str } " package script` ) ,
19
+ Object . keys ( bin || { } )
20
+ . filter ( cmd => distance ( scmd , cmd ) < scmd . length * 0.4 )
21
+ . map ( str => ` npm exec ${ str } # run the "${ str } " command from either this or a remote npm package` )
22
+ )
23
+ } catch ( _ ) {
24
+ // gracefully ignore not being in a folder w/ a package.json
25
+ }
25
26
26
27
if ( best . length === 0 )
27
28
return ''
0 commit comments