summaryrefslogtreecommitdiff
path: root/deps/npm/lib/help.js
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2013-07-12 08:55:57 -0700
committerisaacs <i@izs.me>2013-07-12 08:56:26 -0700
commitff8a4058bfac4f360992cfe7adf04f910282cedc (patch)
treeb271ef198babbc9c46c33bcda9826fbcc6bf0a6d /deps/npm/lib/help.js
parent6d91bd37075a53f42bab01915fa89ba4a4f0a075 (diff)
downloadandroid-node-v8-ff8a4058bfac4f360992cfe7adf04f910282cedc.tar.gz
android-node-v8-ff8a4058bfac4f360992cfe7adf04f910282cedc.tar.bz2
android-node-v8-ff8a4058bfac4f360992cfe7adf04f910282cedc.zip
npm: Upgrade to 1.3.3
Diffstat (limited to 'deps/npm/lib/help.js')
-rw-r--r--deps/npm/lib/help.js257
1 files changed, 167 insertions, 90 deletions
diff --git a/deps/npm/lib/help.js b/deps/npm/lib/help.js
index 8faef1c2c5..f522763dbd 100644
--- a/deps/npm/lib/help.js
+++ b/deps/npm/lib/help.js
@@ -3,9 +3,7 @@ module.exports = help
help.completion = function (opts, cb) {
if (opts.conf.argv.remain.length > 2) return cb(null, [])
- var num = 1
- if (-1 !== opts.conf.argv.remain[1].indexOf("api")) num = 3
- getSections(num, cb)
+ getSections(cb)
}
var fs = require("graceful-fs")
@@ -14,94 +12,165 @@ var fs = require("graceful-fs")
, npm = require("./npm.js")
, log = require("npmlog")
, opener = require("opener")
+ , glob = require("glob")
function help (args, cb) {
- var num = 1
- , argv = npm.config.get("argv").cooked
- if (argv.length && -1 !== argv[0].indexOf("api")) {
- num = 3
+ var argv = npm.config.get("argv").cooked
+
+ var argnum = 0
+ if (args.length === 2 && ~~args[0]) {
+ argnum = ~~args.shift()
}
+ // npm help foo bar baz: search topics
if (args.length > 1 && args[0]) {
return npm.commands["help-search"](args, num, cb)
}
var section = npm.deref(args[0]) || args[0]
- if (section) {
- if ( npm.config.get("usage")
- && npm.commands[section]
- && npm.commands[section].usage
- ) {
- npm.config.set("loglevel", "silent")
- log.level = "silent"
- console.log(npm.commands[section].usage)
- return cb()
- }
-
- var sectionPath = path.join( __dirname, "..", "man", "man" + num
- , section + "." + num)
- , htmlPath = path.resolve( __dirname, "..", "html"
- , num === 3 ? "api" : "doc"
- , section+".html" )
- return fs.stat
- ( sectionPath
- , function (e, o) {
- if (e) return npm.commands["help-search"](args, cb)
-
- var manpath = path.join(__dirname, "..", "man")
- , env = {}
- Object.keys(process.env).forEach(function (i) {
- env[i] = process.env[i]
- })
- env.MANPATH = manpath
- var viewer = npm.config.get("viewer")
-
- switch (viewer) {
- case "woman":
- var a = ["-e", "(woman-find-file \"" + sectionPath + "\")"]
- var conf = { env: env, customFds: [ 0, 1, 2] }
- var woman = spawn("emacsclient", a, conf)
- woman.on("close", cb)
- break
-
- case "browser":
- opener(htmlPath, { command: npm.config.get("browser") }, cb)
- break
-
- default:
- var conf = { env: env, customFds: [ 0, 1, 2] }
- var man = spawn("man", [num, section], conf)
- man.on("close", cb)
- }
- }
- )
- } else getSections(function (er, sections) {
- if (er) return cb(er)
+ // npm help <noargs>: show basic usage
+ if (!section)
+ return npmUsage(cb)
+
+ // npm <cmd> -h: show command usage
+ if ( npm.config.get("usage")
+ && npm.commands[section]
+ && npm.commands[section].usage
+ ) {
npm.config.set("loglevel", "silent")
log.level = "silent"
- console.log
- ( ["\nUsage: npm <command>"
- , ""
- , "where <command> is one of:"
- , npm.config.get("long") ? usages()
- : " " + wrap(Object.keys(npm.commands))
- , ""
- , "npm <cmd> -h quick help on <cmd>"
- , "npm -l display full usage info"
- , "npm faq commonly asked questions"
- , "npm help <term> search for help on <term>"
- , "npm help npm involved overview"
- , ""
- , "Specify configs in the ini-formatted file:"
- , " " + npm.config.get("userconfig")
- , "or on the command line via: npm <command> --key value"
- , "Config info can be viewed via: npm help config"
- , ""
- , "npm@" + npm.version + " " + path.dirname(__dirname)
- ].join("\n"))
- cb(er)
+ console.log(npm.commands[section].usage)
+ return cb()
+ }
+
+ // npm apihelp <section>: Prefer section 3 over section 1
+ var apihelp = argv.length && -1 !== argv[0].indexOf("api")
+ var pref = apihelp ? [3, 1, 5, 7] : [1, 3, 5, 7]
+ if (argnum)
+ pref = [ argnum ].concat(pref.filter(function (n) {
+ return n !== argnum
+ }))
+
+ // npm help <section>: Try to find the path
+ var manroot = path.resolve(__dirname, "..", "man")
+ var htmlroot = path.resolve(__dirname, "..", "html", "doc")
+
+ // legacy
+ if (section === "global")
+ section = "folders"
+ else if (section === "json")
+ section = "package.json"
+
+ // find either /section.n or /npm-section.n
+ var f = "+(npm-" + section + "|" + section + ").[0-9]"
+ return glob(manroot + "/*/" + f, function (er, mans) {
+ if (er)
+ return cb(er)
+
+ if (!mans.length)
+ return npm.commands["help-search"](args, cb)
+
+ viewMan(pickMan(mans, pref), cb)
+ })
+}
+
+function pickMan (mans, pref_) {
+ var nre = /([0-9]+)$/
+ var pref = {}
+ pref_.forEach(function (sect, i) {
+ pref[sect] = i
+ })
+ mans = mans.sort(function (a, b) {
+ var an = a.match(nre)[1]
+ var bn = b.match(nre)[1]
+ return an === bn ? (a > b ? -1 : 1)
+ : pref[an] < pref[bn] ? -1
+ : 1
+ })
+ return mans[0]
+}
+
+function viewMan (man, cb) {
+ var nre = /([0-9]+)$/
+ var num = man.match(nre)[1]
+ var section = path.basename(man, "." + num)
+
+ // at this point, we know that the specified man page exists
+ var manpath = path.join(__dirname, "..", "man")
+ , env = {}
+ Object.keys(process.env).forEach(function (i) {
+ env[i] = process.env[i]
})
+ env.MANPATH = manpath
+ var viewer = npm.config.get("viewer")
+
+ switch (viewer) {
+ case "woman":
+ var a = ["-e", "(woman-find-file \"" + man + "\")"]
+ var conf = { env: env, customFds: [ 0, 1, 2] }
+ var woman = spawn("emacsclient", a, conf)
+ woman.on("close", cb)
+ break
+
+ case "browser":
+ opener(htmlMan(man), { command: npm.config.get("browser") }, cb)
+ break
+
+ default:
+ var conf = { env: env, customFds: [ 0, 1, 2] }
+ var man = spawn("man", [num, section], conf)
+ man.on("close", cb)
+ break
+ }
+}
+
+function htmlMan (man) {
+ var sect = +man.match(/([0-9]+)$/)[1]
+ var f = path.basename(man).replace(/([0-9]+)$/, "html")
+ switch (sect) {
+ case 1:
+ sect = "cli"
+ break
+ case 3:
+ sect = "api"
+ break
+ case 5:
+ sect = "files"
+ break
+ case 7:
+ sect = "misc"
+ break
+ default:
+ throw new Error("invalid man section: " + sect)
+ }
+ return path.resolve(__dirname, "..", "html", "doc", sect, f)
+}
+
+function npmUsage (cb) {
+ npm.config.set("loglevel", "silent")
+ log.level = "silent"
+ console.log
+ ( ["\nUsage: npm <command>"
+ , ""
+ , "where <command> is one of:"
+ , npm.config.get("long") ? usages()
+ : " " + wrap(Object.keys(npm.commands))
+ , ""
+ , "npm <cmd> -h quick help on <cmd>"
+ , "npm -l display full usage info"
+ , "npm faq commonly asked questions"
+ , "npm help <term> search for help on <term>"
+ , "npm help npm involved overview"
+ , ""
+ , "Specify configs in the ini-formatted file:"
+ , " " + npm.config.get("userconfig")
+ , "or on the command line via: npm <command> --key value"
+ , "Config info can be viewed via: npm help config"
+ , ""
+ , "npm@" + npm.version + " " + path.dirname(__dirname)
+ ].join("\n"))
+ cb()
}
function usages () {
@@ -127,9 +196,17 @@ function usages () {
function wrap (arr) {
var out = ['']
, l = 0
+ , line
+
+ line = process.stdout.columns
+ if (!line)
+ line = 60
+ else
+ line = Math.min(60, Math.max(line - 16, 24))
+
arr.sort(function (a,b) { return a<b?-1:1 })
.forEach(function (c) {
- if (out[l].length + c.length + 2 < 60) {
+ if (out[l].length + c.length + 2 < line) {
out[l] += ', '+c
} else {
out[l++] += ','
@@ -139,16 +216,16 @@ function wrap (arr) {
return out.join("\n ").substr(2)
}
-function getSections (num, cb) {
- if (typeof cb !== "function") cb = num, num = 1
-
- var mp = path.join(__dirname, "../man/man" + num + "/")
- , cleaner = new RegExp("\\." + num + "$")
- fs.readdir(mp, function (er, files) {
- if (er) return cb(er)
- var sectionList = files.concat("help." + num)
- .filter(function (s) { return s.match(cleaner) })
- .map(function (s) { return s.replace(cleaner, "")})
- cb(null, sectionList)
+function getSections (cb) {
+ var g = path.resolve(__dirname, "../man/man[0-9]/*.[0-9]")
+ glob(g, function (er, files) {
+ if (er)
+ return cb(er)
+ cb(null, Object.keys(files.reduce(function (acc, file) {
+ file = path.basename(file).replace(/\.[0-9]+$/, "")
+ file = file.replace(/^npm-/, "")
+ acc[file] = true
+ return acc
+ }, { help: true })))
})
}