diff options
author | Evan Lucas <evanlucas@me.com> | 2014-07-25 21:07:46 +0400 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2014-09-21 11:26:18 +0400 |
commit | 36356011b6f2e6a5a81490e85a0a44eb27199dd7 (patch) | |
tree | 95c25071673b6f4db07a49e47005c62f161c81cd /lib | |
parent | b90a91a6d2ea962f39d018eb2c6d745d4bfdb2b3 (diff) |
add `npm view .`
Fixes #5520
This adds back the ability to run `npm view .`
Includes multiple tests for `npm view`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/view.js | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/lib/view.js b/lib/view.js index 43d09cbbc..3bae2ab42 100644 --- a/lib/view.js +++ b/lib/view.js @@ -48,24 +48,54 @@ view.completion = function (opts, cb) { } var npm = require("./npm.js") + , readJson = require("read-package-json") , registry = npm.registry , log = require("npmlog") , util = require("util") , semver = require("semver") , mapToRegistry = require("./utils/map-to-registry.js") , npa = require("npm-package-arg") + , path = require('path') function view (args, silent, cb) { if (typeof cb !== "function") cb = silent, silent = false - if (!args.length) return cb("Usage: "+view.usage) + + if (npm.config.get("global") === true) { + return cb(new Error("Cannot use view command in global mode.")) + } + + if (!args.length) args = ["."] + var pkg = args.shift() , nv = npa(pkg) , name = nv.name - , version = nv.rawSpec || npm.config.get("tag") + , local = (name === "." || !name) + + if (local) { + var dir = npm.prefix + readJson(path.resolve(dir, "package.json"), function (er, d) { + d = d || {} + if (er && er.code !== "ENOENT" && er.code !== "ENOTDIR") return cb(er) + if (!d.name) return cb(new Error("Invalid package.json")) + + var p = d.name + nv = npa(p) + if (pkg && ~pkg.indexOf("@")) { + nv.rawSpec = pkg.split("@")[pkg.indexOf("@")] + } - if (name === ".") return cb(view.usage) + fetchAndRead(nv, args, silent, cb) + }) + } else { + fetchAndRead(nv, args, silent, cb) + } +} +function fetchAndRead (nv, args, silent, cb) { // get the data about this package + var name = nv.name + , version = nv.rawSpec || npm.config.get("tag") + mapToRegistry(name, npm.config, function (er, uri) { if (er) return cb(er) @@ -268,4 +298,3 @@ function unparsePerson (d) { + (d.email ? " <"+d.email+">" : "") + (d.url ? " ("+d.url+")" : "") } - |