@@ -9,13 +9,22 @@ const log = require('../utils/log-shim')
9
9
const otplease = require ( '../utils/otplease.js' )
10
10
const getIdentity = require ( '../utils/get-identity.js' )
11
11
12
+ const LAST_REMAINING_VERSION_ERROR = 'Refusing to delete the last version of the package. ' +
13
+ 'It will block from republishing a new version for 24 hours.\n' +
14
+ 'Run with --force to do this.'
15
+
12
16
const BaseCommand = require ( '../base-command.js' )
13
17
class Unpublish extends BaseCommand {
14
18
static description = 'Remove a package from the registry'
15
19
static name = 'unpublish'
16
20
static params = [ 'dry-run' , 'force' , 'workspace' , 'workspaces' ]
17
21
static usage = [ '[<@scope>/]<pkg>[@<version>]' ]
18
22
23
+ async getKeysOfVersions ( name , opts ) {
24
+ const json = await npmFetch . json ( npa ( name ) . escapedName , opts )
25
+ return Object . keys ( json . versions )
26
+ }
27
+
19
28
async completion ( args ) {
20
29
const { partialWord, conf } = args
21
30
@@ -44,8 +53,7 @@ class Unpublish extends BaseCommand {
44
53
return pkgs
45
54
}
46
55
47
- const json = await npmFetch . json ( npa ( pkgs [ 0 ] ) . escapedName , opts )
48
- const versions = Object . keys ( json . versions )
56
+ const versions = await this . getKeysOfVersions ( pkgs [ 0 ] , opts )
49
57
if ( ! versions . length ) {
50
58
return pkgs
51
59
} else {
@@ -97,12 +105,26 @@ class Unpublish extends BaseCommand {
97
105
const { name, version, publishConfig } = manifest
98
106
const pkgJsonSpec = npa . resolve ( name , version )
99
107
const optsWithPub = { ...opts , publishConfig }
108
+
109
+ const versions = await this . getKeysOfVersions ( name , optsWithPub )
110
+ if ( versions . length === 1 && ! force ) {
111
+ throw this . usageError (
112
+ LAST_REMAINING_VERSION_ERROR
113
+ )
114
+ }
115
+
100
116
if ( ! dryRun ) {
101
117
await otplease ( opts , opts => libunpub ( pkgJsonSpec , optsWithPub ) )
102
118
}
103
119
pkgName = name
104
120
pkgVersion = version ? `@${ version } ` : ''
105
121
} else {
122
+ const versions = await this . getKeysOfVersions ( spec . name , opts )
123
+ if ( versions . length === 1 && ! force ) {
124
+ throw this . usageError (
125
+ LAST_REMAINING_VERSION_ERROR
126
+ )
127
+ }
106
128
if ( ! dryRun ) {
107
129
await otplease ( opts , opts => libunpub ( spec , opts ) )
108
130
}
0 commit comments