UNPKG

2.32 kBJavaScriptView Raw
1
2module.exports = repo
3
4repo.usage = "npm repo <pkgname>"
5
6repo.completion = function (opts, cb) {
7 if (opts.conf.argv.remain.length > 2) return cb()
8 mapToRegistry("/-/short", npm.config, function (er, uri) {
9 if (er) return cb(er)
10
11 registry.get(uri, { timeout : 60000 }, function (er, list) {
12 return cb(null, list || [])
13 })
14 })
15}
16
17var npm = require("./npm.js")
18 , registry = npm.registry
19 , opener = require("opener")
20 , github = require('github-url-from-git')
21 , githubUserRepo = require("github-url-from-username-repo")
22 , path = require("path")
23 , readJson = require("read-package-json")
24 , fs = require("fs")
25 , url_ = require("url")
26 , mapToRegistry = require("./utils/map-to-registry.js")
27 , npa = require("npm-package-arg")
28
29function repo (args, cb) {
30 var n = args.length && npa(args[0]).name || "."
31 fs.stat(n, function (er, s) {
32 if (er && er.code === "ENOENT") return callRegistry(n, cb)
33 else if (er) return cb(er)
34 if (!s.isDirectory()) return callRegistry(n, cb)
35 readJson(path.resolve(n, "package.json"), function (er, d) {
36 if (er) return cb(er)
37 getUrlAndOpen(d, cb)
38 })
39 })
40}
41
42function getUrlAndOpen (d, cb) {
43 var r = d.repository
44 if (!r) return cb(new Error('no repository'))
45 // XXX remove this when npm@v1.3.10 from node 0.10 is deprecated
46 // from https://github.com/npm/npm-www/issues/418
47 if (githubUserRepo(r.url))
48 r.url = githubUserRepo(r.url)
49
50 var url = (r.url && ~r.url.indexOf('github'))
51 ? github(r.url)
52 : nonGithubUrl(r.url)
53
54 if (!url)
55 return cb(new Error('no repository: could not get url'))
56 opener(url, { command: npm.config.get("browser") }, cb)
57}
58
59function callRegistry (n, cb) {
60 mapToRegistry(n, npm.config, function (er, uri) {
61 if (er) return cb(er)
62
63 registry.get(uri + "/latest", { timeout : 3600 }, function (er, d) {
64 if (er) return cb(er)
65 getUrlAndOpen(d, cb)
66 })
67 })
68}
69
70function nonGithubUrl (url) {
71 try {
72 var idx = url.indexOf('@')
73 if (idx !== -1) {
74 url = url.slice(idx+1).replace(/:([^\d]+)/, '/$1')
75 }
76 url = url_.parse(url)
77 var protocol = url.protocol === 'https:'
78 ? 'https:'
79 : 'http:'
80 return protocol + '//' + (url.host || '') +
81 url.path.replace(/\.git$/, '')
82 }
83 catch(e) {}
84}