diff --git a/lib/commands/deprecate.js b/lib/commands/deprecate.js index 95eaf429120fa..d8d33ad9b9d03 100644 --- a/lib/commands/deprecate.js +++ b/lib/commands/deprecate.js @@ -14,6 +14,7 @@ class Deprecate extends BaseCommand { static params = [ 'registry', 'otp', + 'dry-run', ] static ignoreImplicitWorkspace = true @@ -56,17 +57,26 @@ class Deprecate extends BaseCommand { const versions = Object.keys(packument.versions) .filter(v => semver.satisfies(v, spec, { includePrerelease: true })) + const dryRun = this.npm.config.get('dry-run') + if (versions.length) { for (const v of versions) { packument.versions[v].deprecated = msg + if (msg) { + log.notice(`deprecating ${packument.name}@${v} with message "${msg}"`) + } else { + log.notice(`undeprecating ${packument.name}@${v}`) + } + } + if (!dryRun) { + return otplease(this.npm, this.npm.flatOptions, opts => npmFetch(uri, { + ...opts, + spec: p, + method: 'PUT', + body: packument, + ignoreBody: true, + })) } - return otplease(this.npm, this.npm.flatOptions, opts => npmFetch(uri, { - ...opts, - spec: p, - method: 'PUT', - body: packument, - ignoreBody: true, - })) } else { log.warn('deprecate', 'No version found for', p.rawSpec) } diff --git a/test/lib/commands/deprecate.js b/test/lib/commands/deprecate.js index 09aaeacfe8563..eda51bfef2895 100644 --- a/test/lib/commands/deprecate.js +++ b/test/lib/commands/deprecate.js @@ -129,7 +129,7 @@ t.test('deprecates given range', async t => { }) t.test('deprecates all versions when no range is specified', async t => { - const { npm, joinedOutput } = await loadMockNpm(t, { config: { ...auth } }) + const { npm, logs, joinedOutput } = await loadMockNpm(t, { config: { ...auth } }) const registry = new MockRegistry({ tap: t, registry: npm.config.get('registry'), @@ -151,6 +151,37 @@ t.test('deprecates all versions when no range is specified', async t => { }).reply(200, {}) await npm.exec('deprecate', ['foo', message]) + t.match(logs.notice, [ + `deprecating foo@1.0.0 with message "${message}"`, + `deprecating foo@1.0.1 with message "${message}"`, + `deprecating foo@1.0.1-pre with message "${message}"`, + ]) + t.match(joinedOutput(), '') +}) + +t.test('dry-run', async t => { + const { npm, logs, joinedOutput } = await loadMockNpm(t, { config: { + 'dry-run': true, + ...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 } }) + const message = 'test deprecation message' + + await npm.exec('deprecate', ['foo', message]) + t.match(logs.notice, [ + `deprecating foo@1.0.0 with message "${message}"`, + `deprecating foo@1.0.1 with message "${message}"`, + `deprecating foo@1.0.1-pre with message "${message}"`, + ]) t.match(joinedOutput(), '') })