File tree Expand file tree Collapse file tree 2 files changed +45
-5
lines changed Expand file tree Collapse file tree 2 files changed +45
-5
lines changed Original file line number Diff line number Diff line change @@ -9,12 +9,16 @@ const npm = require('./npm.js')
9
9
const { packument } = require ( 'pacote' )
10
10
const path = require ( 'path' )
11
11
const { inspect, promisify } = require ( 'util' )
12
- const readJson = promisify ( require ( 'read-package-json' ) )
13
12
const relativeDate = require ( 'tiny-relative-date' )
14
13
const semver = require ( 'semver' )
15
14
const style = require ( 'ansistyles' )
16
15
const usageUtil = require ( './utils/usage' )
17
16
17
+ const fs = require ( 'fs' )
18
+ const readFile = promisify ( fs . readFile )
19
+ const jsonParse = require ( 'json-parse-even-better-errors' )
20
+ const readJson = async file => jsonParse ( await readFile ( file , 'utf8' ) )
21
+
18
22
const usage = usageUtil (
19
23
'view' ,
20
24
'npm view [<@scope>/]<pkg>[@<version>] [<field>[.subfield]...]'
@@ -86,8 +90,8 @@ const view = async args => {
86
90
if ( local ) {
87
91
const dir = npm . prefix
88
92
const manifest = await readJson ( path . resolve ( dir , 'package.json' ) )
89
- if ( ! manifest || ! manifest . name )
90
- throw new Error ( 'Invalid package.json' )
93
+ if ( ! manifest . name )
94
+ throw new Error ( 'Invalid package.json, no "name" field ' )
91
95
const p = manifest . name
92
96
nv = npa ( p )
93
97
if ( pkg && ~ pkg . indexOf ( '@' ) )
Original file line number Diff line number Diff line change @@ -482,7 +482,43 @@ t.test('throw error if global mode', (t) => {
482
482
} )
483
483
} )
484
484
485
- t . test ( 'throw error if invalid package.json' , ( t ) => {
485
+ t . test ( 'throw ENOENT error if package.json misisng' , ( t ) => {
486
+ const testDir = t . testdir ( { } )
487
+
488
+ const view = requireInject ( '../../lib/view.js' , {
489
+ '../../lib/npm.js' : {
490
+ prefix : testDir ,
491
+ flatOptions : {
492
+ global : false
493
+ }
494
+ }
495
+ } )
496
+ view ( [ ] , ( err ) => {
497
+ t . match ( err , { code : 'ENOENT' } )
498
+ t . end ( )
499
+ } )
500
+ } )
501
+
502
+ t . test ( 'throw EJSONPARSE error if package.json not json' , ( t ) => {
503
+ const testDir = t . testdir ( {
504
+ 'package.json' : 'not json, nope, not even a little bit!'
505
+ } )
506
+
507
+ const view = requireInject ( '../../lib/view.js' , {
508
+ '../../lib/npm.js' : {
509
+ prefix : testDir ,
510
+ flatOptions : {
511
+ global : false
512
+ }
513
+ }
514
+ } )
515
+ view ( [ ] , ( err ) => {
516
+ t . match ( err , { code : 'EJSONPARSE' } )
517
+ t . end ( )
518
+ } )
519
+ } )
520
+
521
+ t . test ( 'throw error if package.json has no name' , ( t ) => {
486
522
const testDir = t . testdir ( {
487
523
'package.json' : '{}'
488
524
} )
@@ -496,7 +532,7 @@ t.test('throw error if invalid package.json', (t) => {
496
532
}
497
533
} )
498
534
view ( [ ] , ( err ) => {
499
- t . equals ( err . message , 'Invalid package.json' )
535
+ t . equals ( err . message , 'Invalid package.json, no "name" field ' )
500
536
t . end ( )
501
537
} )
502
538
} )
You can’t perform that action at this time.
0 commit comments