1 |
|
2 | module.exports = help
|
3 |
|
4 | help.completion = function (opts, cb) {
|
5 | if (opts.conf.argv.remain.length > 2) return cb(null, [])
|
6 | getSections(cb)
|
7 | }
|
8 |
|
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")
|
15 |
|
16 | function help (args, cb) {
|
17 | npm.spinner.stop()
|
18 | var argv = npm.config.get("argv").cooked
|
19 |
|
20 | var argnum = 0
|
21 | if (args.length === 2 && ~~args[0]) {
|
22 | argnum = ~~args.shift()
|
23 | }
|
24 |
|
25 |
|
26 | if (args.length > 1 && args[0]) {
|
27 | return npm.commands["help-search"](args, argnum, cb)
|
28 | }
|
29 |
|
30 | var section = npm.deref(args[0]) || args[0]
|
31 |
|
32 |
|
33 | if (!section) {
|
34 | var valid = argv[0] === "help" ? 0 : 1
|
35 | return npmUsage(valid, cb)
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 | if ( npm.config.get("usage")
|
41 | && npm.commands[section]
|
42 | && npm.commands[section].usage
|
43 | ) {
|
44 | npm.config.set("loglevel", "silent")
|
45 | log.level = "silent"
|
46 | console.log(npm.commands[section].usage)
|
47 | return cb()
|
48 | }
|
49 |
|
50 |
|
51 | var apihelp = argv.length && -1 !== argv[0].indexOf("api")
|
52 | var pref = apihelp ? [3, 1, 5, 7] : [1, 3, 5, 7]
|
53 | if (argnum)
|
54 | pref = [ argnum ].concat(pref.filter(function (n) {
|
55 | return n !== argnum
|
56 | }))
|
57 |
|
58 |
|
59 | var manroot = path.resolve(__dirname, "..", "man")
|
60 |
|
61 |
|
62 | if (section === "global")
|
63 | section = "folders"
|
64 | else if (section === "json")
|
65 | section = "package.json"
|
66 |
|
67 |
|
68 | var f = "+(npm-" + section + "|" + section + ").[0-9]"
|
69 | return glob(manroot + "/*/" + f, function (er, mans) {
|
70 | if (er)
|
71 | return cb(er)
|
72 |
|
73 | if (!mans.length)
|
74 | return npm.commands["help-search"](args, cb)
|
75 |
|
76 | viewMan(pickMan(mans, pref), cb)
|
77 | })
|
78 | }
|
79 |
|
80 | function pickMan (mans, pref_) {
|
81 | var nre = /([0-9]+)$/
|
82 | var pref = {}
|
83 | pref_.forEach(function (sect, i) {
|
84 | pref[sect] = i
|
85 | })
|
86 | mans = mans.sort(function (a, b) {
|
87 | var an = a.match(nre)[1]
|
88 | var bn = b.match(nre)[1]
|
89 | return an === bn ? (a > b ? -1 : 1)
|
90 | : pref[an] < pref[bn] ? -1
|
91 | : 1
|
92 | })
|
93 | return mans[0]
|
94 | }
|
95 |
|
96 | function viewMan (man, cb) {
|
97 | var nre = /([0-9]+)$/
|
98 | var num = man.match(nre)[1]
|
99 | var section = path.basename(man, "." + num)
|
100 |
|
101 |
|
102 | var manpath = path.join(__dirname, "..", "man")
|
103 | , env = {}
|
104 | Object.keys(process.env).forEach(function (i) {
|
105 | env[i] = process.env[i]
|
106 | })
|
107 | env.MANPATH = manpath
|
108 | var viewer = npm.config.get("viewer")
|
109 |
|
110 | var conf
|
111 | switch (viewer) {
|
112 | case "woman":
|
113 | var a = ["-e", "(woman-find-file \"" + man + "\")"]
|
114 | conf = { env: env, stdio: "inherit" }
|
115 | var woman = spawn("emacsclient", a, conf)
|
116 | woman.on("close", cb)
|
117 | break
|
118 |
|
119 | case "browser":
|
120 | opener(htmlMan(man), { command: npm.config.get("browser") }, cb)
|
121 | break
|
122 |
|
123 | default:
|
124 | conf = { env: env, stdio: "inherit" }
|
125 | var manProcess = spawn("man", [num, section], conf)
|
126 | manProcess.on("close", cb)
|
127 | break
|
128 | }
|
129 | }
|
130 |
|
131 | function htmlMan (man) {
|
132 | var sect = +man.match(/([0-9]+)$/)[1]
|
133 | var f = path.basename(man).replace(/([0-9]+)$/, "html")
|
134 | switch (sect) {
|
135 | case 1:
|
136 | sect = "cli"
|
137 | break
|
138 | case 3:
|
139 | sect = "api"
|
140 | break
|
141 | case 5:
|
142 | sect = "files"
|
143 | break
|
144 | case 7:
|
145 | sect = "misc"
|
146 | break
|
147 | default:
|
148 | throw new Error("invalid man section: " + sect)
|
149 | }
|
150 | return path.resolve(__dirname, "..", "html", "doc", sect, f)
|
151 | }
|
152 |
|
153 | function npmUsage (valid, cb) {
|
154 | npm.config.set("loglevel", "silent")
|
155 | log.level = "silent"
|
156 | console.log(
|
157 | [ "\nUsage: " + npm.name + " <command>"
|
158 | , ""
|
159 | , "where <command> is one of:"
|
160 | , npm.config.get("long") ? usages()
|
161 | : " " + wrap(Object.keys(npm.commands))
|
162 | , ""
|
163 | , "npm <cmd> -h quick help on <cmd>"
|
164 | , "npm -l display full usage info"
|
165 | , "npm faq commonly asked questions"
|
166 | , "npm help <term> search for help on <term>"
|
167 | , "npm help npm involved overview"
|
168 | , ""
|
169 | , "Specify configs in the ini-formatted file:"
|
170 | , " " + npm.config.get("userconfig")
|
171 | , "or on the command line via: npm <command> --key value"
|
172 | , "Config info can be viewed via: npm help config"
|
173 | , ""
|
174 | , npm.name + "@" + npm.version + " " + path.dirname(__dirname)
|
175 | ].join("\n"))
|
176 | cb(valid)
|
177 | }
|
178 |
|
179 | function usages () {
|
180 |
|
181 | var maxLen = 0
|
182 | return Object.keys(npm.commands).filter(function (c) {
|
183 | return c === npm.deref(c)
|
184 | }).reduce(function (set, c) {
|
185 | set.push([c, npm.commands[c].usage || ""])
|
186 | maxLen = Math.max(maxLen, c.length)
|
187 | return set
|
188 | }, []).map(function (item) {
|
189 | var c = item[0]
|
190 | , usage = item[1]
|
191 | return "\n " + c + (new Array(maxLen - c.length + 2).join(" "))
|
192 | + (usage.split("\n")
|
193 | .join("\n" + (new Array(maxLen + 6).join(" "))))
|
194 | }).join("\n")
|
195 | }
|
196 |
|
197 |
|
198 | function wrap (arr) {
|
199 | var out = [""]
|
200 | , l = 0
|
201 | , line
|
202 |
|
203 | line = process.stdout.columns
|
204 | if (!line)
|
205 | line = 60
|
206 | else
|
207 | line = Math.min(60, Math.max(line - 16, 24))
|
208 |
|
209 | arr.sort(function (a,b) { return a<b?-1:1 })
|
210 | .forEach(function (c) {
|
211 | if (out[l].length + c.length + 2 < line) {
|
212 | out[l] += ", "+c
|
213 | } else {
|
214 | out[l++] += ","
|
215 | out[l] = c
|
216 | }
|
217 | })
|
218 | return out.join("\n ").substr(2)
|
219 | }
|
220 |
|
221 | function getSections (cb) {
|
222 | var g = path.resolve(__dirname, "../man/man[0-9]/*.[0-9]")
|
223 | glob(g, function (er, files) {
|
224 | if (er)
|
225 | return cb(er)
|
226 | cb(null, Object.keys(files.reduce(function (acc, file) {
|
227 | file = path.basename(file).replace(/\.[0-9]+$/, "")
|
228 | file = file.replace(/^npm-/, "")
|
229 | acc[file] = true
|
230 | return acc
|
231 | }, { help: true })))
|
232 | })
|
233 | }
|