Permalink
Browse files

tmp

  • Loading branch information...
1 parent 7fd4d88 commit ffff8108daac8a7365d0eb30b352bab00cb7f396 @isaacs isaacs committed May 25, 2011
Showing with 33 additions and 15 deletions.
  1. +33 −15 lib/help-search.js
View
@@ -3,25 +3,43 @@ module.exports = helpSearch
var fs = require("./utils/graceful-fs")
, output = require("./utils/output")
- , docs_path = "../npm/doc/"
+ , path = require("path")
+ , asyncMap = require("./utils/async-map")
+ , docsPath = path.join(__dirname, "..", "npm", "doc")
helpSearch.usage = "npm help-search <text>"
- + "\nwhere <text> is the text to find in the documentation"
function helpSearch (args, cb) {
- if (! args.length) {
- output.write("Usage: " + helpSearch.usage, cb)
- return
- }
-
- fs.readdir(docs_path, function(err, files) {
- if (err) {
+ if (!args.length) return cb(helpSearch.usage)
+
+ fs.readdir(docsPath, function(er, files) {
+ if (er) {
return cb(new Error("Could not load documentation"))
}
-
+
var search = args.join(" ")
- for (var file in files) {
- var data = fs.readFileSync(docs_path + files[file]).toString()
+ , results = []
+ asyncMap(files, function (file, cb) {
+ fs.readFile(function (er, data) {
+ if (er) return cb(er)
+ if (data.indexOf(search) === -1) return cb(null, [])
+
+ var start = result > 20 ? result - 20 : 0;
+ , context = data.substr(start, result - start)
+ + "\033[31;40m"
+ + data.substr(result, search.length)
+ + "\033[0m"
+ + data.substr(result + search.length, 20)
+
+ return cb(null, { file: file, context: context, terms: search })
+ })
+ }, function (er, results) {
+ if (er) return cb(er)
+ output.write(results.join("\n"), function (er) { cb(er, results) })
+ })
+
+ Object.keys(files).forEach(function (file) {
+ var data = fs.readFileSync(docsPath + files[file]).toString()
var result = data.indexOf(search)
if (result !== -1) {
var start = result > 20 ? result - 20 : 0;
@@ -32,8 +50,8 @@ function helpSearch (args, cb) {
+ data.substr(result + search.length, 20)
var out = "`npm help " + files[file].replace(".md", "") + "` "
+ context.replace(/\n/gi, "").substr(0,60)
- output.write(out, cb)
+ results.push(out)
}
- }
+ })
})
-}
+}

0 comments on commit ffff810

Please sign in to comment.