Browse files

Use opener to open web pages

  • Loading branch information...
1 parent 2c515fc commit 77c792d17a41d67d902f41b8967a629d0907b6e4 @isaacs isaacs committed Sep 12, 2012
Showing with 17 additions and 73 deletions.
  1. +11 −29 lib/bugs.js
  2. +4 −32 lib/docs.js
  3. +2 −12 lib/help.js
View
40 lib/bugs.js
@@ -7,6 +7,7 @@ var exec = require("./utils/exec.js")
, npm = require("./npm.js")
, registry = npm.registry
, log = require("npmlog")
+ , opener = require("opener")
bugs.completion = function (opts, cb) {
if (opts.conf.argv.remain.length > 2) return cb()
@@ -22,41 +23,22 @@ function bugs (args, cb) {
if (er) return cb(er)
var bugs = d.bugs
, repo = d.repository || d.repositories
+ , url
if (bugs) {
- if (typeof bugs === "string") return open(bugs, cb)
- if (bugs.url) return open(bugs.url, cb)
- }
- if (repo) {
+ url = (typeof bugs === "string") ? bugs : bugs.url
+ } else if (repo) {
if (Array.isArray(repo)) repo = repo.shift()
if (repo.hasOwnProperty("url")) repo = repo.url
log.verbose("repository", repo)
if (repo && repo.match(/^(https?:\/\/|git(:\/\/|@))github.com/)) {
- return open(repo.replace(/^git(@|:\/\/)/, "http://")
- .replace(/^https?:\/\/github.com:/, "github.com/")
- .replace(/\.git$/, '')+"/issues", cb)
+ url = repo.replace(/^git(@|:\/\/)/, "https://")
+ .replace(/^https?:\/\/github.com:/, "https://github.com/")
+ .replace(/\.git$/, '')+"/issues"
}
}
- return open("http://search.npmjs.org/#/" + d.name, cb)
+ if (!url) {
+ url = "https://npmjs.org/package/" + d.name
+ }
+ opener(url, { command: npm.config.get("browser") }, cb)
})
}
-
-function open (url, cb) {
- var args = [url]
- , browser = npm.config.get("browser")
-
- if (process.platform === "win32" && browser === "start") {
- args = [ "/c", "start" ].concat(args)
- browser = "cmd"
- }
-
- if (!browser) {
- var er = ["the 'browser' config is not set. Try doing this:"
- ," npm config set browser google-chrome"
- ,"or:"
- ," npm config set browser lynx"].join("\n")
- return cb(er)
- }
-
- exec(browser, args, process.env, false, function () {})
- cb()
-}
View
36 lib/docs.js
@@ -14,6 +14,7 @@ var exec = require("./utils/exec.js")
, npm = require("./npm.js")
, registry = npm.registry
, log = require("npmlog")
+ , opener = require("opener")
function docs (args, cb) {
if (!args.length) return cb(docs.usage)
@@ -22,37 +23,8 @@ function docs (args, cb) {
if (er) return cb(er)
var homepage = d.homepage
, repo = d.repository || d.repositories
- if (homepage) return open(homepage, cb)
- if (repo) {
- if (Array.isArray(repo)) repo = repo.shift()
- if (repo.hasOwnProperty("url")) repo = repo.url
- log.verbose("repository", repo)
- if (repo) {
- return open(repo.replace(/^git(@|:\/\/)/, 'http://')
- .replace(/\.git$/, '')+"#readme", cb)
- }
- }
- return open("http://search.npmjs.org/#/" + d.name, cb)
+ , url = homepage ? homepage
+ : "https://npmjs.org/package/" + d.name
+ opener(url, { command: npm.config.get("browser") }, cb)
})
}
-
-function open (url, cb) {
- var args = [url]
- , browser = npm.config.get("browser")
-
- if (process.platform === "win32" && browser === "start") {
- args = [ "/c", "start" ].concat(args)
- browser = "cmd"
- }
-
- if (!browser) {
- var er = ["the 'browser' config is not set. Try doing this:"
- ," npm config set browser google-chrome"
- ,"or:"
- ," npm config set browser lynx"].join("\n")
- return cb(er)
- }
-
- exec(browser, args, process.env, false, function () {})
- cb()
-}
View
14 lib/help.js
@@ -13,6 +13,7 @@ var fs = require("graceful-fs")
, exec = require("./utils/exec.js")
, npm = require("./npm.js")
, log = require("npmlog")
+ , opener = require("opener")
function help (args, cb) {
var num = 1
@@ -63,18 +64,7 @@ function help (args, cb) {
break
case "browser":
- var b = npm.config.get("browser")
- if (!b) {
- return cb(new Error("viewer=browser and no browser set."))
- }
- console.log("Opening HTML in default browser...")
- process.nextTick(cb)
- // windows is SO weird.
- if (process.platform === "win32") {
- exec("cmd", ["/c", htmlPath], env, false, function () {})
- } else {
- exec(b, [htmlPath], env, false, function () {})
- }
+ opener(htmlPath, { command: npm.config.get("browser") }, cb)
break
default:

0 comments on commit 77c792d

Please sign in to comment.