Skip to content

Commit

Permalink
hosted-git-info@1.0.0 - Add service specific checks for bitbucket and…
Browse files Browse the repository at this point in the history
… gitlab
  • Loading branch information
iarna committed Oct 13, 2014
1 parent 13ee203 commit 112078b
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 16 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ fields:
* `name` - If known, the `name` field expected in the resulting pkg.
* `type` - One of the following strings:
* `git` - A git repo
* `github` - A github shorthand, like `user/project`
* `github` - A github shorthand, like `github:user/project`, or `user/project`
* `bitbucket` - A bitbucket shorthand, like `bitbucket:user/project`
* `gitlab` - A gitlab shorthand, like `gitlab:user/project`
* `tag` - A tagged version, like `"foo@latest"`
* `version` - A specific version number, like `"foo@1.2.3"`
* `range` - A version range, like `"foo@2.x"`
Expand Down
31 changes: 19 additions & 12 deletions npa.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var assert = require("assert")
var util = require("util")
var semver = require("semver")
var path = require("path")
var HostedGit = require("hosted-git-info")

module.exports = npa

Expand Down Expand Up @@ -78,9 +79,7 @@ function npa (arg) {
// parse tag/range/local/remote

if (maybeGitHubShorthand(arg)) {
res.type = "github"
res.spec = arg
return res
return parseUrl(res, "github:" + arg, urlparse)
}

// at this point, it's not a url, and not github
Expand Down Expand Up @@ -145,6 +144,18 @@ function maybeGitHubShorthand (arg) {
}

function parseUrl (res, arg, urlparse) {
var gitHost = HostedGit.fromUrl(arg)
if (gitHost) {
res.type = gitHost.type
res.spec = gitHost.path().replace(/[.]git$/,"")
res.hosted = {
ssh: gitHost.ssh(),
sshurl: gitHost.sshurl(),
https: gitHost.https(),
directUrl: gitHost.file("package.json")
}
return res
}
// check the protocol, and then see if it's git or not
switch (urlparse.protocol) {
case "git:":
Expand All @@ -169,11 +180,6 @@ function parseUrl (res, arg, urlparse) {
res.spec = urlparse.pathname
break

case "github:":
res.type = "github"
res.spec = arg.replace(/^github:/, "")
break

default:
throw new Error("Unsupported URL Type: " + arg)
break
Expand All @@ -186,7 +192,8 @@ function parseUrl (res, arg, urlparse) {
function Result () {
if (!(this instanceof Result)) return new Result
}
Result.prototype.name = null
Result.prototype.type = null
Result.prototype.spec = null
Result.prototype.raw = null
Result.prototype.name = null
Result.prototype.type = null
Result.prototype.spec = null
Result.prototype.raw = null
Result.prototype.hosted = null
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test": "test"
},
"dependencies": {
"hosted-git-info": "^1.0.0",
"semver": "4"
},
"devDependencies": {
Expand Down
74 changes: 74 additions & 0 deletions test/bitbucket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var npa = require("../npa.js")
var path = require("path")

require("tap").test("basic", function (t) {
t.setMaxListeners(999)

var tests = {
"bitbucket:user/foo-js": {
name: null,
type: "bitbucket",
spec: "user/foo-js",
raw: "bitbucket:user/foo-js"
},

"bitbucket:user/foo-js#bar/baz": {
name: null,
type: "bitbucket",
spec: "user/foo-js#bar/baz",
raw: "bitbucket:user/foo-js#bar/baz"
},

"bitbucket:user..blerg--/..foo-js# . . . . . some . tags / / /": {
name: null,
type: "bitbucket",
spec: "user..blerg--/..foo-js# . . . . . some . tags / / /",
raw: "bitbucket:user..blerg--/..foo-js# . . . . . some . tags / / /"
},

"bitbucket:user/foo-js#bar/baz/bin": {
name: null,
type: "bitbucket",
spec: "user/foo-js#bar/baz/bin",
raw: "bitbucket:user/foo-js#bar/baz/bin"
},

"foo@bitbucket:user/foo-js": {
name: "foo",
type: "bitbucket",
spec: "user/foo-js",
raw: "foo@bitbucket:user/foo-js"
},

"git+ssh://git@bitbucket.org/user/foo#1.2.3": {
name: null,
type: "bitbucket",
spec: "user/foo#1.2.3",
raw: "git+ssh://git@bitbucket.org/user/foo#1.2.3"
},

"https://bitbucket.org/user/foo.git": {
name: null,
type: "bitbucket",
spec: "user/foo",
raw: "https://bitbucket.org/user/foo.git"
},

"@foo/bar@git+ssh://bitbucket.org/user/foo": {
name: "@foo/bar",
scope: "@foo",
type: "bitbucket",
spec: "user/foo",
rawSpec: "git+ssh://bitbucket.org/user/foo",
raw: "@foo/bar@git+ssh://bitbucket.org/user/foo"
}
}

Object.keys(tests).forEach(function (arg) {
var res = npa(arg)
t.type(res, "Result", arg + " is a result")
t.has(res, tests[arg], arg + " matches expectations")
})

t.end()
})
42 changes: 39 additions & 3 deletions test/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,50 @@ require("tap").test("basic", function (t) {
type: "github",
spec: "user/foo-js",
raw: "github:user/foo-js"
}
},

"git+ssh://git@github.com/user/foo#1.2.3": {
name: null,
type: "github",
spec: "user/foo#1.2.3",
raw: "git+ssh://git@github.com/user/foo#1.2.3"
},

"git://github.com/user/foo": {
name: null,
type: "github",
spec: "user/foo",
raw: "git://github.com/user/foo"
},

"https://github.com/user/foo.git": {
name: null,
type: "github",
spec: "user/foo",
raw: "https://github.com/user/foo.git"
},

"@foo/bar@git+ssh://github.com/user/foo": {
name: "@foo/bar",
scope: "@foo",
type: "github",
spec: "user/foo",
rawSpec: "git+ssh://github.com/user/foo",
raw: "@foo/bar@git+ssh://github.com/user/foo"
},

"foo@bar/foo": {
name: "foo",
type: "github",
spec: "bar/foo",
raw: "foo@bar/foo"
}
}

Object.keys(tests).forEach(function (arg) {
var res = npa(arg)
t.type(res, "Result")
t.has(res, tests[arg])
t.type(res, "Result", arg + " is a result")
t.has(res, tests[arg], arg + " matches expectations")
})

t.end()
Expand Down
74 changes: 74 additions & 0 deletions test/gitlab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var npa = require("../npa.js")
var path = require("path")

require("tap").test("basic", function (t) {
t.setMaxListeners(999)

var tests = {
"gitlab:user/foo-js": {
name: null,
type: "gitlab",
spec: "user/foo-js",
raw: "gitlab:user/foo-js"
},

"gitlab:user/foo-js#bar/baz": {
name: null,
type: "gitlab",
spec: "user/foo-js#bar/baz",
raw: "gitlab:user/foo-js#bar/baz"
},

"gitlab:user..blerg--/..foo-js# . . . . . some . tags / / /": {
name: null,
type: "gitlab",
spec: "user..blerg--/..foo-js# . . . . . some . tags / / /",
raw: "gitlab:user..blerg--/..foo-js# . . . . . some . tags / / /"
},

"gitlab:user/foo-js#bar/baz/bin": {
name: null,
type: "gitlab",
spec: "user/foo-js#bar/baz/bin",
raw: "gitlab:user/foo-js#bar/baz/bin"
},

"foo@gitlab:user/foo-js": {
name: "foo",
type: "gitlab",
spec: "user/foo-js",
raw: "foo@gitlab:user/foo-js"
},

"git+ssh://git@gitlab.com/user/foo#1.2.3": {
name: null,
type: "gitlab",
spec: "user/foo#1.2.3",
raw: "git+ssh://git@gitlab.com/user/foo#1.2.3"
},

"https://gitlab.com/user/foo.git": {
name: null,
type: "gitlab",
spec: "user/foo",
raw: "https://gitlab.com/user/foo.git"
},

"@foo/bar@git+ssh://gitlab.com/user/foo": {
name: "@foo/bar",
scope: "@foo",
type: "gitlab",
spec: "user/foo",
rawSpec: "git+ssh://gitlab.com/user/foo",
raw: "@foo/bar@git+ssh://gitlab.com/user/foo"
}
}

Object.keys(tests).forEach(function (arg) {
var res = npa(arg)
t.type(res, "Result", arg + " is a result")
t.has(res, tests[arg], arg + " matches expectations")
})

t.end()
})

0 comments on commit 112078b

Please sign in to comment.