Skip to content
Browse files

Initial bash completion support. Very rudimentary.

  • Loading branch information...
1 parent 1b9243f commit cef89a20b97988596b0f523fd107b3ced1dbcbe0 @isaacs isaacs committed Oct 29, 2010
Showing with 67 additions and 9 deletions.
  1. +32 −0 lib/completion.js
  2. +31 −0 npm-completion.sh
  3. +4 −9 npm.js
View
32 lib/completion.js
@@ -0,0 +1,32 @@
+
+module.exports = completion
+
+completion.usage = "Not intended to be used directly.\n"
+ + "See the npm-completion.sh script in the npm source directory"
+
+var npm = require("../npm")
+ , output = require("./utils/output")
+
+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 = []
+
+ // 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.
+
+ 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)
+}
+
View
31 npm-completion.sh
@@ -0,0 +1,31 @@
+#
+# npm command completion script
+#
+# Install Method 1: Automatic
+# Drop this file in /etc/bash-completion.d or /usr/local/etc/bash-completion.d
+# or wherever bash-completion scripts are sourced on your system.
+#
+# Install Method 2: Generic
+# Put this in your .bashrc or whatever file you run when you log into a machine:
+# . path/to/npm-completion.sh
+#
+# Then use the tab key to complete commands, which executes the "npm completion"
+# command.
+#
+# Note that command completion is very rudimentary and incomplete (harhar) as of
+# this time. Patches welcome!
+#
+# --i
+
+__npm_completion () {
+ COMPREPLY=()
+ local cur prev opts
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ prev="${COMP_WORDS[COMP_CWORD-1]}"
+ # opts=$(npm complete --loglevel silent --color false -- "$cur")
+ COMPREPLY=( $(COMP_CWORD=${COMP_CWORD} npm completion --loglevel silent --color false \
+ -- "${COMP_WORDS[@]}" ) )
+ return $?
+}
+
+complete -o default -o nospace -F __npm_completion npm
View
13 npm.js
@@ -77,28 +77,23 @@ var commandCache = {}
, "bundle"
, "outdated"
, "init"
+ , "completion"
]
- , fullList = cmdList.concat(aliasNames)
+ , fullList = npm.fullList = cmdList.concat(aliasNames)
, abbrevs = abbrev(fullList)
Object.keys(abbrevs).forEach(function (c) {
Object.defineProperty(npm.commands, c, { get : function () {
- var a = complete(c)
+ var a = npm.deref(c)
if (commandCache[a]) return commandCache[a]
return commandCache[a] = require(__dirname+"/lib/"+a)
}, enumerable: fullList.indexOf(c) !== -1 })
})
-function complete (c) {
+npm.deref = function (c) {
var a = abbrevs[c]
if (aliases[a]) a = aliases[a]
return a
}
-npm.commands.complete = function (args, cb_) {
- var a = complete(args[0])
- function cb (e) { cb_(e, a) }
- require("./lib/utils/output").write(npm.config.get("outfd"), a, cb)
-}
-
var loaded = false
npm.load = function (conf, cb_) {
if (!cb_ && typeof conf === "function") cb_ = conf , conf = {}

0 comments on commit cef89a2

Please sign in to comment.
Something went wrong with that request. Please try again.