git-host.js 3.7 KB

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