From 7667143e014d923330dde113b4064ba3c44c4d5f Mon Sep 17 00:00:00 2001 From: bogushevich Date: Wed, 22 Aug 2012 09:36:09 +1000 Subject: [PATCH] Add web-opener (issues #2687) --- lib/bugs.js | 24 ++------------------ lib/docs.js | 24 ++------------------ lib/help.js | 14 ++---------- lib/utils/web-opener.js | 50 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 56 deletions(-) create mode 100644 lib/utils/web-opener.js diff --git a/lib/bugs.js b/lib/bugs.js index 3f9de12315f..3d873838f09 100644 --- a/lib/bugs.js +++ b/lib/bugs.js @@ -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() @@ -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() -} +} \ No newline at end of file diff --git a/lib/docs.js b/lib/docs.js index 72c1869b9b6..d74ee014406 100644 --- a/lib/docs.js +++ b/lib/docs.js @@ -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) @@ -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() -} +} \ No newline at end of file diff --git a/lib/help.js b/lib/help.js index 495db8381ae..966271b0615 100644 --- a/lib/help.js +++ b/lib/help.js @@ -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 @@ -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: diff --git a/lib/utils/web-opener.js b/lib/utils/web-opener.js new file mode 100644 index 00000000000..ec65da87b20 --- /dev/null +++ b/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() +} \ No newline at end of file