Permalink
Browse files

Added install completion on folders and urls.

  • Loading branch information...
1 parent 2670a55 commit 5d5648bbae864a251fceb84b2965c9b049ee7fd5 @deestan committed Jul 5, 2012
Showing with 41 additions and 3 deletions.
  1. +40 −2 lib/install.js
  2. +1 −1 lib/utils/completion.sh
View
42 lib/install.js
@@ -44,12 +44,50 @@ install.completion = function (opts, cb) {
// install can complete to a folder with a package.json, or any package.
// if it has a slash, then it's gotta be a folder
// if it starts with https?://, then just give up, because it's a url
- // for now, not yet implemented.
+ if (/^https?:\/\//.test(opts.partialWord)) {
+ // do not complete to URLs
+ return cb(null, [])
+ }
+
+ if (/\//.test(opts.partialWord)) {
+ // Complete fully to folder if there is exactly one match and
+ // it is a folder containing a package.json file.
+ var lastSlashIdx = opts.partialWord.lastIndexOf("/")
+ var partialPath = opts.partialWord.slice(0, lastSlashIdx)
+ if (partialPath === "")
+ partialPath = "/"
+ var remaining = opts.partialWord.slice(lastSlashIdx + 1)
+ fs.readdir(partialPath, function (err, siblings) {
+ if (err) return cb(null, []) // do not complete on invalid dirs
+ var matches = 0
+ var packageDir = null
+ asyncMap(siblings, function (sibling, cb) {
+ var fullPath = path.join(partialPath, sibling)
+ if (sibling.slice(0, remaining.length) !== remaining)
+ return cb() // no match - skip it
+ fs.readdir(fullPath, function (err, contents) {
+ if (err) return cb() // Was not a dir - skip it
+ matches += 1
+ if (contents.indexOf("package.json") !== -1)
+ packageDir = fullPath
+ cb()
+ })
+ }, function (err) {
+ if (err) return cb(err)
+ if (matches === 1 && packageDir !== null)
+ return cb(null, [packageDir])
+ return cb(null, [])
+ })
+ })
+ return
+ }
+
+ // complete to registry
var registry = npm.registry
registry.get("/-/short", function (er, pkgs) {
if (er) return cb()
if (!opts.partialWord) return cb(null, pkgs)
-
+
var name = opts.partialWord.split("@").shift()
pkgs = pkgs.filter(function (p) {
return p.indexOf(name) === 0
View
2 lib/utils/completion.sh
@@ -21,7 +21,7 @@ if type complete &>/dev/null; then
2>/dev/null)) || return $?
IFS="$si"
}
- complete -F _npm_completion npm
+ complete -o default -F _npm_completion npm
elif type compdef &>/dev/null; then
_npm_completion() {
si=$IFS

0 comments on commit 5d5648b

Please sign in to comment.