Skip to content

Commit

Permalink
Add web-opener (issues npm#2687)
Browse files Browse the repository at this point in the history
  • Loading branch information
bogushevich committed Aug 21, 2012
1 parent 3218386 commit 7667143
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 56 deletions.
24 changes: 2 additions & 22 deletions lib/bugs.js
Expand Up @@ -7,6 +7,7 @@ var exec = require("./utils/exec.js")
, npm = require("./npm.js")
, registry = npm.registry
, log = require("npmlog")
, open = require("./utils/web-opener.js")

bugs.completion = function (opts, cb) {
if (opts.conf.argv.remain.length > 2) return cb()
Expand Down Expand Up @@ -38,25 +39,4 @@ function bugs (args, cb) {
}
return open("http://search.npmjs.org/#/" + d.name, 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()
}
}
24 changes: 2 additions & 22 deletions lib/docs.js
Expand Up @@ -14,6 +14,7 @@ var exec = require("./utils/exec.js")
, npm = require("./npm.js")
, registry = npm.registry
, log = require("npmlog")
, open = require("./utils/web-opener.js")

function docs (args, cb) {
if (!args.length) return cb(docs.usage)
Expand All @@ -34,25 +35,4 @@ function docs (args, cb) {
}
return open("http://search.npmjs.org/#/" + d.name, 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()
}
}
14 changes: 2 additions & 12 deletions lib/help.js
Expand Up @@ -13,6 +13,7 @@ var fs = require("graceful-fs")
, exec = require("./utils/exec.js")
, npm = require("./npm.js")
, log = require("npmlog")
, open = require("./utils/web-opener.js")

function help (args, cb) {
var num = 1
Expand Down Expand Up @@ -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 () {})
}
open(htmlPath, cb)
break

default:
Expand Down
50 changes: 50 additions & 0 deletions lib/utils/web-opener.js
@@ -0,0 +1,50 @@
module.exports = open

var exec = require("./exec.js")
, npm = require("../npm.js")
, log = require("npmlog")

function open (url, cb) {
var browser = npm.config.get("browser")
, cmd
, args

if (!browser) {
var er = new Error(["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)
}

if (process.platform === "win32" && browser === "start") {
cmd = "cmd"
/*
Windows command "start" takes the first argument in quotes
as a window title, but not as a file or command.
For example,
start help.html
open browser with help.html,
start "C:\Program files\help.html"
open empty command prompt with title "C:\Program files\help.html",
start "" "C:\Program files\help.html"
open browser with help.html
*/
args = ["/c", "start", "\"\"", url]
} else {
cmd = browser
args = [url]
}

log.info("Go to", url)
log.http("Opening browser...")

exec(cmd, args, process.env, false, function (err) {
if (err) {
log.error("browser", err)
}
})
cb()
}

0 comments on commit 7667143

Please sign in to comment.