]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/repo.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / repo.js
1
2 module.exports = repo
3
4 repo.usage = "npm repo <pkgname>"
5
6 var npm = require("./npm.js")
7   , opener = require("opener")
8   , github = require("github-url-from-git")
9   , githubUserRepo = require("github-url-from-username-repo")
10   , path = require("path")
11   , readJson = require("read-package-json")
12   , fs = require("fs")
13   , url_ = require("url")
14   , mapToRegistry = require("./utils/map-to-registry.js")
15   , npa = require("npm-package-arg")
16
17 repo.completion = function (opts, cb) {
18   // FIXME: there used to be registry completion here, but it stopped making
19   // sense somewhere around 50,000 packages on the registry
20   cb()
21 }
22
23 function repo (args, cb) {
24   var n = args.length && npa(args[0]).name || "."
25   fs.stat(n, function (er, s) {
26     if (er && er.code === "ENOENT") return callRegistry(n, cb)
27     else if (er) return cb(er)
28     if (!s.isDirectory()) return callRegistry(n, cb)
29     readJson(path.resolve(n, "package.json"), function (er, d) {
30       if (er) return cb(er)
31       getUrlAndOpen(d, cb)
32     })
33   })
34 }
35
36 function getUrlAndOpen (d, cb) {
37   var r = d.repository
38   if (!r) return cb(new Error("no repository"))
39   // XXX remove this when npm@v1.3.10 from node 0.10 is deprecated
40   // from https://github.com/npm/npm-www/issues/418
41   if (githubUserRepo(r.url))
42     r.url = githubUserRepo(r.url)
43
44   var url = (r.url && ~r.url.indexOf("github"))
45           ? github(r.url)
46           : nonGithubUrl(r.url)
47
48   if (!url)
49     return cb(new Error("no repository: could not get url"))
50   opener(url, { command: npm.config.get("browser") }, cb)
51 }
52
53 function callRegistry (n, cb) {
54   mapToRegistry(n, npm.config, function (er, uri) {
55     if (er) return cb(er)
56
57     npm.registry.get(uri + "/latest", { timeout : 3600 }, function (er, d) {
58       if (er) return cb(er)
59       getUrlAndOpen(d, cb)
60     })
61   })
62 }
63
64 function nonGithubUrl (url) {
65   try {
66     var idx = url.indexOf("@")
67     if (idx !== -1) {
68       url = url.slice(idx+1).replace(/:([^\d]+)/, "/$1")
69     }
70     url = url_.parse(url)
71     var protocol = url.protocol === "https:"
72                  ? "https:"
73                  : "http:"
74     return protocol + "//" + (url.host || "") +
75       url.path.replace(/\.git$/, "")
76   }
77   catch(e) {}
78 }