Skip to content

Commit b7d74b6

Browse files
ruyadornoisaacs
authored andcommitted
fix: minor tweaks to lib/unpublish.js
- Fix handling missing files error on reading package.json - Fixes autocompletion - Fixes printing name and version when using no args - Adds `test/lib/unpublish.js` tests Fixes: npm/statusboard#180 PR-URL: #2304 Credit: @ruyadorno Close: #2304 Reviewed-by: @isaacs
1 parent 3db90d9 commit b7d74b6

File tree

2 files changed

+537
-24
lines changed

2 files changed

+537
-24
lines changed

lib/unpublish.js

+31-24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
const path = require('path')
24
const util = require('util')
35
const log = require('npmlog')
@@ -11,7 +13,7 @@ const npm = require('./npm.js')
1113
const usageUtil = require('./utils/usage.js')
1214
const output = require('./utils/output.js')
1315
const otplease = require('./utils/otplease.js')
14-
const whoami = util.promisify(require('./whoami.js'))
16+
const getIdentity = require('./utils/get-identity.js')
1517

1618
const usage = usageUtil('unpublish', 'npm unpublish [<@scope>/]<pkg>[@<version>]')
1719

@@ -25,18 +27,18 @@ const completionFn = async (args) => {
2527
const { partialWord, conf } = args
2628

2729
if (conf.argv.remain.length >= 3)
28-
return
30+
return []
2931

30-
const username = await whoami([], true)
32+
const opts = npm.flatOptions
33+
const username = await getIdentity({ ...opts }).catch(() => null)
3134
if (!username)
3235
return []
33-
const opts = npm.flatOptions
3436

3537
const access = await libaccess.lsPackages(username, opts)
3638
// do a bit of filtering at this point, so that we don't need
3739
// to fetch versions for more than one thing, but also don't
3840
// accidentally a whole project
39-
let pkgs = Object.keys(access)
41+
let pkgs = Object.keys(access || {})
4042
if (!partialWord || !pkgs.length)
4143
return pkgs
4244

@@ -55,18 +57,20 @@ const completionFn = async (args) => {
5557

5658
async function unpublish (args) {
5759
if (args.length > 1)
58-
throw usage
60+
throw new Error(usage)
5961

6062
const spec = args.length && npa(args[0])
6163
const opts = npm.flatOptions
6264
const { force, silent, loglevel } = opts
63-
let ret
65+
let res
66+
let pkgName
67+
let pkgVersion
6468

6569
log.silly('unpublish', 'args[0]', args[0])
6670
log.silly('unpublish', 'spec', spec)
6771

6872
if (!spec.rawSpec && !force) {
69-
throw (
73+
throw new Error(
7074
'Refusing to delete entire project.\n' +
7175
'Run with --force to do this.\n' +
7276
usage
@@ -77,31 +81,34 @@ async function unpublish (args) {
7781
// if there's a package.json in the current folder, then
7882
// read the package name and version out of that.
7983
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
8785
try {
88-
ret = await otplease(opts, opts => libunpub(pkgJsonSpec, { ...opts, publishConfig }))
86+
manifest = await readJson(pkgJson)
8987
} catch (err) {
9088
if (err && err.code !== 'ENOENT' && err.code !== 'ENOTDIR')
9189
throw err
9290
else
93-
throw `Usage: ${usage}`
91+
throw new Error(`Usage: ${usage}`)
9492
}
95-
} else
96-
ret = await otplease(opts, opts => libunpub(spec, opts))
9793

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}` : ''
102106
}
103107

104-
return ret
108+
if (!silent && loglevel !== 'silent')
109+
output(`- ${pkgName}${pkgVersion}`)
110+
111+
return res
105112
}
106113

107114
module.exports = Object.assign(cmd, { completion, usage })

0 commit comments

Comments
 (0)