git-host.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict'
  2. var gitHosts = require('./git-host-info.js')
  3. var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation) {
  4. var gitHostInfo = this
  5. gitHostInfo.type = type
  6. Object.keys(gitHosts[type]).forEach(function (key) {
  7. gitHostInfo[key] = gitHosts[type][key]
  8. })
  9. gitHostInfo.user = user
  10. gitHostInfo.auth = auth
  11. gitHostInfo.project = project
  12. gitHostInfo.committish = committish
  13. gitHostInfo.default = defaultRepresentation
  14. }
  15. GitHost.prototype = {}
  16. GitHost.prototype.hash = function () {
  17. return this.committish ? '#' + this.committish : ''
  18. }
  19. GitHost.prototype._fill = function (template, vars) {
  20. if (!template) return
  21. if (!vars) vars = {}
  22. var self = this
  23. Object.keys(this).forEach(function (key) {
  24. if (self[key] != null && vars[key] == null) vars[key] = self[key]
  25. })
  26. var rawAuth = vars.auth
  27. var rawComittish = vars.committish
  28. Object.keys(vars).forEach(function (key) {
  29. vars[key] = encodeURIComponent(vars[key])
  30. })
  31. vars['auth@'] = rawAuth ? rawAuth + '@' : ''
  32. vars['#committish'] = rawComittish ? '#' + rawComittish : ''
  33. vars['/tree/committish'] = vars.committish
  34. ? '/' + vars.treepath + '/' + vars.committish
  35. : ''
  36. vars['/committish'] = vars.committish ? '/' + vars.committish : ''
  37. vars.committish = vars.committish || 'master'
  38. var res = template
  39. Object.keys(vars).forEach(function (key) {
  40. res = res.replace(new RegExp('[{]' + key + '[}]', 'g'), vars[key])
  41. })
  42. return res
  43. }
  44. GitHost.prototype.ssh = function () {
  45. return this._fill(this.sshtemplate)
  46. }
  47. GitHost.prototype.sshurl = function () {
  48. return this._fill(this.sshurltemplate)
  49. }
  50. GitHost.prototype.browse = function () {
  51. return this._fill(this.browsetemplate)
  52. }
  53. GitHost.prototype.docs = function () {
  54. return this._fill(this.docstemplate)
  55. }
  56. GitHost.prototype.bugs = function () {
  57. return this._fill(this.bugstemplate)
  58. }
  59. GitHost.prototype.https = function () {
  60. return this._fill(this.httpstemplate)
  61. }
  62. GitHost.prototype.git = function () {
  63. return this._fill(this.gittemplate)
  64. }
  65. GitHost.prototype.shortcut = function () {
  66. return this._fill(this.shortcuttemplate)
  67. }
  68. GitHost.prototype.path = function () {
  69. return this._fill(this.pathtemplate)
  70. }
  71. GitHost.prototype.file = function (P) {
  72. return this._fill(this.filetemplate, {
  73. path: P.replace(/^[/]+/g, '')
  74. })
  75. }
  76. GitHost.prototype.getDefaultRepresentation = function () {
  77. return this.default
  78. }
  79. GitHost.prototype.toString = function () {
  80. return (this[this.default] || this.sshurl).call(this)
  81. }