repo.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. module.exports = repo
  2. repo.usage = 'npm repo [<pkg>]'
  3. const openUrl = require('./utils/open-url')
  4. const hostedGitInfo = require('hosted-git-info')
  5. const url_ = require('url')
  6. const fetchPackageMetadata = require('./fetch-package-metadata.js')
  7. repo.completion = function (opts, cb) {
  8. // FIXME: there used to be registry completion here, but it stopped making
  9. // sense somewhere around 50,000 packages on the registry
  10. cb()
  11. }
  12. function repo (args, cb) {
  13. const n = args.length ? args[0] : '.'
  14. fetchPackageMetadata(n, '.', {fullMetadata: true}, function (er, d) {
  15. if (er) return cb(er)
  16. getUrlAndOpen(d, cb)
  17. })
  18. }
  19. function getUrlAndOpen (d, cb) {
  20. const r = d.repository
  21. if (!r) return cb(new Error('no repository'))
  22. // XXX remove this when npm@v1.3.10 from node 0.10 is deprecated
  23. // from https://github.com/npm/npm-www/issues/418
  24. const info = hostedGitInfo.fromUrl(r.url)
  25. const url = info ? info.browse() : unknownHostedUrl(r.url)
  26. if (!url) return cb(new Error('no repository: could not get url'))
  27. openUrl(url, 'repository available at the following URL', cb)
  28. }
  29. function unknownHostedUrl (url) {
  30. try {
  31. const idx = url.indexOf('@')
  32. if (idx !== -1) {
  33. url = url.slice(idx + 1).replace(/:([^\d]+)/, '/$1')
  34. }
  35. url = url_.parse(url)
  36. const protocol = url.protocol === 'https:'
  37. ? 'https:'
  38. : 'http:'
  39. return protocol + '//' + (url.host || '') +
  40. url.path.replace(/\.git$/, '')
  41. } catch (e) {}
  42. }