This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
unpublish.js
99 lines (84 loc) · 2.98 KB
/
unpublish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module.exports = unpublish
var log = require("npmlog")
, npm = require("./npm.js")
, registry = npm.registry
, readJson = require("read-package-json")
, path = require("path")
, mapToRegistry = require("./utils/map-to-registry.js")
unpublish.usage = "npm unpublish <project>[@<version>]"
unpublish.completion = function (opts, cb) {
if (opts.conf.argv.remain.length >= 3) return cb()
npm.commands.whoami([], true, function (er, username) {
if (er) return cb()
var un = encodeURIComponent(username)
if (!un) return cb()
var byUser = "-/by-user/" + un
mapToRegistry(byUser, npm.config, function (er, uri) {
if (er) return cb(er)
registry.get(uri, null, function (er, pkgs) {
// 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.
pkgs = pkgs[un]
if (!pkgs || !pkgs.length) return cb()
var partial = opts.partialWord.split("@")
, pp = partial.shift()
pkgs = pkgs.filter(function (p) {
return p.indexOf(pp) === 0
})
if (pkgs.length > 1) return cb(null, pkgs)
mapToRegistry(pkgs[0], npm.config, function (er, uri) {
if (er) return cb(er)
registry.get(uri, null, function (er, d) {
if (er) return cb(er)
var vers = Object.keys(d.versions)
if (!vers.length) return cb(null, pkgs)
return cb(null, vers.map(function (v) {
return pkgs[0] + "@" + v
}))
})
})
})
})
})
}
function unpublish (args, cb) {
if (args.length > 1) return cb(unpublish.usage)
var thing = args.length ? args.shift().split("@") : []
, project = thing.shift()
, version = thing.join("@")
if (!version && !npm.config.get("force")) {
return cb("Refusing to delete entire project.\n"
+ "Run with --force to do this.\n"
+ unpublish.usage)
}
if (!project || path.resolve(project) === npm.prefix) {
// if there's a package.json in the current folder, then
// read the package name and version out of that.
var cwdJson = path.join(process.cwd(), "package.json")
return readJson(cwdJson, function (er, data) {
if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
if (er) return cb("Usage:\n" + unpublish.usage)
gotProject(data.name, data.version, cb)
})
}
return gotProject(project, version, cb)
}
function gotProject (project, version, cb_) {
function cb (er) {
if (er) return cb_(er)
console.log("- " + project + (version ? "@" + version : ""))
cb_()
}
// remove from the cache first
npm.commands.cache(["clean", project, version], function (er) {
if (er) {
log.error("unpublish", "Failed to clean cache")
return cb(er)
}
mapToRegistry(project, npm.config, function (er, uri) {
if (er) return cb(er)
registry.unpublish(uri, version, cb)
})
})
}