The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / npm / lib / unpublish.js @ 5aef65a9

History | View | Annotate | Download (2.46 KB)

1

    
2
module.exports = unpublish
3

    
4
var log = require("npmlog")
5
  , npm = require("./npm.js")
6
  , registry = npm.registry
7
  , readJson = require("read-package-json")
8
  , path = require("path")
9

    
10
unpublish.usage = "npm unpublish <project>[@<version>]"
11

    
12
unpublish.completion = function (opts, cb) {
13
  if (opts.conf.argv.remain.length >= 3) return cb()
14
  var un = encodeURIComponent(npm.config.get("username"))
15
  if (!un) return cb()
16
  registry.get("/-/by-user/"+un, function (er, pkgs) {
17
    // do a bit of filtering at this point, so that we don't need
18
    // to fetch versions for more than one thing, but also don't
19
    // accidentally a whole project.
20
    pkgs = pkgs[un]
21
    if (!pkgs || !pkgs.length) return cb()
22
    var partial = opts.partialWord.split("@")
23
      , pp = partial.shift()
24
      , pv = partial.join("@")
25
    pkgs = pkgs.filter(function (p) {
26
      return p.indexOf(pp) === 0
27
    })
28
    if (pkgs.length > 1) return cb(null, pkgs)
29
    registry.get(pkgs[0], function (er, d) {
30
      if (er) return cb(er)
31
      var vers = Object.keys(d.versions)
32
      if (!vers.length) return cb(null, pkgs)
33
      return cb(null, vers.map(function (v) {
34
        return pkgs[0]+"@"+v
35
      }))
36
    })
37
  })
38
}
39

    
40
function unpublish (args, cb) {
41

    
42
  if (args.length > 1) return cb(unpublish.usage)
43

    
44
  var thing = args.length ? args.shift().split("@") : []
45
    , project = thing.shift()
46
    , version = thing.join("@")
47

    
48
  if (!version && !npm.config.get("force")) {
49
    return cb("Refusing to delete entire project.\n"
50
             +"Run with --force to do this.\n"
51
             +unpublish.usage)
52
  }
53

    
54
  if (!project || path.resolve(project) === npm.prefix) {
55
    // if there's a package.json in the current folder, then
56
    // read the package name and version out of that.
57
    var cwdJson = path.join(process.cwd(), "package.json")
58
    return readJson(cwdJson, function (er, data) {
59
      if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er)
60
      if (er) return cb("Usage:\n"+unpublish.usage)
61
      gotProject(data.name, data.version, cb)
62
    })
63
  }
64
  return gotProject(project, version, cb)
65
}
66

    
67
function gotProject (project, version, cb_) {
68
  function cb (er) {
69
    if (er) return cb_(er)
70
    console.log("- " + project + (version ? "@" + version : ""))
71
    cb_()
72
  }
73

    
74
  // remove from the cache first
75
  npm.commands.cache(["clean", project, version], function (er) {
76
    if (er) {
77
      log.error("unpublish", "Failed to clean cache")
78
      return cb(er)
79
    }
80

    
81
    registry.unpublish(project, version, cb)
82
  })
83
}