Skip to content

Commit

Permalink
feat: add dry-run to deprecate/undeprecate commands
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jan 6, 2025
1 parent 1764a37 commit 7f6c997
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
24 changes: 17 additions & 7 deletions lib/commands/deprecate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Deprecate extends BaseCommand {
static params = [
'registry',
'otp',
'dry-run',
]

static ignoreImplicitWorkspace = true
Expand Down Expand Up @@ -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)
}
Expand Down
33 changes: 32 additions & 1 deletion test/lib/commands/deprecate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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 [email protected] with message "${message}"`,
`deprecating [email protected] with message "${message}"`,
`deprecating [email protected] 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 [email protected] with message "${message}"`,
`deprecating [email protected] with message "${message}"`,
`deprecating [email protected] with message "${message}"`,
])
t.match(joinedOutput(), '')
})

Expand Down

0 comments on commit 7f6c997

Please sign in to comment.