Skip to content

Commit 90a069e

Browse files
committed
unpublish: stop using npm-registry-client
1 parent 6ed9433 commit 90a069e

File tree

1 file changed

+96
-105
lines changed

1 file changed

+96
-105
lines changed

lib/unpublish.js

Lines changed: 96 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,110 @@
11
/* eslint-disable standard/no-callback-literal */
2+
'use strict'
23

34
module.exports = unpublish
45

5-
var log = require('npmlog')
6-
var npm = require('./npm.js')
7-
var readJson = require('read-package-json')
8-
var path = require('path')
9-
var mapToRegistry = require('./utils/map-to-registry.js')
10-
var npa = require('npm-package-arg')
11-
var getPublishConfig = require('./utils/get-publish-config.js')
12-
var output = require('./utils/output.js')
13-
14-
unpublish.usage = 'npm unpublish [<@scope>/]<pkg>[@<version>]'
15-
16-
unpublish.completion = function (opts, cb) {
17-
if (opts.conf.argv.remain.length >= 3) return cb()
18-
npm.commands.whoami([], true, function (er, username) {
19-
if (er) return cb()
20-
21-
var un = encodeURIComponent(username)
22-
if (!un) return cb()
23-
var byUser = '-/by-user/' + un
24-
mapToRegistry(byUser, npm.config, function (er, uri, auth) {
25-
if (er) return cb(er)
26-
27-
npm.registry.get(uri, { auth: auth }, function (er, pkgs) {
28-
// do a bit of filtering at this point, so that we don't need
29-
// to fetch versions for more than one thing, but also don't
30-
// accidentally a whole project.
31-
pkgs = pkgs[un]
32-
if (!pkgs || !pkgs.length) return cb()
33-
var pp = npa(opts.partialWord).name
34-
pkgs = pkgs.filter(function (p) {
35-
return p.indexOf(pp) === 0
36-
})
37-
if (pkgs.length > 1) return cb(null, pkgs)
38-
mapToRegistry(pkgs[0], npm.config, function (er, uri, auth) {
39-
if (er) return cb(er)
6+
const BB = require('bluebird')
7+
8+
const figgyPudding = require('figgy-pudding')
9+
const libaccess = require('libnpm/access')
10+
const libunpub = require('libnpm/unpublish')
11+
const log = require('npmlog')
12+
const npa = require('npm-package-arg')
13+
const npm = require('./npm.js')
14+
const npmConfig = require('./config/figgy-config.js')
15+
const npmFetch = require('npm-registry-fetch')
16+
const otplease = require('./utils/otplease.js')
17+
const output = require('./utils/output.js')
18+
const path = require('path')
19+
const readJson = BB.promisify(require('read-package-json'))
20+
const usage = require('./utils/usage.js')
21+
const whoami = BB.promisify(require('./whoami.js'))
22+
23+
unpublish.usage = usage('npm unpublish [<@scope>/]<pkg>[@<version>]')
24+
25+
function UsageError () {
26+
throw Object.assign(new Error(`Usage: ${unpublish.usage}`), {
27+
code: 'EUSAGE'
28+
})
29+
}
4030

41-
npm.registry.get(uri, { auth: auth }, function (er, d) {
42-
if (er) return cb(er)
43-
var vers = Object.keys(d.versions)
44-
if (!vers.length) return cb(null, pkgs)
45-
return cb(null, vers.map(function (v) {
46-
return pkgs[0] + '@' + v
47-
}))
48-
})
49-
})
31+
const UnpublishConfig = figgyPudding({
32+
force: {},
33+
loglevel: {},
34+
silent: {}
35+
})
36+
37+
unpublish.completion = function (cliOpts, cb) {
38+
if (cliOpts.conf.argv.remain.length >= 3) return cb()
39+
40+
whoami([], true).then(username => {
41+
if (!username) { return [] }
42+
const opts = UnpublishConfig(npmConfig())
43+
return libaccess.lsPackages(username, opts).then(access => {
44+
// do a bit of filtering at this point, so that we don't need
45+
// to fetch versions for more than one thing, but also don't
46+
// accidentally a whole project.
47+
let pkgs = Object.keys(access)
48+
if (!cliOpts.partialWord || !pkgs.length) { return pkgs }
49+
const pp = npa(cliOpts.partialWord).name
50+
pkgs = pkgs.filter(p => !p.indexOf(pp))
51+
if (pkgs.length > 1) return pkgs
52+
return npmFetch.json(npa(pkgs[0]).escapedName, opts).then(doc => {
53+
const vers = Object.keys(doc.versions)
54+
if (!vers.length) {
55+
return pkgs
56+
} else {
57+
return vers.map(v => `${pkgs[0]}@${v}`)
58+
}
5059
})
5160
})
52-
})
61+
}).nodeify(cb)
5362
}
5463

5564
function unpublish (args, cb) {
5665
if (args.length > 1) return cb(unpublish.usage)
5766

58-
var thing = args.length ? npa(args[0]) : {}
59-
var project = thing.name
60-
var version = thing.rawSpec
61-
62-
log.silly('unpublish', 'args[0]', args[0])
63-
log.silly('unpublish', 'thing', thing)
64-
if (!version && !npm.config.get('force')) {
65-
return cb(
66-
'Refusing to delete entire project.\n' +
67-
'Run with --force to do this.\n' +
68-
unpublish.usage
69-
)
70-
}
71-
72-
if (!project || path.resolve(project) === npm.localPrefix) {
73-
// if there's a package.json in the current folder, then
74-
// read the package name and version out of that.
75-
var cwdJson = path.join(npm.localPrefix, 'package.json')
76-
return readJson(cwdJson, function (er, data) {
77-
if (er && er.code !== 'ENOENT' && er.code !== 'ENOTDIR') return cb(er)
78-
if (er) return cb('Usage:\n' + unpublish.usage)
79-
log.verbose('unpublish', data)
80-
gotProject(data.name, data.version, data.publishConfig, cb)
81-
})
82-
}
83-
return gotProject(project, version, cb)
84-
}
85-
86-
function gotProject (project, version, publishConfig, cb_) {
87-
if (typeof cb_ !== 'function') {
88-
cb_ = publishConfig
89-
publishConfig = null
90-
}
91-
92-
function cb (er) {
93-
if (er) return cb_(er)
94-
output('- ' + project + (version ? '@' + version : ''))
95-
cb_()
96-
}
97-
98-
var mappedConfig = getPublishConfig(publishConfig, npm.config, npm.registry)
99-
var config = mappedConfig.config
100-
var registry = mappedConfig.client
101-
102-
// remove from the cache first
103-
// npm.commands.cache(['clean', project, version], function (er) {
104-
// if (er) {
105-
// log.error('unpublish', 'Failed to clean cache')
106-
// return cb(er)
107-
// }
108-
109-
mapToRegistry(project, config, function (er, uri, auth) {
110-
if (er) return cb(er)
111-
112-
var params = {
113-
version: version,
114-
auth: auth
67+
const spec = args.length && npa(args[0])
68+
const opts = UnpublishConfig(npmConfig())
69+
const version = spec.rawSpec
70+
BB.try(() => {
71+
log.silly('unpublish', 'args[0]', args[0])
72+
log.silly('unpublish', 'spec', spec)
73+
if (!version && !opts.force) {
74+
throw Object.assign(new Error(
75+
'Refusing to delete entire project.\n' +
76+
'Run with --force to do this.\n' +
77+
unpublish.usage
78+
), {code: 'EUSAGE'})
11579
}
116-
registry.unpublish(uri, params, cb)
117-
})
118-
// })
80+
if (!spec || path.resolve(spec.name) === npm.localPrefix) {
81+
// if there's a package.json in the current folder, then
82+
// read the package name and version out of that.
83+
const cwdJson = path.join(npm.localPrefix, 'package.json')
84+
return readJson(cwdJson).then(data => {
85+
log.verbose('unpublish', data)
86+
return otplease(opts, opts => {
87+
return libunpub(npa.resolve(data.name, data.version), opts.concat(data.publishConfig))
88+
})
89+
}, err => {
90+
if (err && err.code !== 'ENOENT' && err.code !== 'ENOTDIR') {
91+
throw err
92+
} else {
93+
UsageError()
94+
}
95+
})
96+
} else {
97+
return otplease(opts, opts => libunpub(spec, opts))
98+
}
99+
}).then(
100+
ret => {
101+
if (!opts.silent && opts.loglevel !== 'silent') {
102+
output(`-${spec.name}${
103+
spec.type === 'version' ? `@${spec.rawSpec}` : ''
104+
}`)
105+
}
106+
cb(null, ret)
107+
},
108+
err => err.code === 'EUSAGE' ? cb(err.message) : cb(err)
109+
)
119110
}

0 commit comments

Comments
 (0)