Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEmin Buğra Saral <eminbugrasaral@me.com>2022-01-07 00:11:04 +0300
committernlf <quitlahok@gmail.com>2022-01-07 00:15:25 +0300
commit2ac540b0ccd016a14676ad891758e8d9e903a12c (patch)
tree3d280d1d4b7ecd45ff62f0442f8dcb8537aa6159 /lib
parent1f0d1370ff6bf2ca978ef0d7d32640314c62204e (diff)
fix(unpublish): Show warning on unpublish command when last version (#4191)
Diffstat (limited to 'lib')
-rw-r--r--lib/commands/unpublish.js26
1 files changed, 24 insertions, 2 deletions
diff --git a/lib/commands/unpublish.js b/lib/commands/unpublish.js
index 578890025..85c366381 100644
--- a/lib/commands/unpublish.js
+++ b/lib/commands/unpublish.js
@@ -9,6 +9,10 @@ const log = require('../utils/log-shim')
const otplease = require('../utils/otplease.js')
const getIdentity = require('../utils/get-identity.js')
+const LAST_REMAINING_VERSION_ERROR = 'Refusing to delete the last version of the package. ' +
+'It will block from republishing a new version for 24 hours.\n' +
+'Run with --force to do this.'
+
const BaseCommand = require('../base-command.js')
class Unpublish extends BaseCommand {
static description = 'Remove a package from the registry'
@@ -16,6 +20,11 @@ class Unpublish extends BaseCommand {
static params = ['dry-run', 'force', 'workspace', 'workspaces']
static usage = ['[<@scope>/]<pkg>[@<version>]']
+ async getKeysOfVersions (name, opts) {
+ const json = await npmFetch.json(npa(name).escapedName, opts)
+ return Object.keys(json.versions)
+ }
+
async completion (args) {
const { partialWord, conf } = args
@@ -44,8 +53,7 @@ class Unpublish extends BaseCommand {
return pkgs
}
- const json = await npmFetch.json(npa(pkgs[0]).escapedName, opts)
- const versions = Object.keys(json.versions)
+ const versions = await this.getKeysOfVersions(pkgs[0], opts)
if (!versions.length) {
return pkgs
} else {
@@ -97,12 +105,26 @@ class Unpublish extends BaseCommand {
const { name, version, publishConfig } = manifest
const pkgJsonSpec = npa.resolve(name, version)
const optsWithPub = { ...opts, publishConfig }
+
+ const versions = await this.getKeysOfVersions(name, optsWithPub)
+ if (versions.length === 1 && !force) {
+ throw this.usageError(
+ LAST_REMAINING_VERSION_ERROR
+ )
+ }
+
if (!dryRun) {
await otplease(opts, opts => libunpub(pkgJsonSpec, optsWithPub))
}
pkgName = name
pkgVersion = version ? `@${version}` : ''
} else {
+ const versions = await this.getKeysOfVersions(spec.name, opts)
+ if (versions.length === 1 && !force) {
+ throw this.usageError(
+ LAST_REMAINING_VERSION_ERROR
+ )
+ }
if (!dryRun) {
await otplease(opts, opts => libunpub(spec, opts))
}