1 |
|
2 | module.exports = repo
|
3 |
|
4 | repo.usage = "npm repo <pkgname>"
|
5 |
|
6 | repo.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 |
|
13 | var 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 |
|
23 | function 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 |
|
36 | function getUrlAndOpen (d, cb) {
|
37 | var r = d.repository;
|
38 | if (!r) return cb(new Error('no repository'));
|
39 |
|
40 |
|
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 |
|
49 | function callRegistry (n, cb) {
|
50 | registry.get(n + "/latest", 3600, function (er, d) {
|
51 | if (er) return cb(er)
|
52 | getUrlAndOpen(d, cb)
|
53 | })
|
54 | }
|