Bez popisu

git-host.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict'
  2. var gitHosts = require('./git-host-info.js')
  3. var extend = Object.assign || require('util')._extend
  4. var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation, opts) {
  5. var gitHostInfo = this
  6. gitHostInfo.type = type
  7. Object.keys(gitHosts[type]).forEach(function (key) {
  8. gitHostInfo[key] = gitHosts[type][key]
  9. })
  10. gitHostInfo.user = user
  11. gitHostInfo.auth = auth
  12. gitHostInfo.project = project
  13. gitHostInfo.committish = committish
  14. gitHostInfo.default = defaultRepresentation
  15. gitHostInfo.opts = opts || {}
  16. }
  17. GitHost.prototype = {}
  18. GitHost.prototype.hash = function () {
  19. return this.committish ? '#' + this.committish : ''
  20. }
  21. GitHost.prototype._fill = function (template, opts) {
  22. if (!template) return
  23. var vars = extend({}, opts)
  24. opts = extend(extend({}, 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. Object.keys(vars).forEach(function (key) {
  32. vars[key] = encodeURIComponent(vars[key])
  33. })
  34. vars['auth@'] = rawAuth ? rawAuth + '@' : ''
  35. if (opts.noCommittish) {
  36. vars['#committish'] = ''
  37. vars['/tree/committish'] = ''
  38. vars['/comittish'] = ''
  39. vars.comittish = ''
  40. } else {
  41. vars['#committish'] = rawComittish ? '#' + rawComittish : ''
  42. vars['/tree/committish'] = vars.committish
  43. ? '/' + vars.treepath + '/' + vars.committish
  44. : ''
  45. vars['/committish'] = vars.committish ? '/' + vars.committish : ''
  46. vars.committish = vars.committish || 'master'
  47. }
  48. var res = template
  49. Object.keys(vars).forEach(function (key) {
  50. res = res.replace(new RegExp('[{]' + key + '[}]', 'g'), vars[key])
  51. })
  52. if (opts.noGitPlus) {
  53. return res.replace(/^git[+]/, '')
  54. } else {
  55. return res
  56. }
  57. }
  58. GitHost.prototype.ssh = function (opts) {
  59. return this._fill(this.sshtemplate, opts)
  60. }
  61. GitHost.prototype.sshurl = function (opts) {
  62. return this._fill(this.sshurltemplate, opts)
  63. }
  64. GitHost.prototype.browse = function (opts) {
  65. return this._fill(this.browsetemplate, opts)
  66. }
  67. GitHost.prototype.docs = function (opts) {
  68. return this._fill(this.docstemplate, opts)
  69. }
  70. GitHost.prototype.bugs = function (opts) {
  71. return this._fill(this.bugstemplate, opts)
  72. }
  73. GitHost.prototype.https = function (opts) {
  74. return this._fill(this.httpstemplate, opts)
  75. }
  76. GitHost.prototype.git = function (opts) {
  77. return this._fill(this.gittemplate, opts)
  78. }
  79. GitHost.prototype.shortcut = function (opts) {
  80. return this._fill(this.shortcuttemplate, opts)
  81. }
  82. GitHost.prototype.path = function (opts) {
  83. return this._fill(this.pathtemplate, opts)
  84. }
  85. GitHost.prototype.tarball = function (opts) {
  86. return this._fill(this.tarballtemplate, opts)
  87. }
  88. GitHost.prototype.file = function (P, opts) {
  89. return this._fill(this.filetemplate, extend({
  90. path: P.replace(/^[/]+/g, '')
  91. }, opts))
  92. }
  93. GitHost.prototype.getDefaultRepresentation = function () {
  94. return this.default
  95. }
  96. GitHost.prototype.toString = function (opts) {
  97. return (this[this.default] || this.sshurl).call(this, opts)
  98. }