1 | module.exports = repo
|
2 |
|
3 | repo.usage = 'npm repo [<pkg>]'
|
4 |
|
5 | var openUrl = require('./utils/open-url')
|
6 | var hostedGitInfo = require('hosted-git-info')
|
7 | var url_ = require('url')
|
8 | var fetchPackageMetadata = require('./fetch-package-metadata.js')
|
9 |
|
10 | repo.completion = function (opts, cb) {
|
11 |
|
12 |
|
13 | cb()
|
14 | }
|
15 |
|
16 | function repo (args, cb) {
|
17 | var n = args.length ? args[0] : '.'
|
18 | fetchPackageMetadata(n, '.', {fullMetadata: true}, function (er, d) {
|
19 | if (er) return cb(er)
|
20 | getUrlAndOpen(d, cb)
|
21 | })
|
22 | }
|
23 |
|
24 | function getUrlAndOpen (d, cb) {
|
25 | var r = d.repository
|
26 | if (!r) return cb(new Error('no repository'))
|
27 |
|
28 |
|
29 | var info = hostedGitInfo.fromUrl(r.url)
|
30 | var url = info ? info.browse() : unknownHostedUrl(r.url)
|
31 |
|
32 | if (!url) return cb(new Error('no repository: could not get url'))
|
33 |
|
34 | openUrl(url, 'repository available at the following URL', cb)
|
35 | }
|
36 |
|
37 | function unknownHostedUrl (url) {
|
38 | try {
|
39 | var idx = url.indexOf('@')
|
40 | if (idx !== -1) {
|
41 | url = url.slice(idx + 1).replace(/:([^\d]+)/, '/$1')
|
42 | }
|
43 | url = url_.parse(url)
|
44 | var protocol = url.protocol === 'https:'
|
45 | ? 'https:'
|
46 | : 'http:'
|
47 | return protocol + '//' + (url.host || '') +
|
48 | url.path.replace(/\.git$/, '')
|
49 | } catch (e) {}
|
50 | }
|