UNPKG

1.56 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 registry.get("/-/short", 60000, function (er, list) {
9 return cb(null, list || [])
10 })
11}
12
13var npm = require("./npm.js")
14 , registry = npm.registry
15 , log = require("npmlog")
16 , opener = require("opener")
17 , github = require('github-url-from-git')
18 , githubUserRepo = require("github-url-from-username-repo")
19 , path = require("path")
20 , readJson = require("read-package-json")
21 , fs = require("fs")
22
23function repo (args, cb) {
24 var n = args.length && args[0].split("@").shift() || '.'
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
36function 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 var url = github(r.url)
44 if (!url)
45 return cb(new Error('no repository: could not get url'))
46 opener(url, { command: npm.config.get("browser") }, cb)
47}
48
49function callRegistry (n, cb) {
50 registry.get(n + "/latest", 3600, function (er, d) {
51 if (er) return cb(er)
52 getUrlAndOpen(d, cb)
53 })
54}