Skip to content

Commit

Permalink
fix: ignore invalid package.json during read-pkg-up (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Jul 9, 2016
1 parent 8d6ad6e commit e058c87
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions test/fixtures/broken-json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'foo': bar}
9 changes: 9 additions & 0 deletions test/yargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,15 @@ describe('yargs dsl tests', function () {
argv.foo.should.equal('a')
argv.dotNotation.should.equal(false)
})

// see https://github.com/yargs/yargs/issues/485
it('handles an invalid package.json', function () {
var argv = yargs('--foo a')
.pkgConf('yargs', './test/fixtures/broken-json')
.argv

argv.foo.should.equal('a')
})
})

describe('skipValidation', function () {
Expand Down
9 changes: 6 additions & 3 deletions yargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,12 @@ function Yargs (processArgs, cwd, parentRequire) {
if (pkg && !path) return pkg
const readPkgUp = require('read-pkg-up')

var obj = readPkgUp.sync({
cwd: path || requireMainFilename(parentRequire || require)
})
var obj = {}
try {
obj = readPkgUp.sync({
cwd: path || requireMainFilename(parentRequire || require)
})
} catch (noop) {}

if (obj.pkg) pkg = obj.pkg
return pkg || {}
Expand Down

0 comments on commit e058c87

Please sign in to comment.