/deps/npm/node_modules/hosted-git-info/git-host.js

https://gitlab.com/MichelZuniga/node · JavaScript · 96 lines · 81 code · 15 blank · 0 comment · 8 complexity · 32603e306e51c3a15b38c2748faf7c5b MD5 · raw file

  1. 'use strict'
  2. var gitHosts = require('./git-host-info.js')
  3. var GitHost = module.exports = function (type, user, auth, project, comittish, 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.comittish = comittish
  13. gitHostInfo.default = defaultRepresentation
  14. }
  15. GitHost.prototype = {}
  16. GitHost.prototype.hash = function () {
  17. return this.comittish ? '#' + this.comittish : ''
  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.comittish
  28. Object.keys(vars).forEach(function (key) {
  29. vars[key] = encodeURIComponent(vars[key])
  30. })
  31. vars['auth@'] = rawAuth ? rawAuth + '@' : ''
  32. vars['#comittish'] = rawComittish ? '#' + rawComittish : ''
  33. vars['/tree/comittish'] = vars.comittish
  34. ? '/' + vars.treepath + '/' + vars.comittish
  35. : ''
  36. vars['/comittish'] = vars.comittish ? '/' + vars.comittish : ''
  37. vars.comittish = vars.comittish || '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. }