Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
completion for owner command
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Mar 21, 2011
1 parent 78e3a26 commit eecfd3c
Showing 1 changed file with 55 additions and 16 deletions.
71 changes: 55 additions & 16 deletions lib/owner.js
Expand Up @@ -5,24 +5,63 @@ 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] || ""
, users = require("./utils/completion/users")
owner.completion = function (opts, cb) {
var argv = opts.conf.argv.remain
if (argv.length > 4) return cb()
if (argv.length <= 2) {
var subs = ["add", "rm"]
if (opts.partialWord === "l") subs.push("ls")
else subs.push("ls", "list")
return cb(null, subs)
}
var un = encodeURIComponent(npm.config.get("username"))
switch (argv[2]) {
case "ls":
if (argv.length > 3) return cb()
else return registry.get("/-/short", cb)

if (subcmdList.indexOf(subcmd) !== -1) {
if (subcmd === "ls") {
remotePkgs(args.slice(1), index - 1, false, false, false, cb)
} else if (subcmd === "add" || subcmd === "rm") {
if (index === 4) {
remotePkgs(args.slice(2), index - 2, false, false, false, cb)
} else {
users(args.slice(1), index - 1, cb)
case "rm":
if (argv.length > 3) {
var theUser = encodeURIComponent(argv[3])
, uri = "/-/by-user/"+theUser+"|"+un
console.error(uri)
return registry.get(uri, function (er, d) {
if (er) return cb(er)
// return the intersection
return cb(null, d[theUser].filter(function (p) {
// kludge for server adminery.
return un === "isaacs" || d[un].indexOf(p) === -1
}))
})
}
}
} else cb(null, getCompletions(subcmd, subcmdList))
// else fallthrough
case "add":
if (argv.length > 3) {
var theUser = encodeURIComponent(argv[3])
, uri = "/-/by-user/"+theUser+"|"+un
console.error(uri)
return registry.get(uri, function (er, d) {
console.error(uri, er || d)
// return mine that they're not already on.
if (er) return cb(er)
var mine = d[un] || []
, theirs = d[theUser] || []
return cb(null, mine.filter(function (p) {
return theirs.indexOf(p) === -1
}))
})
}
// just list all users who aren't me.
return registry.get("/-/users", function (er, list) {
if (er) return cb()
return cb(null, Object.keys(list).filter(function (n) {
return n !== un
}))
})

default:
return cb()
}
}

var registry = require("./utils/npm-registry-client")
Expand Down

0 comments on commit eecfd3c

Please sign in to comment.