Skip to content

Commit

Permalink
fix: minor tweaks to lib/unpublish.js
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ruyadorno committed Dec 8, 2020
1 parent 2b8b9bd commit 3d6dfba
Show file tree
Hide file tree
Showing 2 changed files with 537 additions and 24 deletions.
55 changes: 31 additions & 24 deletions lib/unpublish.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

const path = require('path')
const util = require('util')
const log = require('npmlog')
Expand All @@ -11,7 +13,7 @@ const npm = require('./npm.js')
const usageUtil = require('./utils/usage.js')
const output = require('./utils/output.js')
const otplease = require('./utils/otplease.js')
const whoami = util.promisify(require('./whoami.js'))
const getIdentity = require('./utils/get-identity.js')

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

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

if (conf.argv.remain.length >= 3)
return
return []

const username = await whoami([], true)
const opts = npm.flatOptions
const username = await getIdentity({ ...opts }).catch(() => null)
if (!username)
return []
const opts = npm.flatOptions

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

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

async function unpublish (args) {
if (args.length > 1)
throw usage
throw new Error(usage)

const spec = args.length && npa(args[0])
const opts = npm.flatOptions
const { force, silent, loglevel } = opts
let ret
let res
let pkgName
let pkgVersion

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

if (!spec.rawSpec && !force) {
throw (
throw new Error(
'Refusing to delete entire project.\n' +
'Run with --force to do this.\n' +
usage
Expand All @@ -77,31 +81,34 @@ async function unpublish (args) {
// if there's a package.json in the current folder, then
// read the package name and version out of that.
const pkgJson = path.join(npm.localPrefix, 'package.json')
const manifest = await readJson(pkgJson)

log.verbose('unpublish', manifest)

const { name, version, publishConfig } = manifest
const pkgJsonSpec = npa.resolve(name, version)

let manifest
try {
ret = await otplease(opts, opts => libunpub(pkgJsonSpec, { ...opts, publishConfig }))
manifest = await readJson(pkgJson)
} catch (err) {
if (err && err.code !== 'ENOENT' && err.code !== 'ENOTDIR')
throw err
else
throw `Usage: ${usage}`
throw new Error(`Usage: ${usage}`)
}
} else
ret = await otplease(opts, opts => libunpub(spec, opts))

if (!silent && loglevel !== 'silent') {
output(`- ${spec.name}${
spec.type === 'version' ? `@${spec.rawSpec}` : ''
}`)
log.verbose('unpublish', manifest)

const { name, version, publishConfig } = manifest
const pkgJsonSpec = npa.resolve(name, version)

res = await otplease(opts, opts => libunpub(pkgJsonSpec, { ...opts, publishConfig }))
pkgName = name
pkgVersion = version ? `@${version}` : ''
} else {
res = await otplease(opts, opts => libunpub(spec, opts))
pkgName = spec.name
pkgVersion = spec.type === 'version' ? `@${spec.rawSpec}` : ''
}

return ret
if (!silent && loglevel !== 'silent')
output(`- ${pkgName}${pkgVersion}`)

return res
}

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

0 comments on commit 3d6dfba

Please sign in to comment.