Skip to content

Commit 8be672b

Browse files
authoredJan 13, 2023
fix: don't try to deprecate nonexistant versions (#6050)
If you pass npm a version that doesn't exist, it still tries to PUT the packument but with no changes. This is unneccessary and currently results in a 422 error from the npm registry
1 parent 1c3612c commit 8be672b

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed
 

‎lib/commands/deprecate.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,21 @@ class Deprecate extends BaseCommand {
5252
query: { write: true },
5353
})
5454

55-
Object.keys(packument.versions)
55+
const versions = Object.keys(packument.versions)
5656
.filter(v => semver.satisfies(v, spec, { includePrerelease: true }))
57-
.forEach(v => {
58-
packument.versions[v].deprecated = msg
59-
})
6057

61-
return otplease(this.npm, this.npm.flatOptions, opts => fetch(uri, {
62-
...opts,
63-
spec: p,
64-
method: 'PUT',
65-
body: packument,
66-
ignoreBody: true,
67-
}))
58+
if (versions.length) {
59+
for (const v of versions) {
60+
packument.versions[v].deprecated = msg
61+
}
62+
return otplease(this.npm, this.npm.flatOptions, opts => fetch(uri, {
63+
...opts,
64+
spec: p,
65+
method: 'PUT',
66+
body: packument,
67+
ignoreBody: true,
68+
}))
69+
}
6870
}
6971
}
7072

‎test/lib/commands/deprecate.js

+16
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,19 @@ t.test('deprecates all versions when no range is specified', async t => {
153153
await npm.exec('deprecate', ['foo', message])
154154
t.match(joinedOutput(), '')
155155
})
156+
157+
t.test('does nothing if version does not actually exist', async t => {
158+
const { npm, joinedOutput } = await loadMockNpm(t, { config: { ...auth } })
159+
const registry = new MockRegistry({
160+
tap: t,
161+
registry: npm.config.get('registry'),
162+
authorization: token,
163+
})
164+
const manifest = registry.manifest({
165+
name: 'foo',
166+
versions,
167+
})
168+
await registry.package({ manifest, query: { write: true } })
169+
await npm.exec('deprecate', ['foo@1.0.99', 'this should be ignored'])
170+
t.match(joinedOutput(), '')
171+
})

0 commit comments

Comments
 (0)
Please sign in to comment.