git-host.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict'
  2. var gitHosts = require('./git-host-info.js')
  3. /* eslint-disable node/no-deprecated-api */
  4. // copy-pasta util._extend from node's source, to avoid pulling
  5. // the whole util module into peoples' webpack bundles.
  6. /* istanbul ignore next */
  7. var extend = Object.assign || function _extend (target, source) {
  8. // Don't do anything if source isn't an object
  9. if (source === null || typeof source !== 'object') return target
  10. var keys = Object.keys(source)
  11. var i = keys.length
  12. while (i--) {
  13. target[keys[i]] = source[keys[i]]
  14. }
  15. return target
  16. }
  17. module.exports = GitHost
  18. function GitHost (type, user, auth, project, committish, defaultRepresentation, opts) {
  19. var gitHostInfo = this
  20. gitHostInfo.type = type
  21. Object.keys(gitHosts[type]).forEach(function (key) {
  22. gitHostInfo[key] = gitHosts[type][key]
  23. })
  24. gitHostInfo.user = user
  25. gitHostInfo.auth = auth
  26. gitHostInfo.project = project
  27. gitHostInfo.committish = committish
  28. gitHostInfo.default = defaultRepresentation
  29. gitHostInfo.opts = opts || {}
  30. }
  31. GitHost.prototype.hash = function () {
  32. return this.committish ? '#' + this.committish : ''
  33. }
  34. GitHost.prototype._fill = function (template, opts) {
  35. if (!template) return
  36. var vars = extend({}, opts)
  37. vars.path = vars.path ? vars.path.replace(/^[/]+/g, '') : ''
  38. opts = extend(extend({}, this.opts), opts)
  39. var self = this
  40. Object.keys(this).forEach(function (key) {
  41. if (self[key] != null && vars[key] == null) vars[key] = self[key]
  42. })
  43. var rawAuth = vars.auth
  44. var rawcommittish = vars.committish
  45. var rawFragment = vars.fragment
  46. var rawPath = vars.path
  47. var rawProject = vars.project
  48. Object.keys(vars).forEach(function (key) {
  49. var value = vars[key]
  50. if ((key === 'path' || key === 'project') && typeof value === 'string') {
  51. vars[key] = value.split('/').map(function (pathComponent) {
  52. return encodeURIComponent(pathComponent)
  53. }).join('/')
  54. } else {
  55. vars[key] = encodeURIComponent(value)
  56. }
  57. })
  58. vars['auth@'] = rawAuth ? rawAuth + '@' : ''
  59. vars['#fragment'] = rawFragment ? '#' + this.hashformat(rawFragment) : ''
  60. vars.fragment = vars.fragment ? vars.fragment : ''
  61. vars['#path'] = rawPath ? '#' + this.hashformat(rawPath) : ''
  62. vars['/path'] = vars.path ? '/' + vars.path : ''
  63. vars.projectPath = rawProject.split('/').map(encodeURIComponent).join('/')
  64. if (opts.noCommittish) {
  65. vars['#committish'] = ''
  66. vars['/tree/committish'] = ''
  67. vars['/committish'] = ''
  68. vars.committish = ''
  69. } else {
  70. vars['#committish'] = rawcommittish ? '#' + rawcommittish : ''
  71. vars['/tree/committish'] = vars.committish
  72. ? '/' + vars.treepath + '/' + vars.committish
  73. : ''
  74. vars['/committish'] = vars.committish ? '/' + vars.committish : ''
  75. vars.committish = vars.committish || 'master'
  76. }
  77. var res = template
  78. Object.keys(vars).forEach(function (key) {
  79. res = res.replace(new RegExp('[{]' + key + '[}]', 'g'), vars[key])
  80. })
  81. if (opts.noGitPlus) {
  82. return res.replace(/^git[+]/, '')
  83. } else {
  84. return res
  85. }
  86. }
  87. GitHost.prototype.ssh = function (opts) {
  88. return this._fill(this.sshtemplate, opts)
  89. }
  90. GitHost.prototype.sshurl = function (opts) {
  91. return this._fill(this.sshurltemplate, opts)
  92. }
  93. GitHost.prototype.browse = function (P, F, opts) {
  94. if (typeof P === 'string') {
  95. if (typeof F !== 'string') {
  96. opts = F
  97. F = null
  98. }
  99. return this._fill(this.browsefiletemplate, extend({
  100. fragment: F,
  101. path: P
  102. }, opts))
  103. } else {
  104. return this._fill(this.browsetemplate, P)
  105. }
  106. }
  107. GitHost.prototype.docs = function (opts) {
  108. return this._fill(this.docstemplate, opts)
  109. }
  110. GitHost.prototype.bugs = function (opts) {
  111. return this._fill(this.bugstemplate, opts)
  112. }
  113. GitHost.prototype.https = function (opts) {
  114. return this._fill(this.httpstemplate, opts)
  115. }
  116. GitHost.prototype.git = function (opts) {
  117. return this._fill(this.gittemplate, opts)
  118. }
  119. GitHost.prototype.shortcut = function (opts) {
  120. return this._fill(this.shortcuttemplate, opts)
  121. }
  122. GitHost.prototype.path = function (opts) {
  123. return this._fill(this.pathtemplate, opts)
  124. }
  125. GitHost.prototype.tarball = function (opts_) {
  126. var opts = extend({}, opts_, { noCommittish: false })
  127. return this._fill(this.tarballtemplate, opts)
  128. }
  129. GitHost.prototype.file = function (P, opts) {
  130. return this._fill(this.filetemplate, extend({ path: P }, opts))
  131. }
  132. GitHost.prototype.getDefaultRepresentation = function () {
  133. return this.default
  134. }
  135. GitHost.prototype.toString = function (opts) {
  136. if (this.default && typeof this[this.default] === 'function') return this[this.default](opts)
  137. return this.sshurl(opts)
  138. }