1
1
const path = require ( 'path' )
2
2
const util = require ( 'util' )
3
- const log = require ( 'npmlog' )
4
3
const npa = require ( 'npm-package-arg' )
5
4
const libaccess = require ( 'libnpmaccess' )
6
5
const npmFetch = require ( 'npm-registry-fetch' )
7
6
const libunpub = require ( 'libnpmpublish' ) . unpublish
8
7
const readJson = util . promisify ( require ( 'read-package-json' ) )
9
8
9
+ const getWorkspaces = require ( './workspaces/get-workspaces.js' )
10
10
const otplease = require ( './utils/otplease.js' )
11
11
const getIdentity = require ( './utils/get-identity.js' )
12
12
@@ -22,13 +22,13 @@ class Unpublish extends BaseCommand {
22
22
}
23
23
24
24
/* istanbul ignore next - see test/lib/load-all-commands.js */
25
- static get usage ( ) {
26
- return [ '[<@scope>/]<pkg>[@<version>] ' ]
25
+ static get params ( ) {
26
+ return [ 'dry-run' , 'force' , 'workspace' , 'workspaces ']
27
27
}
28
28
29
29
/* istanbul ignore next - see test/lib/load-all-commands.js */
30
- static get params ( ) {
31
- return [ 'force ' ]
30
+ static get usage ( ) {
31
+ return [ '[<@scope>/]<pkg>[@<version>] ' ]
32
32
}
33
33
34
34
async completion ( args ) {
@@ -67,25 +67,29 @@ class Unpublish extends BaseCommand {
67
67
this . unpublish ( args ) . then ( ( ) => cb ( ) ) . catch ( cb )
68
68
}
69
69
70
+ execWorkspaces ( args , filters , cb ) {
71
+ this . unpublishWorkspaces ( args , filters ) . then ( ( ) => cb ( ) ) . catch ( cb )
72
+ }
73
+
70
74
async unpublish ( args ) {
71
75
if ( args . length > 1 )
72
- throw new Error ( this . usage )
76
+ throw this . usageError ( )
73
77
74
78
const spec = args . length && npa ( args [ 0 ] )
75
79
const force = this . npm . config . get ( 'force' )
76
- const silent = this . npm . config . get ( 'silent' )
77
80
const loglevel = this . npm . config . get ( 'loglevel' )
81
+ const silent = loglevel === 'silent'
82
+ const dryRun = this . npm . config . get ( 'dry-run' )
78
83
let pkgName
79
84
let pkgVersion
80
85
81
- log . silly ( 'unpublish' , 'args[0]' , args [ 0 ] )
82
- log . silly ( 'unpublish' , 'spec' , spec )
86
+ this . npm . log . silly ( 'unpublish' , 'args[0]' , args [ 0 ] )
87
+ this . npm . log . silly ( 'unpublish' , 'spec' , spec )
83
88
84
- if ( ! spec . rawSpec && ! force ) {
85
- throw new Error (
89
+ if ( ( ! spec || ! spec . rawSpec ) && ! force ) {
90
+ throw this . usageError (
86
91
'Refusing to delete entire project.\n' +
87
- 'Run with --force to do this.\n' +
88
- this . usage
92
+ 'Run with --force to do this.'
89
93
)
90
94
}
91
95
@@ -101,25 +105,44 @@ class Unpublish extends BaseCommand {
101
105
if ( err && err . code !== 'ENOENT' && err . code !== 'ENOTDIR' )
102
106
throw err
103
107
else
104
- throw new Error ( `Usage: ${ this . usage } ` )
108
+ throw this . usageError ( )
105
109
}
106
110
107
- log . verbose ( 'unpublish' , manifest )
111
+ this . npm . log . verbose ( 'unpublish' , manifest )
108
112
109
113
const { name, version, publishConfig } = manifest
110
114
const pkgJsonSpec = npa . resolve ( name , version )
111
115
const optsWithPub = { ...opts , publishConfig }
112
- await otplease ( opts , opts => libunpub ( pkgJsonSpec , optsWithPub ) )
116
+ if ( ! dryRun )
117
+ await otplease ( opts , opts => libunpub ( pkgJsonSpec , optsWithPub ) )
113
118
pkgName = name
114
119
pkgVersion = version ? `@${ version } ` : ''
115
120
} else {
116
- await otplease ( opts , opts => libunpub ( spec , opts ) )
121
+ if ( ! dryRun )
122
+ await otplease ( opts , opts => libunpub ( spec , opts ) )
117
123
pkgName = spec . name
118
124
pkgVersion = spec . type === 'version' ? `@${ spec . rawSpec } ` : ''
119
125
}
120
126
121
- if ( ! silent && loglevel !== 'silent' )
127
+ if ( ! silent )
122
128
this . npm . output ( `- ${ pkgName } ${ pkgVersion } ` )
123
129
}
130
+
131
+ async unpublishWorkspaces ( args , filters ) {
132
+ const workspaces =
133
+ await getWorkspaces ( filters , { path : this . npm . localPrefix } )
134
+
135
+ const force = this . npm . config . get ( 'force' )
136
+ if ( ! force ) {
137
+ throw this . usageError (
138
+ 'Refusing to delete entire project(s).\n' +
139
+ 'Run with --force to do this.'
140
+ )
141
+ }
142
+
143
+ for ( const [ name , workspace ] of workspaces . entries ( ) ) {
144
+ await this . unpublish ( [ name ] )
145
+ }
146
+ }
124
147
}
125
148
module . exports = Unpublish
0 commit comments