Skip to content

Commit

Permalink
fix(unpublish): bubble up all errors parsing local package.json
Browse files Browse the repository at this point in the history
If the file is there and there is any error that's always a show stopper
  • Loading branch information
wraithgar committed Dec 1, 2023
1 parent be4741f commit 82809a9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/commands/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ class Unpublish extends BaseCommand {
const { content } = await pkgJson.prepare(localPrefix)
manifest = content
} catch (err) {
// we needed the manifest to figure out the package to unpublish
if (!spec) {
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
if (!spec) {
// We needed a local package.json to figure out what package to
// unpublish
throw this.usageError()
} else {
throw err
}
} else {
// folks should know if ANY local package.json had a parsing error.
// They may be relying on `publishConfig` to be loading and we don't
// want to ignore errors in that case.
throw err
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/lib/commands/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ t.test('no args --force error reading package.json', async t => {
)
})

t.test('with args --force error reading package.json', async t => {
const { npm } = await loadMockNpm(t, {
config: {
force: true,
},
prefixDir: {
'package.json': '{ not valid json ]',
},
})

await t.rejects(
npm.exec('unpublish', [pkg]),
/Invalid package.json/,
'should throw error from reading package.json'
)
})

t.test('no force entire project', async t => {
const { npm } = await loadMockNpm(t)

Expand Down

0 comments on commit 82809a9

Please sign in to comment.