diff options
author | Evan Lucas <evanlucas@me.com> | 2014-02-14 21:15:04 +0400 |
---|---|---|
committer | Domenic Denicola <domenic@domenicdenicola.com> | 2014-05-12 01:54:01 +0400 |
commit | c46a13c008b603bc33a9b269363eecc909e78a17 (patch) | |
tree | 1534963d690c2199ae04fcbd4967efaa13e7ee11 | |
parent | 48d8353ac8ee8270019ce1ee34336c74a38cc42e (diff) |
Allow non-github repositories with `npm repo`
-rw-r--r-- | lib/repo.js | 23 | ||||
-rw-r--r-- | test/tap/repo.js | 115 |
2 files changed, 123 insertions, 15 deletions
diff --git a/lib/repo.js b/lib/repo.js index b95bd7562..a2ec99bb0 100644 --- a/lib/repo.js +++ b/lib/repo.js @@ -19,6 +19,7 @@ var npm = require("./npm.js") , path = require("path") , readJson = require("read-package-json") , fs = require("fs") + , url_ = require('url') function repo (args, cb) { var n = args.length && args[0].split("@").shift() || '.' @@ -40,7 +41,11 @@ function getUrlAndOpen (d, cb) { // from https://github.com/npm/npm-www/issues/418 if (githubUserRepo(r.url)) r.url = githubUserRepo(r.url) - var url = github(r.url) + + var url = (r.url && ~r.url.indexOf('github')) + ? github(r.url) + : nonGithubUrl(r.url) + if (!url) return cb(new Error('no repository: could not get url')) opener(url, { command: npm.config.get("browser") }, cb) @@ -52,3 +57,19 @@ function callRegistry (n, cb) { getUrlAndOpen(d, cb) }) } + +function nonGithubUrl (url) { + try { + var idx = url.indexOf('@') + if (idx !== -1) { + url = url.slice(idx+1).replace(/:([^\d]+)/, '/$1') + } + url = url_.parse(url) + var protocol = url.protocol === 'https:' + ? 'https:' + : 'http:' + return protocol + '//' + (url.host || '') + + url.path.replace(/\.git$/, '') + } + catch(e) {} +} diff --git a/test/tap/repo.js b/test/tap/repo.js index 44671e7d5..bf7b574f8 100644 --- a/test/tap/repo.js +++ b/test/tap/repo.js @@ -12,6 +12,10 @@ var node = process.execPath var rimraf = require("rimraf") var spawn = require("child_process").spawn var fs = require("fs") +var path = require('path') +var outFile = path.join(__dirname, '/_output') + +var opts = { cwd: __dirname } test("setup", function (t) { var s = "#!/usr/bin/env bash\n" + @@ -24,21 +28,105 @@ test("setup", function (t) { test("npm repo underscore", function (t) { mr(common.port, function (s) { - var c = spawn(node, [ - npm, "repo", "underscore", - "--registry=" + common.registry, - "--loglevel=silent", - "--browser=" + __dirname + "/_script.sh", - ]) - c.stdout.on("data", function(d) { - t.fail("Should not get data on stdout: " + d) - }) - c.stderr.pipe(process.stderr) - c.on("close", function(code) { - t.equal(code, 0, "exit ok") - var res = fs.readFileSync(__dirname + "/_output", "ascii") + common.npm([ + 'repo', 'underscore', + '--registry=' + common.registry, + '--loglevel=silent', + '--browser=' + __dirname + '/_script.sh' + ], opts, function(err, code, stdout, stderr) { + t.equal(code, 0, 'exit ok') + var res = fs.readFileSync(outFile, 'ascii') s.close() t.equal(res, "https://github.com/jashkenas/underscore\n") + rimraf.sync(outFile) + t.end() + }) + }) +}) + + +test('npm repo optimist - github (https://)', function (t) { + mr(common.port, function (s) { + common.npm([ + 'repo', 'optimist', + '--registry=' + common.registry, + '--loglevel=silent', + '--browser=' + __dirname + '/_script.sh' + ], opts, function(err, code, stdout, stderr) { + t.equal(code, 0, 'exit ok') + var res = fs.readFileSync(outFile, 'ascii') + s.close() + t.equal(res, "https://github.com/substack/node-optimist\n") + rimraf.sync(outFile) + t.end() + }) + }) +}) + +test("npm repo npm-test-peer-deps - no repo", function (t) { + mr(common.port, function (s) { + common.npm([ + 'repo', 'npm-test-peer-deps', + '--registry=' + common.registry, + '--loglevel=silent', + '--browser=' + __dirname + '/_script.sh' + ], opts, function(err, code, stdout, stderr) { + t.equal(code, 1, 'exit not ok') + s.close() + t.end() + }) + }) +}) + +test("npm repo test-repo-url-http - non-github (http://)", function (t) { + mr(common.port, function (s) { + common.npm([ + 'repo', 'test-repo-url-http', + '--registry=' + common.registry, + '--loglevel=silent', + '--browser=' + __dirname + '/_script.sh' + ], opts, function(err, code, stdout, stderr) { + t.equal(code, 0, 'exit ok') + var res = fs.readFileSync(outFile, 'ascii') + s.close() + t.equal(res, "http://gitlab.com/evanlucas/test-repo-url-http\n") + rimraf.sync(outFile) + t.end() + }) + }) +}) + +test("npm repo test-repo-url-https - non-github (https://)", function (t) { + mr(common.port, function (s) { + common.npm([ + 'repo', 'test-repo-url-https', + '--registry=' + common.registry, + '--loglevel=silent', + '--browser=' + __dirname + '/_script.sh' + ], opts, function(err, code, stdout, stderr) { + t.equal(code, 0, 'exit ok') + var res = fs.readFileSync(outFile, 'ascii') + s.close() + t.equal(res, "https://gitlab.com/evanlucas/test-repo-url-https\n") + rimraf.sync(outFile) + t.end() + }) + }) +}) + +test("npm repo test-repo-url-ssh - non-github (ssh://)", function (t) { + mr(common.port, function (s) { + common.npm([ + 'repo', 'test-repo-url-ssh', + '--registry=' + common.registry, + '--loglevel=silent', + '--browser=' + __dirname + '/_script.sh' + ], opts, function(err, code, stdout, stderr) { + t.equal(code, 0, 'exit ok') + var res = fs.readFileSync(outFile, 'ascii') + s.close() + t.equal(res, "http://gitlab.com/evanlucas/test-repo-url-ssh\n") + rimraf.sync(outFile) t.end() }) }) @@ -46,7 +134,6 @@ test("npm repo underscore", function (t) { test("cleanup", function (t) { fs.unlinkSync(__dirname + "/_script.sh") - fs.unlinkSync(__dirname + "/_output") t.pass("cleaned up") t.end() }) |