Skip to content

Commit

Permalink
Increase tab-completion coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
evnm authored and isaacs committed Dec 2, 2010
1 parent c182cff commit 299a9a4
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 27 deletions.
5 changes: 5 additions & 0 deletions lib/activate.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ module.exports = activate
activate.usage =
"npm activate <name>@<version> [<name>@<version> ...]"

activate.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, true, cb)
}

function activate (args, cb) {
// make sure package and version exists.
// If there's already an active version, then deactivate it.
Expand Down
13 changes: 13 additions & 0 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ cache.usage = "npm cache add <tarball file>"
+ "\nnpm cache ls [<path>]"
+ "\nnpm cache clean [<pkg>[@<version>]]"

cache.completion = function(args, index, cb) {
var remotePkgs = require("./utils/completion/remote-packages")
, getCompletions = require("./utils/completion/get-completions")
, subcmdList = ["add", "clean", "ls"]
, subcmd = args[0] || ""

if (subcmdList.indexOf(subcmd) !== -1) {
if (subcmd === "add") {
remotePkgs(args.slice(1), index - 1, true, false, false, cb)
}
} else cb(null, getCompletions(subcmd, subcmdList))
}

function cache (args, cb) {
var cmd = args.shift()
switch (cmd) {
Expand Down
41 changes: 22 additions & 19 deletions lib/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 14 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ config.usage = "npm config set <key> <value>"
+ "\nnpm config list"
+ "\nnpm config edit"

config.completion = function(args, index, cb) {
var getCompletions = require("./utils/completion/get-completions")
, subcmdList = ["set", "get", "ls", "delete", "edit"]
, takeKeys = ["set", "get", "delete"]
, subcmd = args[0] || ""
, key = args[1] || ""

if (subcmdList.indexOf(subcmd) !== -1) {
if (takeKeys.indexOf(subcmd) !== -1 && ini.keys.indexOf(key) === -1) {
cb(null, getCompletions(key, ini.keys))
}
} else cb(null, getCompletions(subcmd, subcmdList))
}

var ini = require("./utils/ini")
, log = require("./utils/log")
, npm = require("../npm")
Expand Down
13 changes: 9 additions & 4 deletions lib/deactivate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@

module.exports = deactivate

deactivate.usage = "npm deactivate <pkg>"

deactivate.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, false, false, cb)
}

var mkdir = require("./utils/mkdir-p")
, npm = require("../npm")
, fs = require("./utils/graceful-fs")
Expand All @@ -11,10 +20,6 @@ var mkdir = require("./utils/mkdir-p")
, asyncMap = require("./utils/async-map")
, loadPackageDefaults = require("./utils/load-package-defaults")

module.exports = deactivate

deactivate.usage = "npm deactivate <pkg>"

function deactivate (args, cb) {
var rb = npm.ROLLBACK
npm.ROLLBACK = true
Expand Down
5 changes: 5 additions & 0 deletions lib/deprecate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ module.exports = deprecate

deprecate.usage = "npm deprecate <pkg>[@<version>] <message>"

deprecate.completion = function (args, index, cb) {
var remotePkgs = require("./utils/completion/remote-packages")
remotePkgs(args, index, true, false, false, cb)
}

var registry = require("./utils/registry")
, semver = require("./utils/semver")
, log = require("./utils/log")
Expand Down
5 changes: 5 additions & 0 deletions lib/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
module.exports = edit
edit.usage = "npm edit <pkg>[@<version>]"

edit.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, false, cb)
}

var npm = require("../npm")
, exec = require("./utils/exec")
, path = require("path")
Expand Down
16 changes: 14 additions & 2 deletions lib/help.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@

module.exports = help

help.completion = function (args, index, cb) {
if (index < 3) {
var getCompletions = require("./utils/completion/get-completions")
, section = args[0] || ""

getSections(function (er, sectionList) {
if (er) return cb(er)
cb(null, getCompletions(section, sectionList))
})
}
}

var fs = require("./utils/graceful-fs")
, path = require("path")
, exec = require("./utils/exec")
, npm = require("../npm")
, output = require("./utils/output")

module.exports = help

function help (args, cb) {
var section = args.shift()
if (section === "help") {
Expand Down
5 changes: 5 additions & 0 deletions lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ install.usage = "npm install <tarball file>"
+ "\n\nCan specify one or more: npm install ./foo.tgz bar@stable /some/folder"
+ "\nInstalls '.' if no argument supplied"

install.completion = function (args, index, cb) {
var remotePkgs = require("./utils/completion/remote-packages")
remotePkgs(args, index, true, true, false, cb)
}

var registry = require("./utils/registry")
, npm = require("../npm")
, readInstalled = require("./utils/read-installed")
Expand Down
5 changes: 5 additions & 0 deletions lib/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ module.exports = outdated

outdated.usage = "npm outdated [<pkg> [<pkg> ...]]"

outdated.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, false, true, cb)
}

var readInstalled = require("./utils/read-installed")
, chain = require("./utils/chain")
, log = require("./utils/log")
Expand Down
15 changes: 15 additions & 0 deletions lib/owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ owner.usage = "npm owner add <username> <pkg>"
+ "\nnpm owner rm <username> <pkg>"
+ "\nnpm owner ls <pkg>"

