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