2 module.exports = unpublish
4 var log = require("npmlog")
5 var npm = require("./npm.js")
6 var readJson = require("read-package-json")
7 var path = require("path")
8 var mapToRegistry = require("./utils/map-to-registry.js")
9 var npa = require("npm-package-arg")
10 var getPublishConfig = require("./utils/get-publish-config.js")
12 unpublish.usage = "npm unpublish <project>[@<version>]"
14 unpublish.completion = function (opts, cb) {
15 if (opts.conf.argv.remain.length >= 3) return cb()
16 npm.commands.whoami([], true, function (er, username) {
19 var un = encodeURIComponent(username)
21 var byUser = "-/by-user/" + un
22 mapToRegistry(byUser, npm.config, function (er, uri, auth) {
25 npm.registry.get(uri, { auth : auth }, function (er, pkgs) {
26 // do a bit of filtering at this point, so that we don't need
27 // to fetch versions for more than one thing, but also don't
28 // accidentally a whole project.
30 if (!pkgs || !pkgs.length) return cb()
31 var pp = npa(opts.partialWord).name
32 pkgs = pkgs.filter(function (p) {
33 return p.indexOf(pp) === 0
35 if (pkgs.length > 1) return cb(null, pkgs)
36 mapToRegistry(pkgs[0], npm.config, function (er, uri, auth) {
39 npm.registry.get(uri, { auth : auth }, function (er, d) {
41 var vers = Object.keys(d.versions)
42 if (!vers.length) return cb(null, pkgs)
43 return cb(null, vers.map(function (v) {
44 return pkgs[0] + "@" + v
53 function unpublish (args, cb) {
54 if (args.length > 1) return cb(unpublish.usage)
56 var thing = args.length ? npa(args[0]) : {}
57 , project = thing.name
58 , version = thing.rawSpec
60 log.silly("unpublish", "args[0]", args[0])
61 log.silly("unpublish", "thing", thing)
62 if (!version && !npm.config.get("force")) {
63 return cb("Refusing to delete entire project.\n"
64 + "Run with --force to do this.\n"
68 if (!project || path.resolve(project) === npm.localPrefix) {
69 // if there's a package.json in the current folder, then
70 // read the package name and version out of that.
71 var cwdJson = path.join(npm.localPrefix, "package.json")
72 return readJson(cwdJson, function (er, data) {
73 if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
74 if (er) return cb("Usage:\n" + unpublish.usage)
75 log.verbose('unpublish', data)
76 gotProject(data.name, data.version, data.publishConfig, cb)
79 return gotProject(project, version, cb)
82 function gotProject (project, version, publishConfig, cb_) {
83 if (typeof cb_ !== 'function') {
89 if (er) return cb_(er)
90 console.log("- " + project + (version ? "@" + version : ""))
94 var mappedConfig = getPublishConfig(publishConfig, npm.config, npm.registry)
95 var config = mappedConfig.config
96 var registry = mappedConfig.client
98 // remove from the cache first
99 npm.commands.cache(["clean", project, version], function (er) {
101 log.error("unpublish", "Failed to clean cache")
105 mapToRegistry(project, config, function (er, uri, auth) {
106 if (er) return cb(er)
112 registry.unpublish(uri, params, cb)