diff options
author | Evan Meagher <evan.meagher@gmail.com> | 2010-11-27 22:54:55 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-12-03 01:33:18 +0300 |
commit | c182cff972173a8b5e212bea5abe9445c1ff7c36 (patch) | |
tree | 33aa98875295ce9306aecd28fdc0a044fed1f6b6 /lib | |
parent | bd4677f2e1a827e403366603e93a70d127eac3f1 (diff) |
Add new utils used by completion.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/utils/completion/contains-single-match.js | 8 | ||||
-rw-r--r-- | lib/utils/completion/get-completions.js | 17 | ||||
-rw-r--r-- | lib/utils/completion/installed-packages.js | 36 | ||||
-rw-r--r-- | lib/utils/completion/remote-packages.js | 44 |
4 files changed, 105 insertions, 0 deletions
diff --git a/lib/utils/completion/contains-single-match.js b/lib/utils/completion/contains-single-match.js new file mode 100644 index 000000000..5f22c2515 --- /dev/null +++ b/lib/utils/completion/contains-single-match.js @@ -0,0 +1,8 @@ + +module.exports = containsSingleMatch + +// True if arr contains only one element starting with str. +function containsSingleMatch(str, arr) { + var filtered = arr.filter(function(e) { return e.indexOf(str) === 0 }) + return filtered.length === 1 +}
\ No newline at end of file diff --git a/lib/utils/completion/get-completions.js b/lib/utils/completion/get-completions.js new file mode 100644 index 000000000..29f3fa08d --- /dev/null +++ b/lib/utils/completion/get-completions.js @@ -0,0 +1,17 @@ + +module.exports = getCompletions + +var npm = require("../../../npm") + +// Returns all completions of str within list. +// NOTE: isCmd predicates whether or not to deref str. +function getCompletions (str, list, isCmd) { + var result = [] + list.forEach(function (f) { + var a = isCmd ? npm.deref(f) : f + if (result.indexOf(a) === -1 && f.indexOf(str) === 0) { + result.push(a) + } + }) + return result +} diff --git a/lib/utils/completion/installed-packages.js b/lib/utils/completion/installed-packages.js new file mode 100644 index 000000000..3abb7d29e --- /dev/null +++ b/lib/utils/completion/installed-packages.js @@ -0,0 +1,36 @@ + +module.exports = installedPackages + +var containsSingleMatch = require("./contains-single-match") + , getCompletions = require("./get-completions") + , readInstalled = require("../read-installed") + +/* + Looks up installed packages for CLI tab-completion. + + NOTE: If doVersion is true, versions in the form <name>@<version> + will be completed. + + If recurring in true, sequences of multiple packages can be + completed. i.e. for schemes such as: + npm <command> <name>[@<version> [<name>[@<version>] ...] +*/ +function installedPackages (args, index, doVersion, recurring, cb) { + if (recurring || index < 3) { + var name = (args.length + 1 == index) ? args[args.length - 1] : "" + readInstalled([], function (er, installed) { + if (er) return cb(er) + var instList = Object.keys(installed) + if (instList.indexOf(name) === -1 || + !containsSingleMatch(name, instList)) { + if (doVersion && name.indexOf('@') !== -1) { + var pieces = name.split('@') + , nameverList = Object.keys(installed[pieces[0]]).map(function(v) { + return pieces[0] + '@' + v + }) + cb(null, getCompletions(name, nameverList)) + } else cb(null, getCompletions(name, instList)) + } + }) + } +} diff --git a/lib/utils/completion/remote-packages.js b/lib/utils/completion/remote-packages.js new file mode 100644 index 000000000..94fd12ac4 --- /dev/null +++ b/lib/utils/completion/remote-packages.js @@ -0,0 +1,44 @@ + +module.exports = remotePackages + +var registry = require("../registry") + , containsSingleMatch = require("./contains-single-match") + , getCompletions = require("./get-completions") + +/* + Looks up remote packages for CLI tab-completion. + + NOTE: If doVersion is true, versions in the form <name>@<version> + will be completed. + + If doTag is true, tags in the form <name>@<tag> will be + completed. + + If recurring in true, sequences of multiple packages can be + completed. i.e. for schemes such as: + npm <command> <name>[@<version> [<name>[@<version>] ...] +*/ +function remotePackages (args, index, doVersion, doTag + , recurring, cb) { + if (recurring || index < 3) { + var name = (args.length + 1 == index) ? args[args.length - 1] : "" + registry.get("/", function (er, d) { + if (er) return cb(er) + var remoteList = Object.keys(d) + if (remoteList.indexOf(name) === -1 || + !containsSingleMatch(name, remoteList)) { + if ((doVersion || doTag) && name.indexOf('@') !== -1) { + var pieces = name.split('@') + , pkgname = pieces[0] + , lst = [] + if (d[pkgname]) { + if (doVersion) lst = lst.concat(Object.keys(d[pkgname].versions)) + if (doTag) lst = lst.concat(Object.keys(d[pkgname]["dist-tags"])) + } + lst = lst.map(function(e) { return pkgname + '@' + e }) + cb(null, getCompletions(name, lst)) + } else cb(null, getCompletions(name, remoteList)) + } + }) + } +} |