git-host.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict'
  2. var gitHosts = require('./git-host-info.js')
  3. var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation, opts) {
  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. gitHostInfo.opts = opts || {}
  15. }
  16. GitHost.prototype = {}
  17. GitHost.prototype.hash = function () {
  18. return this.committish ? '#' + this.committish : ''
  19. }
  20. GitHost.prototype._fill = function (template, opts) {
  21. if (!template) return
  22. var vars = Object.assign({}, opts)
  23. vars.path = vars.path ? vars.path.replace(/^[/]+/g, '') : ''
  24. opts = Object.assign({}, this.opts, opts)
  25. var self = this
  26. Object.keys(this).forEach(function (key) {
  27. if (self[key] != null && vars[key] == null) vars[key] = self[key]
  28. })
  29. var rawAuth = vars.auth
  30. var rawComittish = vars.committish
  31. var rawFragment = vars.fragment
  32. var rawPath = vars.path
  33. Object.keys(vars).forEach(function (key) {
  34. vars[key] = encodeURIComponent(vars[key])
  35. })
  36. vars['auth@'] = rawAuth ? rawAuth + '@' : ''
  37. vars['#fragment'] = rawFragment ? '#' + this.hashformat(rawFragment) : ''
  38. vars.fragment = vars.fragment ? vars.fragment : ''
  39. vars['#path'] = rawPath ? '#' + this.hashformat(rawPath) : ''
  40. vars['/path'] = vars.path ? '/' + vars.path : ''
  41. if (opts.noCommittish) {
  42. vars['#committish'] = ''
  43. vars['/tree/committish'] = ''
  44. vars['/comittish'] = ''
  45. vars.comittish = ''
  46. } else {
  47. vars['#committish'] = rawComittish ? '#' + rawComittish : ''
  48. vars['/tree/committish'] = vars.committish
  49. ? '/' + vars.treepath + '/' + vars.committish
  50. : ''
  51. vars['/committish'] = vars.committish ? '/' + vars.committish : ''
  52. vars.committish = vars.committish || 'master'
  53. }
  54. var res = template
  55. Object.keys(vars).forEach(function (key) {
  56. res = res.replace(new RegExp('[{]' + key + '[}]', 'g'), vars[key])
  57. })
  58. if (opts.noGitPlus) {
  59. return res.replace(/^git[+]/, '')
  60. } else {
  61. return res
  62. }
  63. }
  64. GitHost.prototype.ssh = function (opts) {
  65. return this._fill(this.sshtemplate, opts)
  66. }
  67. GitHost.prototype.sshurl = function (opts) {
  68. return this._fill(this.sshurltemplate, opts)
  69. }
  70. GitHost.prototype.browse = function (P, F, opts) {
  71. if (typeof P === 'string') {
  72. if (typeof F !== 'string') {
  73. opts = F
  74. F = null
  75. }
  76. return this._fill(this.browsefiletemplate, Object.assign({
  77. fragment: F,
  78. path: P
  79. }, opts))
  80. } else {
  81. return this._fill(this.browsetemplate, P)
  82. }
  83. }
  84. GitHost.prototype.docs = function (opts) {
  85. return this._fill(this.docstemplate, opts)
  86. }
  87. GitHost.prototype.bugs = function (opts) {
  88. return this._fill(this.bugstemplate, opts)
  89. }
  90. GitHost.prototype.https = function (opts) {
  91. return this._fill(this.httpstemplate, opts)
  92. }
  93. GitHost.prototype.git = function (opts) {
  94. return this._fill(this.gittemplate, opts)
  95. }
  96. GitHost.prototype.shortcut = function (opts) {
  97. return this._fill(this.shortcuttemplate, opts)
  98. }
  99. GitHost.prototype.path = function (opts) {
  100. return this._fill(this.pathtemplate, opts)
  101. }
  102. GitHost.prototype.tarball = function (opts) {
  103. return this._fill(this.tarballtemplate, opts)
  104. }
  105. GitHost.prototype.file = function (P, opts) {
  106. return this._fill(this.filetemplate, Object.assign({ path: P }, opts))
  107. }
  108. GitHost.prototype.getDefaultRepresentation = function () {
  109. return this.default
  110. }
  111. GitHost.prototype.toString = function (opts) {
  112. return (this[this.default] || this.sshurl).call(this, opts)
  113. }