2 module.exports = helpSearch
4 var fs = require("graceful-fs")
5 , path = require("path")
6 , asyncMap = require("slide").asyncMap
7 , npm = require("./npm.js")
8 , glob = require("glob")
9 , color = require("ansicolors")
11 helpSearch.usage = "npm help-search <text>"
13 function helpSearch (args, silent, cb) {
14 if (typeof cb !== "function") cb = silent, silent = false
15 if (!args.length) return cb(helpSearch.usage)
17 var docPath = path.resolve(__dirname, "..", "doc")
18 return glob(docPath + "/*/*.md", function (er, files) {
21 readFiles(files, function (er, data) {
24 searchFiles(args, data, function (er, results) {
27 formatResults(args, results, cb)
33 function readFiles (files, cb) {
35 asyncMap(files, function (file, cb) {
36 fs.readFile(file, 'utf8', function (er, data) {
45 function searchFiles (args, files, cb) {
47 Object.keys(files).forEach(function (file) {
48 var data = files[file]
50 // skip if no matches at all
52 for (var a = 0, l = args.length; a < l && !match; a++) {
53 match = data.toLowerCase().indexOf(args[a].toLowerCase()) !== -1
58 var lines = data.split(/\n+/)
60 // if a line has a search term, then skip it and the next line.
61 // if the next line has a search term, then skip all 3
62 // otherwise, set the line to null. then remove the nulls.
64 for (var i = 0; i < l; i ++) {
66 , nextLine = lines[i + 1]
71 for (a = 0, ll = args.length; a < ll && !match; a ++) {
72 match = nextLine.toLowerCase()
73 .indexOf(args[a].toLowerCase()) !== -1
76 // skip over the next line, and the line after it.
83 for (a = 0, ll = args.length; a < ll && !match; a ++) {
84 match = line.toLowerCase().indexOf(args[a].toLowerCase()) !== -1
87 // skip over the next line
95 // now squish any string of nulls into a single null
96 lines = lines.reduce(function (l, r) {
97 if (!(r === null && l[l.length-1] === null)) l.push(r)
101 if (lines[lines.length - 1] === null) lines.pop()
102 if (lines[0] === null) lines.shift()
104 // now see how many args were found at all.
107 lines.forEach(function (line) {
108 args.forEach(function (arg) {
109 var hit = (line || "").toLowerCase()
110 .split(arg.toLowerCase()).length - 1
112 found[arg] = (found[arg] || 0) + hit
118 var cmd = "npm help "
119 if (path.basename(path.dirname(file)) === "api") {
122 cmd += path.basename(file, ".md").replace(/^npm-/, "")
123 results.push({ file: file
126 , found: Object.keys(found)
128 , totalHits: totalHits
132 // if only one result, then just show that help section.
133 if (results.length === 1) {
134 return npm.commands.help([results[0].file.replace(/\.md$/, "")], cb)
137 if (results.length === 0) {
138 console.log("No results for " + args.map(JSON.stringify).join(" "))
142 // sort results by number of results found, then by number of hits
143 // then by number of matching lines
144 results = results.sort(function (a, b) {
145 return a.found.length > b.found.length ? -1
146 : a.found.length < b.found.length ? 1
147 : a.totalHits > b.totalHits ? -1
148 : a.totalHits < b.totalHits ? 1
149 : a.lines.length > b.lines.length ? -1
150 : a.lines.length < b.lines.length ? 1
157 function formatResults (args, results, cb) {
158 if (!results) return cb(null)
160 var cols = Math.min(process.stdout.columns || Infinity, 80) + 1
162 var out = results.map(function (res) {
164 , r = Object.keys(res.hits).map(function (k) {
165 return k + ":" + res.hits[k]
166 }).sort(function (a, b) {
167 return a > b ? 1 : -1
170 out += ((new Array(Math.max(1, cols - out.length - r.length)))
173 if (!npm.config.get("long")) return out
176 + "\n" + (new Array(cols)).join("—") + "\n"
177 + res.lines.map(function (line, i) {
178 if (line === null || i > 3) return ""
179 for (var out = line, a = 0, l = args.length; a < l; a ++) {
180 var finder = out.toLowerCase().split(args[a].toLowerCase())
184 finder.forEach(function (f) {
185 newOut += out.substr(p, f.length)
187 var hilit = out.substr(p + f.length, args[a].length)
188 if (npm.color) hilit = color.bgBlack(color.red(hilit))
191 p += f.length + args[a].length
200 if (results.length && !npm.config.get("long")) {
201 out = "Top hits for "+(args.map(JSON.stringify).join(" "))
202 + "\n" + (new Array(cols)).join("—") + "\n"
204 + "\n" + (new Array(cols)).join("—") + "\n"
205 + "(run with -l or --long to see more context)"
208 console.log(out.trim())