hosted-git-info
Advanced tools
Comparing version 2.3.1 to 2.4.0
@@ -66,4 +66,4 @@ 'use strict' | ||
gitHosts[name].protocols.map(function (protocol) { | ||
return protocol.replace(/([\\+*{}()\[\]$^|])/g, '\\$1') | ||
return protocol.replace(/([\\+*{}()[\]$^|])/g, '\\$1') | ||
}).join('|') + '):$') | ||
}) |
'use strict' | ||
var gitHosts = require('./git-host-info.js') | ||
var extend = Object.assign || require('util')._extend | ||
var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation) { | ||
var GitHost = module.exports = function (type, user, auth, project, committish, defaultRepresentation, opts) { | ||
var gitHostInfo = this | ||
@@ -15,2 +16,3 @@ gitHostInfo.type = type | ||
gitHostInfo.default = defaultRepresentation | ||
gitHostInfo.opts = opts || {} | ||
} | ||
@@ -23,5 +25,6 @@ GitHost.prototype = {} | ||
GitHost.prototype._fill = function (template, vars) { | ||
GitHost.prototype._fill = function (template, opts) { | ||
if (!template) return | ||
if (!vars) vars = {} | ||
var vars = extend({}, opts) | ||
opts = extend(extend({}, this.opts), opts) | ||
var self = this | ||
@@ -37,8 +40,15 @@ Object.keys(this).forEach(function (key) { | ||
vars['auth@'] = rawAuth ? rawAuth + '@' : '' | ||
vars['#committish'] = rawComittish ? '#' + rawComittish : '' | ||
vars['/tree/committish'] = vars.committish | ||
? '/' + vars.treepath + '/' + vars.committish | ||
: '' | ||
vars['/committish'] = vars.committish ? '/' + vars.committish : '' | ||
vars.committish = vars.committish || 'master' | ||
if (opts.noCommittish) { | ||
vars['#committish'] = '' | ||
vars['/tree/committish'] = '' | ||
vars['/comittish'] = '' | ||
vars.comittish = '' | ||
} else { | ||
vars['#committish'] = rawComittish ? '#' + rawComittish : '' | ||
vars['/tree/committish'] = vars.committish | ||
? '/' + vars.treepath + '/' + vars.committish | ||
: '' | ||
vars['/committish'] = vars.committish ? '/' + vars.committish : '' | ||
vars.committish = vars.committish || 'master' | ||
} | ||
var res = template | ||
@@ -48,49 +58,53 @@ Object.keys(vars).forEach(function (key) { | ||
}) | ||
return res | ||
if (opts.noGitPlus) { | ||
return res.replace(/^git[+]/, '') | ||
} else { | ||
return res | ||
} | ||
} | ||
GitHost.prototype.ssh = function () { | ||
return this._fill(this.sshtemplate) | ||
GitHost.prototype.ssh = function (opts) { | ||
return this._fill(this.sshtemplate, opts) | ||
} | ||
GitHost.prototype.sshurl = function () { | ||
return this._fill(this.sshurltemplate) | ||
GitHost.prototype.sshurl = function (opts) { | ||
return this._fill(this.sshurltemplate, opts) | ||
} | ||
GitHost.prototype.browse = function () { | ||
return this._fill(this.browsetemplate) | ||
GitHost.prototype.browse = function (opts) { | ||
return this._fill(this.browsetemplate, opts) | ||
} | ||
GitHost.prototype.docs = function () { | ||
return this._fill(this.docstemplate) | ||
GitHost.prototype.docs = function (opts) { | ||
return this._fill(this.docstemplate, opts) | ||
} | ||
GitHost.prototype.bugs = function () { | ||
return this._fill(this.bugstemplate) | ||
GitHost.prototype.bugs = function (opts) { | ||
return this._fill(this.bugstemplate, opts) | ||
} | ||
GitHost.prototype.https = function () { | ||
return this._fill(this.httpstemplate) | ||
GitHost.prototype.https = function (opts) { | ||
return this._fill(this.httpstemplate, opts) | ||
} | ||
GitHost.prototype.git = function () { | ||
return this._fill(this.gittemplate) | ||
GitHost.prototype.git = function (opts) { | ||
return this._fill(this.gittemplate, opts) | ||
} | ||
GitHost.prototype.shortcut = function () { | ||
return this._fill(this.shortcuttemplate) | ||
GitHost.prototype.shortcut = function (opts) { | ||
return this._fill(this.shortcuttemplate, opts) | ||
} | ||
GitHost.prototype.path = function () { | ||
return this._fill(this.pathtemplate) | ||
GitHost.prototype.path = function (opts) { | ||
return this._fill(this.pathtemplate, opts) | ||
} | ||
GitHost.prototype.tarball = function () { | ||
return this._fill(this.tarballtemplate) | ||
GitHost.prototype.tarball = function (opts) { | ||
return this._fill(this.tarballtemplate, opts) | ||
} | ||
GitHost.prototype.file = function (P) { | ||
return this._fill(this.filetemplate, { | ||
GitHost.prototype.file = function (P, opts) { | ||
return this._fill(this.filetemplate, extend({ | ||
path: P.replace(/^[/]+/g, '') | ||
}) | ||
}, opts)) | ||
} | ||
@@ -102,4 +116,4 @@ | ||
GitHost.prototype.toString = function () { | ||
return (this[this.default] || this.sshurl).call(this) | ||
GitHost.prototype.toString = function (opts) { | ||
return (this[this.default] || this.sshurl).call(this, opts) | ||
} |
@@ -26,3 +26,3 @@ 'use strict' | ||
module.exports.fromUrl = function (giturl) { | ||
module.exports.fromUrl = function (giturl, opts) { | ||
if (giturl == null || giturl === '') return | ||
@@ -60,3 +60,3 @@ var url = fixupUnqualifiedGist( | ||
} | ||
return new GitHost(gitHostName, user, auth, project, committish, defaultRepresentation) | ||
return new GitHost(gitHostName, user, auth, project, committish, defaultRepresentation, opts) | ||
} catch (ex) { | ||
@@ -94,3 +94,3 @@ if (!(ex instanceof URIError)) throw ex | ||
if (typeof giturl !== 'string') giturl = '' + giturl | ||
var matched = giturl.match(/^([^@]+)@([^:]+):[/]?((?:[^/]+[/])?[^/]+?)(?:[.]git)?(#.*)?$/) | ||
var matched = giturl.match(/^([^@]+)@([^:/]+):[/]?((?:[^/]+[/])?[^/]+?)(?:[.]git)?(#.*)?$/) | ||
if (!matched) return url.parse(giturl) | ||
@@ -97,0 +97,0 @@ return { |
{ | ||
"name": "hosted-git-info", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"description": "Provides metadata and conversions from repository urls for Github, Bitbucket and Gitlab", | ||
@@ -23,7 +23,7 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "standard && tap -j8 test/*.js" | ||
"test": "standard && tap -J --coverage test/*.js" | ||
}, | ||
"devDependencies": { | ||
"standard": "^3.3.2", | ||
"tap": "^10.0.2" | ||
"standard": "^9.0.2", | ||
"tap": "^10.3.0" | ||
}, | ||
@@ -30,0 +30,0 @@ "files": [ |
@@ -7,7 +7,7 @@ # hosted-git-info | ||
## Usage | ||
## Example | ||
```javascript | ||
var hostedGitInfo = require("hosted-git-info") | ||
var info = hostedGitInfo.fromUrl("git@github.com:npm/hosted-git-info.git") | ||
var info = hostedGitInfo.fromUrl("git@github.com:npm/hosted-git-info.git", opts) | ||
/* info looks like: | ||
@@ -42,3 +42,3 @@ { | ||
* A method is removed. | ||
* A method no longer accepts the number and type of arguments it previously accepted. | ||
* A method can no longer accept the number and type of arguments it previously accepted. | ||
* A method can return a different type than it currently returns. | ||
@@ -55,6 +55,18 @@ | ||
## Usage | ||
### var info = hostedGitInfo.fromUrl(gitSpecifier[, options]) | ||
* *gitSpecifer* is a URL of a git repository or a SCP-style specifier of one. | ||
* *options* is an optional object. It can have the following properties: | ||
* *noCommittish* — If true then committishes won't be included in generated URLs. | ||
* *noGitPlus* — If true then `git+` won't be prefixed on URLs. | ||
## Methods | ||
* info.file(path) | ||
All of the methods take the same options as the `fromUrl` factory. Options | ||
provided to a method override those provided to the constructor. | ||
* info.file(path, opts) | ||
Given the path of a file relative to the repository, returns a URL for | ||
@@ -67,35 +79,35 @@ directly fetching it from the githost. If no committish was set then | ||
* info.shortcut() | ||
* info.shortcut(opts) | ||
eg, `github:npm/hosted-git-info` | ||
* info.browse() | ||
* info.browse(opts) | ||
eg, `https://github.com/npm/hosted-git-info/tree/v1.2.0` | ||
* info.bugs() | ||
* info.bugs(opts) | ||
eg, `https://github.com/npm/hosted-git-info/issues` | ||
* info.docs() | ||
* info.docs(opts) | ||
eg, `https://github.com/npm/hosted-git-info/tree/v1.2.0#readme` | ||
* info.https() | ||
* info.https(opts) | ||
eg, `git+https://github.com/npm/hosted-git-info.git` | ||
* info.sshurl() | ||
* info.sshurl(opts) | ||
eg, `git+ssh://git@github.com/npm/hosted-git-info.git` | ||
* info.ssh() | ||
* info.ssh(opts) | ||
eg, `git@github.com:npm/hosted-git-info.git` | ||
* info.path() | ||
* info.path(opts) | ||
eg, `npm/hosted-git-info` | ||
* info.tarball() | ||
* info.tarball(opts) | ||
@@ -109,3 +121,3 @@ eg, `https://github.com/npm/hosted-git-info/archive/v1.2.0.tar.gz` | ||
* info.toString() | ||
* info.toString(opts) | ||
@@ -121,3 +133,2 @@ Uses the getDefaultRepresentation to call one of the other methods to get a URL for | ||
## Supported hosts | ||
@@ -124,0 +135,0 @@ |
19305
7
265
133