1 | 'use strict'
|
---|
2 | const gitHosts = require('./git-host-info.js')
|
---|
3 |
|
---|
4 | class GitHost {
|
---|
5 | constructor (type, user, auth, project, committish, defaultRepresentation, opts = {}) {
|
---|
6 | Object.assign(this, gitHosts[type])
|
---|
7 | this.type = type
|
---|
8 | this.user = user
|
---|
9 | this.auth = auth
|
---|
10 | this.project = project
|
---|
11 | this.committish = committish
|
---|
12 | this.default = defaultRepresentation
|
---|
13 | this.opts = opts
|
---|
14 | }
|
---|
15 |
|
---|
16 | hash () {
|
---|
17 | return this.committish ? `#${this.committish}` : ''
|
---|
18 | }
|
---|
19 |
|
---|
20 | ssh (opts) {
|
---|
21 | return this._fill(this.sshtemplate, opts)
|
---|
22 | }
|
---|
23 |
|
---|
24 | _fill (template, opts) {
|
---|
25 | if (typeof template === 'function') {
|
---|
26 | const options = { ...this, ...this.opts, ...opts }
|
---|
27 |
|
---|
28 | // the path should always be set so we don't end up with 'undefined' in urls
|
---|
29 | if (!options.path) {
|
---|
30 | options.path = ''
|
---|
31 | }
|
---|
32 |
|
---|
33 | // template functions will insert the leading slash themselves
|
---|
34 | if (options.path.startsWith('/')) {
|
---|
35 | options.path = options.path.slice(1)
|
---|
36 | }
|
---|
37 |
|
---|
38 | if (options.noCommittish) {
|
---|
39 | options.committish = null
|
---|
40 | }
|
---|
41 |
|
---|
42 | const result = template(options)
|
---|
43 | return options.noGitPlus && result.startsWith('git+') ? result.slice(4) : result
|
---|
44 | }
|
---|
45 |
|
---|
46 | return null
|
---|
47 | }
|
---|
48 |
|
---|
49 | sshurl (opts) {
|
---|
50 | return this._fill(this.sshurltemplate, opts)
|
---|
51 | }
|
---|
52 |
|
---|
53 | browse (path, fragment, opts) {
|
---|
54 | // not a string, treat path as opts
|
---|
55 | if (typeof path !== 'string') {
|
---|
56 | return this._fill(this.browsetemplate, path)
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (typeof fragment !== 'string') {
|
---|
60 | opts = fragment
|
---|
61 | fragment = null
|
---|
62 | }
|
---|
63 | return this._fill(this.browsefiletemplate, { ...opts, fragment, path })
|
---|
64 | }
|
---|
65 |
|
---|
66 | docs (opts) {
|
---|
67 | return this._fill(this.docstemplate, opts)
|
---|
68 | }
|
---|
69 |
|
---|
70 | bugs (opts) {
|
---|
71 | return this._fill(this.bugstemplate, opts)
|
---|
72 | }
|
---|
73 |
|
---|
74 | https (opts) {
|
---|
75 | return this._fill(this.httpstemplate, opts)
|
---|
76 | }
|
---|
77 |
|
---|
78 | git (opts) {
|
---|
79 | return this._fill(this.gittemplate, opts)
|
---|
80 | }
|
---|
81 |
|
---|
82 | shortcut (opts) {
|
---|
83 | return this._fill(this.shortcuttemplate, opts)
|
---|
84 | }
|
---|
85 |
|
---|
86 | path (opts) {
|
---|
87 | return this._fill(this.pathtemplate, opts)
|
---|
88 | }
|
---|
89 |
|
---|
90 | tarball (opts) {
|
---|
91 | return this._fill(this.tarballtemplate, { ...opts, noCommittish: false })
|
---|
92 | }
|
---|
93 |
|
---|
94 | file (path, opts) {
|
---|
95 | return this._fill(this.filetemplate, { ...opts, path })
|
---|
96 | }
|
---|
97 |
|
---|
98 | getDefaultRepresentation () {
|
---|
99 | return this.default
|
---|
100 | }
|
---|
101 |
|
---|
102 | toString (opts) {
|
---|
103 | if (this.default && typeof this[this.default] === 'function') {
|
---|
104 | return this[this.default](opts)
|
---|
105 | }
|
---|
106 |
|
---|
107 | return this.sshurl(opts)
|
---|
108 | }
|
---|
109 | }
|
---|
110 | module.exports = GitHost
|
---|