@@ -9,13 +9,22 @@ const log = require('../utils/log-shim')
99const otplease = require ( '../utils/otplease.js' )
1010const getIdentity = require ( '../utils/get-identity.js' )
1111
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+
1216const BaseCommand = require ( '../base-command.js' )
1317class Unpublish extends BaseCommand {
1418 static description = 'Remove a package from the registry'
1519 static name = 'unpublish'
1620 static params = [ 'dry-run' , 'force' , 'workspace' , 'workspaces' ]
1721 static usage = [ '[<@scope>/]<pkg>[@<version>]' ]
1822
23+ async getKeysOfVersions ( name , opts ) {
24+ const json = await npmFetch . json ( npa ( name ) . escapedName , opts )
25+ return Object . keys ( json . versions )
26+ }
27+
1928 async completion ( args ) {
2029 const { partialWord, conf } = args
2130
@@ -44,8 +53,7 @@ class Unpublish extends BaseCommand {
4453 return pkgs
4554 }
4655
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 )
4957 if ( ! versions . length ) {
5058 return pkgs
5159 } else {
@@ -97,12 +105,26 @@ class Unpublish extends BaseCommand {
97105 const { name, version, publishConfig } = manifest
98106 const pkgJsonSpec = npa . resolve ( name , version )
99107 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+
100116 if ( ! dryRun ) {
101117 await otplease ( opts , opts => libunpub ( pkgJsonSpec , optsWithPub ) )
102118 }
103119 pkgName = name
104120 pkgVersion = version ? `@${ version } ` : ''
105121 } 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+ }
106128 if ( ! dryRun ) {
107129 await otplease ( opts , opts => libunpub ( spec , opts ) )
108130 }
0 commit comments