Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/commands/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Unpublish extends BaseCommand {
query: { write: true },
_isRoot: true,
})
return Object.keys(packument.versions)
return Object.keys(packument.versions || {})
}

static async completion (args, npm) {
Expand Down Expand Up @@ -136,6 +136,9 @@ class Unpublish extends BaseCommand {
}

const versions = await Unpublish.getKeysOfVersions(spec.name, opts)
if (versions.length === 0 && spec.type === 'version') {
throw this.usageError(`Refusing to delete ${spec.name}@${spec.rawSpec}: the registry returned no versions for ${spec.name}.`)
}
if (versions.length === 1 && spec.rawSpec === versions[0] && !force) {
throw this.usageError(LAST_REMAINING_VERSION_ERROR)
}
Expand Down
43 changes: 43 additions & 0 deletions test/lib/commands/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,49 @@ t.test('unpublish <pkg>@version last version', async t => {
)
})

t.test('unpublish <pkg>@version of a packument with no versions', async t => {
const { npm } = await loadMockNpm(t, {
config: {
...auth,
},
})
const registry = new MockRegistry({
tap: t,
registry: npm.config.get('registry'),
authorization: 'test-auth-token',
})
const manifest = registry.manifest({ name: pkg })
delete manifest.versions
await registry.package({ manifest, query: { write: true } })

await t.rejects(
npm.exec('unpublish', ['test-package@1.0.0']),
/the registry returned no versions for test-package/
)
})

t.test('unpublish <pkg>@version of a packument with no versions with --force', async t => {
const { npm } = await loadMockNpm(t, {
config: {
force: true,
...auth,
},
})
const registry = new MockRegistry({
tap: t,
registry: npm.config.get('registry'),
authorization: 'test-auth-token',
})
const manifest = registry.manifest({ name: pkg })
manifest.versions = {}
await registry.package({ manifest, query: { write: true } })

await t.rejects(
npm.exec('unpublish', ['test-package@1.0.0']),
/the registry returned no versions for test-package/
)
})

t.test('no version found in package.json no force', async t => {
const { npm } = await loadMockNpm(t, {
config: {
Expand Down