From 8be672b21f7d434c898bb4e97dc3fca6e54b29a1 Mon Sep 17 00:00:00 2001 From: Gar Date: Fri, 13 Jan 2023 13:51:27 -0800 Subject: [PATCH] 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 --- lib/commands/deprecate.js | 24 +++++++++++++----------- test/lib/commands/deprecate.js | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/lib/commands/deprecate.js b/lib/commands/deprecate.js index 1e1b8994c55a3..844d5f60a02ab 100644 --- a/lib/commands/deprecate.js +++ b/lib/commands/deprecate.js @@ -52,19 +52,21 @@ class Deprecate extends BaseCommand { query: { write: true }, }) - Object.keys(packument.versions) + const versions = Object.keys(packument.versions) .filter(v => semver.satisfies(v, spec, { includePrerelease: true })) - .forEach(v => { - packument.versions[v].deprecated = msg - }) - return otplease(this.npm, this.npm.flatOptions, opts => fetch(uri, { - ...opts, - spec: p, - method: 'PUT', - body: packument, - ignoreBody: true, - })) + if (versions.length) { + for (const v of versions) { + packument.versions[v].deprecated = msg + } + return otplease(this.npm, this.npm.flatOptions, opts => fetch(uri, { + ...opts, + spec: p, + method: 'PUT', + body: packument, + ignoreBody: true, + })) + } } } diff --git a/test/lib/commands/deprecate.js b/test/lib/commands/deprecate.js index 22ddfe182de6e..48513c7303a01 100644 --- a/test/lib/commands/deprecate.js +++ b/test/lib/commands/deprecate.js @@ -153,3 +153,19 @@ t.test('deprecates all versions when no range is specified', async t => { await npm.exec('deprecate', ['foo', message]) t.match(joinedOutput(), '') }) + +t.test('does nothing if version does not actually exist', async t => { + const { npm, joinedOutput } = await loadMockNpm(t, { config: { ...auth } }) + const registry = new MockRegistry({ + tap: t, + registry: npm.config.get('registry'), + authorization: token, + }) + const manifest = registry.manifest({ + name: 'foo', + versions, + }) + await registry.package({ manifest, query: { write: true } }) + await npm.exec('deprecate', ['foo@1.0.99', 'this should be ignored']) + t.match(joinedOutput(), '') +})