1
+ 'use strict'
2
+
1
3
const path = require ( 'path' )
2
4
const util = require ( 'util' )
3
5
const log = require ( 'npmlog' )
@@ -11,7 +13,7 @@ const npm = require('./npm.js')
11
13
const usageUtil = require ( './utils/usage.js' )
12
14
const output = require ( './utils/output.js' )
13
15
const otplease = require ( './utils/otplease.js' )
14
- const whoami = util . promisify ( require ( './whoami .js' ) )
16
+ const getIdentity = require ( './utils/get-identity .js' )
15
17
16
18
const usage = usageUtil ( 'unpublish' , 'npm unpublish [<@scope>/]<pkg>[@<version>]' )
17
19
@@ -25,18 +27,18 @@ const completionFn = async (args) => {
25
27
const { partialWord, conf } = args
26
28
27
29
if ( conf . argv . remain . length >= 3 )
28
- return
30
+ return [ ]
29
31
30
- const username = await whoami ( [ ] , true )
32
+ const opts = npm . flatOptions
33
+ const username = await getIdentity ( { ...opts } ) . catch ( ( ) => null )
31
34
if ( ! username )
32
35
return [ ]
33
- const opts = npm . flatOptions
34
36
35
37
const access = await libaccess . lsPackages ( username , opts )
36
38
// do a bit of filtering at this point, so that we don't need
37
39
// to fetch versions for more than one thing, but also don't
38
40
// accidentally a whole project
39
- let pkgs = Object . keys ( access )
41
+ let pkgs = Object . keys ( access || { } )
40
42
if ( ! partialWord || ! pkgs . length )
41
43
return pkgs
42
44
@@ -55,18 +57,20 @@ const completionFn = async (args) => {
55
57
56
58
async function unpublish ( args ) {
57
59
if ( args . length > 1 )
58
- throw usage
60
+ throw new Error ( usage )
59
61
60
62
const spec = args . length && npa ( args [ 0 ] )
61
63
const opts = npm . flatOptions
62
64
const { force, silent, loglevel } = opts
63
- let ret
65
+ let res
66
+ let pkgName
67
+ let pkgVersion
64
68
65
69
log . silly ( 'unpublish' , 'args[0]' , args [ 0 ] )
66
70
log . silly ( 'unpublish' , 'spec' , spec )
67
71
68
72
if ( ! spec . rawSpec && ! force ) {
69
- throw (
73
+ throw new Error (
70
74
'Refusing to delete entire project.\n' +
71
75
'Run with --force to do this.\n' +
72
76
usage
@@ -77,31 +81,34 @@ async function unpublish (args) {
77
81
// if there's a package.json in the current folder, then
78
82
// read the package name and version out of that.
79
83
const pkgJson = path . join ( npm . localPrefix , 'package.json' )
80
- const manifest = await readJson ( pkgJson )
81
-
82
- log . verbose ( 'unpublish' , manifest )
83
-
84
- const { name, version, publishConfig } = manifest
85
- const pkgJsonSpec = npa . resolve ( name , version )
86
-
84
+ let manifest
87
85
try {
88
- ret = await otplease ( opts , opts => libunpub ( pkgJsonSpec , { ... opts , publishConfig } ) )
86
+ manifest = await readJson ( pkgJson )
89
87
} catch ( err ) {
90
88
if ( err && err . code !== 'ENOENT' && err . code !== 'ENOTDIR' )
91
89
throw err
92
90
else
93
- throw `Usage: ${ usage } `
91
+ throw new Error ( `Usage: ${ usage } ` )
94
92
}
95
- } else
96
- ret = await otplease ( opts , opts => libunpub ( spec , opts ) )
97
93
98
- if ( ! silent && loglevel !== 'silent' ) {
99
- output ( `- ${ spec . name } ${
100
- spec . type === 'version' ? `@${ spec . rawSpec } ` : ''
101
- } `)
94
+ log . verbose ( 'unpublish' , manifest )
95
+
96
+ const { name, version, publishConfig } = manifest
97
+ const pkgJsonSpec = npa . resolve ( name , version )
98
+
99
+ res = await otplease ( opts , opts => libunpub ( pkgJsonSpec , { ...opts , publishConfig } ) )
100
+ pkgName = name
101
+ pkgVersion = version ? `@${ version } ` : ''
102
+ } else {
103
+ res = await otplease ( opts , opts => libunpub ( spec , opts ) )
104
+ pkgName = spec . name
105
+ pkgVersion = spec . type === 'version' ? `@${ spec . rawSpec } ` : ''
102
106
}
103
107
104
- return ret
108
+ if ( ! silent && loglevel !== 'silent' )
109
+ output ( `- ${ pkgName } ${ pkgVersion } ` )
110
+
111
+ return res
105
112
}
106
113
107
114
module . exports = Object . assign ( cmd , { completion, usage } )
0 commit comments