]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/help.js
942d27b41e17b8bb2f8bfa5acaacaa84c2c5ac5c
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / help.js
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   // npm help foo bar baz: search topics
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   // npm help <noargs>:  show basic usage
33   if (!section) {
34     var valid = argv[0] === "help" ? 0 : 1
35     return npmUsage(valid, cb)
36   }
37
38
39   // npm <cmd> -h: show command usage
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   // 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]
53   if (argnum)
54     pref = [ argnum ].concat(pref.filter(function (n) {
55       return n !== argnum
56     }))
57
58   // npm help <section>: Try to find the path
59   var manroot = path.resolve(__dirname, "..", "man")
60
61   // legacy
62   if (section === "global") section = "folders"
63   else if (section === "json") section = "package.json"
64
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) {
72     if (er) return cb(er)
73
74     if (!mans.length) return npm.commands["help-search"](args, cb)
75
76     mans = mans.map(function (man) {
77       var ext = path.extname(man)
78       if (man.match(new RegExp(compextre))) man = path.basename(man, ext)
79
80       return man
81     })
82
83     viewMan(pickMan(mans, pref), cb)
84   })
85 }
86
87 function pickMan (mans, pref_) {
88   var nre = /([0-9]+)$/
89   var pref = {}
90   pref_.forEach(function (sect, i) {
91     pref[sect] = i
92   })
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
98          : 1
99   })
100   return mans[0]
101 }
102
103 function viewMan (man, cb) {
104   var nre = /([0-9]+)$/
105   var num = man.match(nre)[1]
106   var section = path.basename(man, "." + num)
107
108   // at this point, we know that the specified man page exists
109   var manpath = path.join(__dirname, "..", "man")
110     , env = {}
111   Object.keys(process.env).forEach(function (i) {
112     env[i] = process.env[i]
113   })
114   env.MANPATH = manpath
115   var viewer = npm.config.get("viewer")
116
117   var conf
118   switch (viewer) {
119     case "woman":
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)
124       break
125
126     case "browser":
127       opener(htmlMan(man), { command: npm.config.get("browser") }, cb)
128       break
129
130     default:
131       conf = { env: env, stdio: "inherit" }
132       var manProcess = spawn("man", [num, section], conf)
133       manProcess.on("close", cb)
134       break
135   }
136 }
137
138 function htmlMan (man) {
139   var sect = +man.match(/([0-9]+)$/)[1]
140   var f = path.basename(man).replace(/([0-9]+)$/, "html")
141   switch (sect) {
142     case 1:
143       sect = "cli"
144       break
145     case 3:
146       sect = "api"
147       break
148     case 5:
149       sect = "files"
150       break
151     case 7:
152       sect = "misc"
153       break
154     default:
155       throw new Error("invalid man section: " + sect)
156   }
157   return path.resolve(__dirname, "..", "html", "doc", sect, f)
158 }
159
160 function npmUsage (valid, cb) {
161   npm.config.set("loglevel", "silent")
162   log.level = "silent"
163   console.log(
164     [ "\nUsage: npm <command>"
165       , ""
166       , "where <command> is one of:"
167       , npm.config.get("long") ? usages()
168         : "    " + wrap(Object.keys(npm.commands))
169       , ""
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"
175       , ""
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"
180       , ""
181       , "npm@" + npm.version + " " + path.dirname(__dirname)
182       ].join("\n"))
183   cb(valid)
184 }
185
186 function usages () {
187   // return a string of <cmd>: <usage>
188   var maxLen = 0
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)
194     return set
195   }, []).map(function (item) {
196     var c = item[0]
197       , usage = item[1]
198     return "\n    " + c + (new Array(maxLen - c.length + 2).join(" "))
199          + (usage.split("\n")
200             .join("\n" + (new Array(maxLen + 6).join(" "))))
201   }).join("\n")
202 }
203
204
205 function wrap (arr) {
206   var out = [""]
207     , l = 0
208     , line
209
210   line = process.stdout.columns
211   if (!line)
212     line = 60
213   else
214     line = Math.min(60, Math.max(line - 16, 24))
215
216   arr.sort(function (a,b) { return a<b?-1:1 })
217     .forEach(function (c) {
218       if (out[l].length + c.length + 2 < line) {
219         out[l] += ", "+c
220       } else {
221         out[l++] += ","
222         out[l] = c
223       }
224     })
225   return out.join("\n    ").substr(2)
226 }
227
228 function getSections (cb) {
229   var g = path.resolve(__dirname, "../man/man[0-9]/*.[0-9]")
230   glob(g, function (er, files) {
231     if (er)
232       return cb(er)
233     cb(null, Object.keys(files.reduce(function (acc, file) {
234       file = path.basename(file).replace(/\.[0-9]+$/, "")
235       file = file.replace(/^npm-/, "")
236       acc[file] = true
237       return acc
238     }, { help: true })))
239   })
240 }