diff options
Diffstat (limited to 'lib/completion.js')
-rw-r--r-- | lib/completion.js | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/lib/completion.js b/lib/completion.js index 5bca58c91..2217a4304 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -2,31 +2,34 @@ module.exports = completion completion.usage = "Not intended to be used directly.\n" - + "See the npm-completion.sh script in the npm source directory" + + "See the npm-completion.sh script in the npm " + + "source directory" var npm = require("../npm") + , getCompletions = require("./utils/completion/get-completions") + , containsSingleMatch = require("./utils/completion/contains-single-match") , output = require("./utils/output") + , index = npm.config.get("comp-cword") || process.env.COMP_CWORD function completion (args, cb_) { - var index = npm.config.get("comp-cword") || process.env.COMP_CWORD || args.length - 1 - , c = args[index] || "" - , p = args[index - 1] - , outfd = npm.config.get("outfd") - , m = [] + var cmd = args[1] || "" + , complFullList = getCompletions(cmd, npm.fullList, true) - // TODO: Need to have command-specific functions or something for completion. - // so, if you do "npm install <TAB>", then it should show a list of the package - // names in the registry, and "npm install foo<TAB>" should show all the install - // targets for foo. - // Especially, stuff like "npm config" that have sub-commands should get - // completion love. + function cb (er, list) { + if (er) return cb_(er) + outputCompletions(list, cb_) + } - npm.fullList.forEach(function (f) { - // console.error(f) - var a = npm.deref(f) - if (m.indexOf(a) === -1 && f.indexOf(c) === 0) m.push(a) - }) - function cb () { cb_(m.length ? null : "no match for "+JSON.stringify(c), m) } - output.write(outfd, m, false, cb) + if (index > 1 || (complFullList.indexOf(cmd) !== -1 && + containsSingleMatch(cmd, complFullList))) { + var subargs = args.slice(2) + // TODO: bundle + npm.commands[npm.deref(cmd)].completion(subargs, index, cb) + } else cb(null, complFullList) } +function outputCompletions (list, cb_) { + var outfd = npm.config.get("outfd") + function cb () { cb_(list.length ? null : "no match found", list) } + output.write(outfd, list, false, cb) +} |