owner.completion = function(args, index, cb) {
var remotePkgs = require("./utils/completion/remote-packages")
, getCompletions = require("./utils/completion/get-completions")
, subcmdList = ["add", "ls", "rm"]
, subcmd = args[0] || ""

if (subcmdList.indexOf(subcmd) !== -1) {
if (subcmd === "ls") {
remotePkgs(args.slice(1), index - 1, false, false, false, cb)
} else if (index == 4 && (subcmd === "add" || subcmd === "rm")) {
remotePkgs(args.slice(2), index - 2, false, false, false, cb)
}
} else cb(null, getCompletions(subcmd, subcmdList))
}

var registry = require("./utils/registry")
, get = registry.request.GET
, put = registry.request.PUT
Expand Down
5 changes: 5 additions & 0 deletions lib/rebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ var readInstalled = require("./utils/read-installed")

rebuild.usage = "npm rebuild [<name>[@<version>] [name[@<version>] ...]]"

rebuild.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, true, cb)
}

function rebuild (args, cb) {
log.verbose(args, "rebuild")
lookupArgs(args, function (er, args) {
Expand Down
5 changes: 5 additions & 0 deletions lib/restart.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ var lifecycle = require("./utils/lifecycle")

restart.usage = "npm restart <name>[@<version>] [<name>[@<version>] ...]"

restart.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, true, cb)
}

function restart (args, cb) {
restartCmd(args, function (er) {
if (!er) return cb()
Expand Down
6 changes: 5 additions & 1 deletion lib/start.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@


module.exports = require("./utils/lifecycle").cmd("start")
module.exports.usage = "npm start <name>[@<version>] [<name>[@<version>] ...]"
module.exports.usage = "npm start <name>[@<version>] [<name>[@<version>] ...]"
module.exports.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, true, cb)
}
4 changes: 4 additions & 0 deletions lib/stop.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

module.exports = require("./utils/lifecycle").cmd("stop")
module.exports.usage = "npm stop <name>[@<version>] [<name>[@<version>] ...]"
module.exports.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, true, cb)
}
5 changes: 5 additions & 0 deletions lib/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
module.exports = tag
tag.usage = "npm tag <project>@<version> <tag>"

tag.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, false, cb)
}

function tag (args, cb) {
var thing = (args.shift() || "").split("@")
, project = thing.shift()
Expand Down
4 changes: 4 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

module.exports = require("./utils/lifecycle").cmd("test")
module.exports.usage = "npm test <name>[@<version>] [<name>[@<version>] ...]"
module.exports.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, true, cb)
}
6 changes: 5 additions & 1 deletion lib/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module.exports = uninstall
uninstall.usage = "npm uninstall <name>[@<version> [<name>[@<version>] ...]"
+ "\nnpm rm <name>[@<version> [<name>[@<version>] ...]"

uninstall.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, true, true, cb)
}

var rm = require("./utils/rm-rf")
, fs = require("./utils/graceful-fs")
, log = require("./utils/log")
Expand All @@ -17,7 +22,6 @@ var rm = require("./utils/rm-rf")
, chain = require("./utils/chain")
, lifecycle = require("./utils/lifecycle")
, semver = require("./utils/semver")
, readInstalled = require("./utils/read-installed")
, mkdir = require("./utils/mkdir-p")
, asyncMap = require("./utils/async-map")
, loadPackageDefaults = require("./utils/load-package-defaults")
Expand Down
5 changes: 5 additions & 0 deletions lib/unpublish.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ var registry = require("./utils/registry")

unpublish.usage = "npm unpublish <project>[@<version>]"

unpublish.completion = function (args, index, cb) {
var remotePkgs = require("./utils/completion/remote-packages")
remotePkgs(args, index, true, false, false, cb)
}

function unpublish (args, cb) {
var thing = args.shift().split("@")
, project = thing.shift()
Expand Down
5 changes: 5 additions & 0 deletions lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ module.exports = update

update.usage = "npm update [pkg]"

update.completion = function (args, index, cb) {
var installedPkgs = require("./utils/completion/installed-packages")
installedPkgs(args, index, false, true, cb)
}

var chain = require("./utils/chain")
, log = require("./utils/log")
, npm = require("../npm")
Expand Down
29 changes: 29 additions & 0 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
module.exports = view
view.usage = "npm view pkg[@version] [<field>[.subfield]...]"

view.completion = function (args, index, cb) {
var registry = require("./utils/registry")
, getCompletions = require("./utils/completion/get-completions")
, remotePkgs = require("./utils/completion/remote-packages")
, name = args[0] || ""
, key = args[1] || ""

if (index < 3) remotePkgs(args, index, true, false, false, cb)
else if (index == 3) {
var pieces = name.split("@")
registry.get(pieces[0], pieces[1], function (er, d) {
var cur = d.versions ? d.versions[d["dist-tags"].latest] : d
, path = key.split(".")
, options
if (path.length > 1) {
for (var i = 0; i < path.length; i++) {
if (!cur) return cb()
if (!cur[path[i]]) break
else cur = cur[path[i]]
}
options = Object.keys(cur).map(function (e) {
return path.slice(0, path.length - 1).join(".") + "." + e
})
} else options = Object.keys(cur).concat(["ctime","mtime","maintainers"])
cb(null, getCompletions(key, options))
})
}
}

var registry = require("./utils/registry")
, ini = require("./utils/ini-parser")
, log = require("./utils/log")
Expand Down

0 comments on commit 299a9a4

Please sign in to comment.