Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add web-opener (issues #2687) #2741

Closed
wants to merge 2 commits into from

1 participant

@bogushevich

Add web-opener module for open web page. #2687

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 21, 2012
  1. @bogushevich

    Add web-opener (issues #2687)

    bogushevich authored
Commits on Aug 22, 2012
  1. @bogushevich

    Update lib/bugs.js

    bogushevich authored
    Add new line at end of file
This page is out of date. Refresh to see the latest.
Showing with 55 additions and 56 deletions.
  1. +1 −22 lib/bugs.js
  2. +2 −22 lib/docs.js
  3. +2 −12 lib/help.js
  4. +50 −0 lib/utils/web-opener.js
View
23 lib/bugs.js
@@ -1,4 +1,3 @@
-
module.exports = bugs
bugs.usage = "npm bugs <pkgname>"
@@ -7,6 +6,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()
@@ -39,24 +39,3 @@ 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()
-}
View
24 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()
-}
+}
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")
+ , 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:
View
50 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()
+}
Something went wrong with that request. Please try again.