1module.exports = repo 2 3repo.usage = 'npm repo [<pkg>]' 4 5const openUrl = require('./utils/open-url') 6const hostedGitInfo = require('hosted-git-info') 7const url_ = require('url') 8const fetchPackageMetadata = require('./fetch-package-metadata.js') 9 10repo.completion = function (opts, cb) { 11 // FIXME: there used to be registry completion here, but it stopped making 12 // sense somewhere around 50,000 packages on the registry 13 cb() 14} 15 16function repo (args, cb) { 17 const 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 24function getUrlAndOpen (d, cb) { 25 const r = d.repository 26 if (!r) return cb(new Error('no repository')) 27 // XXX remove this when npm@v1.3.10 from node 0.10 is deprecated 28 // from https://github.com/npm/npm-www/issues/418 29 const info = hostedGitInfo.fromUrl(r.url) 30 const 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 37function unknownHostedUrl (url) { 38 try { 39 const idx = url.indexOf('@') 40 if (idx !== -1) { 41 url = url.slice(idx + 1).replace(/:([^\d]+)/, '/$1') 42 } 43 url = url_.parse(url) 44 const protocol = url.protocol === 'https:' 45 ? 'https:' 46 : 'http:' 47 return protocol + '//' + (url.host || '') + 48 url.path.replace(/\.git$/, '') 49 } catch (e) {} 50} 51