4 help.completion = function (opts, cb) {
5 if (opts.conf.argv.remain.length > 2) return cb(null, [])
9 var path = require("path")
10 , spawn = require("./utils/spawn")
11 , npm = require("./npm.js")
12 , log = require("npmlog")
13 , opener = require("opener")
14 , glob = require("glob")
16 function help (args, cb) {
18 var argv = npm.config.get("argv").cooked
21 if (args.length === 2 && ~~args[0]) {
22 argnum = ~~args.shift()
25 // npm help foo bar baz: search topics
26 if (args.length > 1 && args[0]) {
27 return npm.commands["help-search"](args, argnum, cb)
30 var section = npm.deref(args[0]) || args[0]
32 // npm help <noargs>: show basic usage
34 var valid = argv[0] === "help" ? 0 : 1
35 return npmUsage(valid, cb)
39 // npm <cmd> -h: show command usage
40 if ( npm.config.get("usage")
41 && npm.commands[section]
42 && npm.commands[section].usage
44 npm.config.set("loglevel", "silent")
46 console.log(npm.commands[section].usage)
50 // npm apihelp <section>: Prefer section 3 over section 1
51 var apihelp = argv.length && -1 !== argv[0].indexOf("api")
52 var pref = apihelp ? [3, 1, 5, 7] : [1, 3, 5, 7]
54 pref = [ argnum ].concat(pref.filter(function (n) {
58 // npm help <section>: Try to find the path
59 var manroot = path.resolve(__dirname, "..", "man")
62 if (section === "global") section = "folders"
63 else if (section === "json") section = "package.json"
65 // find either /section.n or /npm-section.n
66 // The glob is used in the glob. The regexp is used much
67 // further down. Globs and regexps are different
68 var compextglob = ".+(gz|bz2|lzma|[FYzZ]|xz)"
69 var compextre = "\\.(gz|bz2|lzma|[FYzZ]|xz)$"
70 var f = "+(npm-" + section + "|" + section + ").[0-9]?(" + compextglob + ")"
71 return glob(manroot + "/*/" + f, function (er, mans) {
74 if (!mans.length) return npm.commands["help-search"](args, cb)
76 mans = mans.map(function (man) {
77 var ext = path.extname(man)
78 if (man.match(new RegExp(compextre))) man = path.basename(man, ext)
83 viewMan(pickMan(mans, pref), cb)
87 function pickMan (mans, pref_) {
90 pref_.forEach(function (sect, i) {
93 mans = mans.sort(function (a, b) {
94 var an = a.match(nre)[1]
95 var bn = b.match(nre)[1]
96 return an === bn ? (a > b ? -1 : 1)
97 : pref[an] < pref[bn] ? -1
103 function viewMan (man, cb) {
104 var nre = /([0-9]+)$/
105 var num = man.match(nre)[1]
106 var section = path.basename(man, "." + num)
108 // at this point, we know that the specified man page exists
109 var manpath = path.join(__dirname, "..", "man")
111 Object.keys(process.env).forEach(function (i) {
112 env[i] = process.env[i]
114 env.MANPATH = manpath
115 var viewer = npm.config.get("viewer")
120 var a = ["-e", "(woman-find-file \"" + man + "\")"]
121 conf = { env: env, stdio: "inherit" }
122 var woman = spawn("emacsclient", a, conf)
123 woman.on("close", cb)
127 opener(htmlMan(man), { command: npm.config.get("browser") }, cb)
131 conf = { env: env, stdio: "inherit" }
132 var manProcess = spawn("man", [num, section], conf)
133 manProcess.on("close", cb)
138 function htmlMan (man) {
139 var sect = +man.match(/([0-9]+)$/)[1]
140 var f = path.basename(man).replace(/([0-9]+)$/, "html")
155 throw new Error("invalid man section: " + sect)
157 return path.resolve(__dirname, "..", "html", "doc", sect, f)
160 function npmUsage (valid, cb) {
161 npm.config.set("loglevel", "silent")
164 [ "\nUsage: npm <command>"
166 , "where <command> is one of:"
167 , npm.config.get("long") ? usages()
168 : " " + wrap(Object.keys(npm.commands))
170 , "npm <cmd> -h quick help on <cmd>"
171 , "npm -l display full usage info"
172 , "npm faq commonly asked questions"
173 , "npm help <term> search for help on <term>"
174 , "npm help npm involved overview"
176 , "Specify configs in the ini-formatted file:"
177 , " " + npm.config.get("userconfig")
178 , "or on the command line via: npm <command> --key value"
179 , "Config info can be viewed via: npm help config"
181 , "npm@" + npm.version + " " + path.dirname(__dirname)
187 // return a string of <cmd>: <usage>
189 return Object.keys(npm.commands).filter(function (c) {
190 return c === npm.deref(c)
191 }).reduce(function (set, c) {
192 set.push([c, npm.commands[c].usage || ""])
193 maxLen = Math.max(maxLen, c.length)
195 }, []).map(function (item) {
198 return "\n " + c + (new Array(maxLen - c.length + 2).join(" "))
200 .join("\n" + (new Array(maxLen + 6).join(" "))))
205 function wrap (arr) {
210 line = process.stdout.columns
214 line = Math.min(60, Math.max(line - 16, 24))
216 arr.sort(function (a,b) { return a<b?-1:1 })
217 .forEach(function (c) {
218 if (out[l].length + c.length + 2 < line) {
225 return out.join("\n ").substr(2)
228 function getSections (cb) {
229 var g = path.resolve(__dirname, "../man/man[0-9]/*.[0-9]")
230 glob(g, function (er, files) {
233 cb(null, Object.keys(files.reduce(function (acc, file) {
234 file = path.basename(file).replace(/\.[0-9]+$/, "")
235 file = file.replace(/^npm-/, "")