diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2014-06-21 11:27:31 +0400 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2014-07-02 05:43:16 +0400 |
commit | f34878fc4cee29901e4daf7bace94be01e25cad7 (patch) | |
tree | f23cd34ee04200231b47dc8b879229d564d2d8f4 /lib | |
parent | 1b6e84161cb1fc32bfc4dae49c4a7b5a2f0ffb3f (diff) |
username and email are now always scoped to a registry
Diffstat (limited to 'lib')
-rw-r--r-- | lib/owner.js | 139 | ||||
-rw-r--r-- | lib/publish.js | 14 | ||||
-rw-r--r-- | lib/stars.js | 16 | ||||
-rw-r--r-- | lib/unpublish.js | 60 |
4 files changed, 122 insertions, 107 deletions
diff --git a/lib/owner.js b/lib/owner.js index 1f9d79b25..2fdee7adb 100644 --- a/lib/owner.js +++ b/lib/owner.js @@ -5,6 +5,12 @@ owner.usage = "npm owner add <username> <pkg>" + "\nnpm owner rm <username> <pkg>" + "\nnpm owner ls <pkg>" +var npm = require("./npm.js") + , registry = npm.registry + , log = require("npmlog") + , readJson = require("read-package-json") + , mapToRegistry = require("./utils/map-to-registry.js") + owner.completion = function (opts, cb) { var argv = opts.conf.argv.remain if (argv.length > 4) return cb() @@ -14,79 +20,78 @@ owner.completion = function (opts, cb) { else subs.push("ls", "list") return cb(null, subs) } - var un = encodeURIComponent(npm.config.get("username")) - var byUser, theUser - switch (argv[2]) { - case "ls": - if (argv.length > 3) return cb() - return mapToRegistry("-/short", npm.config, function (er, uri) { - if (er) return cb(er) - - registry.get(uri, null, cb) - }) - case "rm": - if (argv.length > 3) { - theUser = encodeURIComponent(argv[3]) - byUser = "-/by-user/" + theUser + "|" + un - return mapToRegistry(byUser, npm.config, function (er, uri) { + npm.commands.whoami([], true, function (er, username) { + if (er) return cb() + + var un = encodeURIComponent(username) + var byUser, theUser + switch (argv[2]) { + case "ls": + if (argv.length > 3) return cb() + return mapToRegistry("-/short", npm.config, function (er, uri) { if (er) return cb(er) - console.error(uri) - registry.get(uri, null, function (er, d) { + registry.get(uri, null, cb) + }) + + case "rm": + if (argv.length > 3) { + theUser = encodeURIComponent(argv[3]) + byUser = "-/by-user/" + theUser + "|" + un + return mapToRegistry(byUser, npm.config, function (er, uri) { 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 - })) + + console.error(uri) + registry.get(uri, null, 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 fallthrough - case "add": - if (argv.length > 3) { - theUser = encodeURIComponent(argv[3]) - byUser = "-/by-user/"+theUser+"|"+un - return mapToRegistry(byUser, npm.config, function (er, uri) { + } + // else fallthrough + case "add": + if (argv.length > 3) { + theUser = encodeURIComponent(argv[3]) + byUser = "-/by-user/" + theUser + "|" + un + return mapToRegistry(byUser, npm.config, function (er, uri) { + if (er) return cb(er) + + console.error(uri) + registry.get(uri, null, 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 mapToRegistry("-/users", npm.config, function (er, uri) { if (er) return cb(er) - console.error(uri) - registry.get(uri, null, 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 + registry.get(uri, null, function (er, list) { + if (er) return cb() + return cb(null, Object.keys(list).filter(function (n) { + return n !== un })) }) }) - } - // just list all users who aren't me. - return mapToRegistry("-/users", npm.config, function (er, uri) { - if (er) return cb(er) - - registry.get(uri, null, function (er, list) { - if (er) return cb() - return cb(null, Object.keys(list).filter(function (n) { - return n !== un - })) - }) - }) - default: - return cb() - } + default: + return cb() + } + }) } -var npm = require("./npm.js") - , registry = npm.registry - , log = require("npmlog") - , readJson = require("read-package-json") - , mapToRegistry = require("./utils/map-to-registry.js") - function owner (args, cb) { var action = args.shift() switch (action) { @@ -115,7 +120,9 @@ function ls (pkg, cb) { } var owners = data.maintainers if (!owners || !owners.length) msg = "admin party!" - else msg = owners.map(function (o) { return o.name +" <"+o.email+">" }).join("\n") + else msg = owners.map(function (o) { + return o.name + " <" + o.email + ">" + }).join("\n") console.log(msg) cb(er, owners) }) @@ -137,7 +144,7 @@ function add (user, pkg, cb) { var o = owners[i] if (o.name === u.name) { log.info( "owner add" - , "Already a package owner: "+o.name+" <"+o.email+">") + , "Already a package owner: " + o.name + " <" + o.email + ">") return false } } @@ -162,7 +169,7 @@ function rm (user, pkg, cb) { return !match }) if (!found) { - log.info("owner rm", "Not a package owner: "+user) + log.info("owner rm", "Not a package owner: " + user) return false } if (!m.length) return new Error( @@ -185,7 +192,7 @@ function mutate (pkg, user, mutation, cb) { function mutate_ (er, u) { if (!er && user && (!u || u.error)) er = new Error( - "Couldn't get user data for "+user+": "+JSON.stringify(u)) + "Couldn't get user data for " + user + ": " + JSON.stringify(u)) if (er) { log.error("owner mutate", "Error getting user data for %s", user) @@ -214,7 +221,7 @@ function mutate (pkg, user, mutation, cb) { registry.request("PUT", uri, { body : data }, function (er, data) { if (!er && data.error) er = new Error( - "Failed to update package metadata: "+JSON.stringify(data)) + "Failed to update package metadata: " + JSON.stringify(data)) if (er) { log.error("owner mutate", "Failed to update package metadata") } @@ -235,5 +242,5 @@ function readLocalPkg (cb) { } function unknown (action, cb) { - cb("Usage: \n"+owner.usage) + cb("Usage: \n" + owner.usage) } diff --git a/lib/publish.js b/lib/publish.js index 349ac00a7..833cab5de 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -24,7 +24,10 @@ publish.completion = function (opts, cb) { } function publish (args, isRetry, cb) { - if (typeof cb !== "function") cb = isRetry, isRetry = false + if (typeof cb !== "function") { + cb = isRetry + isRetry = false + } if (args.length === 0) args = ["."] if (args.length !== 1) return cb(publish.usage) @@ -56,8 +59,8 @@ function cacheAddPublish (dir, didPre, isRetry, cb) { , data.name , data.version , "package" ) - chain - ( [ !didPre && [lifecycle, data, "prepublish", cachedir] + chain([ !didPre && + [lifecycle, data, "prepublish", cachedir] , [publish_, dir, data, isRetry, cachedir] , [lifecycle, data, "publish", didPre ? dir : cachedir] , [lifecycle, data, "postpublish", didPre ? dir : cachedir] ] @@ -86,8 +89,6 @@ function publish_ (arg, data, isRetry, cachedir, cb) { } data._npmVersion = npm.version - data._npmUser = { name: npm.config.get("username") - , email: npm.config.get("email") } delete data.modules if (data.private) return cb( @@ -106,6 +107,9 @@ function publish_ (arg, data, isRetry, cachedir, cb) { var registryBase = url.resolve(registryURI, ".") log.verbose("publish", "registryBase", registryBase) + var c = config.getCredentialsByURI(registryBase) + data._npmUser = {name: c.username, email: c.email} + registry.publish(registryBase, data, tarball, function (er) { if (er && er.code === "EPUBLISHCONFLICT" && npm.config.get("force") && !isRetry) { diff --git a/lib/stars.js b/lib/stars.js index a482a4b4a..dee5c152a 100644 --- a/lib/stars.js +++ b/lib/stars.js @@ -8,20 +8,20 @@ var npm = require("./npm.js") , mapToRegistry = require("./utils/map-to-registry.js") function stars (args, cb) { - var name = args.length === 1 ? args[0] : npm.config.get("username") - mapToRegistry(name, npm.config, function (er, uri) { - if (er) return cb(er) + npm.commands.whoami([], true, function (er, username) { + var name = args.length === 1 ? args[0] : username + mapToRegistry("", npm.config, function (er, uri) { + if (er) return cb(er) - registry.stars(uri, showstars) + registry.stars(uri, name, showstars) + }) }) function showstars (er, data) { - if (er) { - return cb(er) - } + if (er) return cb(er) if (data.rows.length === 0) { - log.warn('stars', 'user has not starred any packages.') + log.warn("stars", "user has not starred any packages.") } else { data.rows.forEach(function(a) { console.log(a.value) diff --git a/lib/unpublish.js b/lib/unpublish.js index cd4f18deb..f4bb05f10 100644 --- a/lib/unpublish.js +++ b/lib/unpublish.js @@ -12,34 +12,38 @@ unpublish.usage = "npm unpublish <project>[@<version>]" unpublish.completion = function (opts, cb) { if (opts.conf.argv.remain.length >= 3) return cb() - var un = encodeURIComponent(npm.config.get("username")) - if (!un) return cb() - var byUser = "-/by-user/" + un - mapToRegistry(byUser, npm.config, function (er, uri) { - if (er) return cb(er) + npm.commands.whoami([], true, function (er, username) { + if (er) return cb() - registry.get(uri, null, function (er, pkgs) { - // do a bit of filtering at this point, so that we don't need - // to fetch versions for more than one thing, but also don't - // accidentally a whole project. - pkgs = pkgs[un] - if (!pkgs || !pkgs.length) return cb() - var partial = opts.partialWord.split("@") - , pp = partial.shift() - pkgs = pkgs.filter(function (p) { - return p.indexOf(pp) === 0 - }) - if (pkgs.length > 1) return cb(null, pkgs) - mapToRegistry(pkgs[0], npm.config, function (er, uri) { - if (er) return cb(er) + var un = encodeURIComponent(username) + if (!un) return cb() + var byUser = "-/by-user/" + un + mapToRegistry(byUser, npm.config, function (er, uri) { + if (er) return cb(er) - registry.get(uri, null, function (er, d) { + registry.get(uri, null, function (er, pkgs) { + // do a bit of filtering at this point, so that we don't need + // to fetch versions for more than one thing, but also don't + // accidentally a whole project. + pkgs = pkgs[un] + if (!pkgs || !pkgs.length) return cb() + var partial = opts.partialWord.split("@") + , pp = partial.shift() + pkgs = pkgs.filter(function (p) { + return p.indexOf(pp) === 0 + }) + if (pkgs.length > 1) return cb(null, pkgs) + mapToRegistry(pkgs[0], npm.config, function (er, uri) { if (er) return cb(er) - var vers = Object.keys(d.versions) - if (!vers.length) return cb(null, pkgs) - return cb(null, vers.map(function (v) { - return pkgs[0]+"@"+v - })) + + registry.get(uri, null, function (er, d) { + if (er) return cb(er) + var vers = Object.keys(d.versions) + if (!vers.length) return cb(null, pkgs) + return cb(null, vers.map(function (v) { + return pkgs[0] + "@" + v + })) + }) }) }) }) @@ -55,8 +59,8 @@ function unpublish (args, cb) { if (!version && !npm.config.get("force")) { return cb("Refusing to delete entire project.\n" - +"Run with --force to do this.\n" - +unpublish.usage) + + "Run with --force to do this.\n" + + unpublish.usage) } if (!project || path.resolve(project) === npm.prefix) { @@ -65,7 +69,7 @@ function unpublish (args, cb) { var cwdJson = path.join(process.cwd(), "package.json") return readJson(cwdJson, function (er, data) { if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er) - if (er) return cb("Usage:\n"+unpublish.usage) + if (er) return cb("Usage:\n" + unpublish.usage) gotProject(data.name, data.version, cb) }) } |