Skip to content

Commit 7b952f6

Browse files
authoredDec 1, 2023
fix(unpublish): bubble up all errors parsing local package.json (#7049)
If the file is there and there is any error that's always a show stopper
1 parent be4741f commit 7b952f6

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed
 

‎lib/commands/unpublish.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,17 @@ class Unpublish extends BaseCommand {
109109
const { content } = await pkgJson.prepare(localPrefix)
110110
manifest = content
111111
} catch (err) {
112-
// we needed the manifest to figure out the package to unpublish
113-
if (!spec) {
114-
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
112+
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
113+
if (!spec) {
114+
// We needed a local package.json to figure out what package to
115+
// unpublish
115116
throw this.usageError()
116-
} else {
117-
throw err
118117
}
118+
} else {
119+
// folks should know if ANY local package.json had a parsing error.
120+
// They may be relying on `publishConfig` to be loading and we don't
121+
// want to ignore errors in that case.
122+
throw err
119123
}
120124
}
121125

‎test/lib/commands/unpublish.js

+17
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ t.test('no args --force error reading package.json', async t => {
6363
)
6464
})
6565

66+
t.test('with args --force error reading package.json', async t => {
67+
const { npm } = await loadMockNpm(t, {
68+
config: {
69+
force: true,
70+
},
71+
prefixDir: {
72+
'package.json': '{ not valid json ]',
73+
},
74+
})
75+
76+
await t.rejects(
77+
npm.exec('unpublish', [pkg]),
78+
/Invalid package.json/,
79+
'should throw error from reading package.json'
80+
)
81+
})
82+
6683
t.test('no force entire project', async t => {
6784
const { npm } = await loadMockNpm(t)
6885

0 commit comments

Comments
 (0)
Please sign in to comment.