From d04cf6483932c693452f3f778c2fa90f6153a4af Mon Sep 17 00:00:00 2001 From: Evan Lucas Date: Fri, 20 Dec 2013 01:45:26 -0600 Subject: [PATCH] Allow npm bugs for the current directory If you are in your local module, you might want to visit the repository without knowing the repo name or typing the name in. This adds `npm bugs .` and `npm bugs` so that the npm-bugs-command behaves like `npm install`, `npm docs`, `npm home`, and `npm repo.` Also fixed the parsing of `./` (instead of `.`). Closes #4204. --- doc/cli/npm-bugs.md | 4 ++- doc/cli/npm-docs.md | 5 +++- doc/cli/repo.md | 3 +-- lib/bugs.js | 60 +++++++++++++++++++++++++++++---------------- lib/docs.js | 2 +- 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/doc/cli/npm-bugs.md b/doc/cli/npm-bugs.md index 2a7dae98db4..002d9f7556f 100644 --- a/doc/cli/npm-bugs.md +++ b/doc/cli/npm-bugs.md @@ -4,12 +4,14 @@ npm-bugs(1) -- Bugs for a package in a web browser maybe ## SYNOPSIS npm bugs + npm bugs (with no args in a package dir) ## DESCRIPTION This command tries to guess at the likely location of a package's bug tracker URL, and then tries to open it using the `--browser` -config param. +config param. If no package name is provided, it will search for +a `package.json` in the current folder and use the `name` property. ## CONFIGURATION diff --git a/doc/cli/npm-docs.md b/doc/cli/npm-docs.md index cc39e82c497..cece021f110 100644 --- a/doc/cli/npm-docs.md +++ b/doc/cli/npm-docs.md @@ -4,13 +4,16 @@ npm-docs(1) -- Docs for a package in a web browser maybe ## SYNOPSIS npm docs + npm docs (with no args in a package dir) npm home + npm home (with no args in a package dir) ## DESCRIPTION This command tries to guess at the likely location of a package's documentation URL, and then tries to open it using the `--browser` -config param. +config param. If no package name is provided, it will search for +a `package.json` in the current folder and use the `name` property. ## CONFIGURATION diff --git a/doc/cli/repo.md b/doc/cli/repo.md index 12085c10156..6bc6f3b3758 100644 --- a/doc/cli/repo.md +++ b/doc/cli/repo.md @@ -11,8 +11,7 @@ npm-repo(1) -- Open package repository page in the browser This command tries to guess at the likely location of a package's repository URL, and then tries to open it using the `--browser` config param. If no package name is provided, it will search for -a `package.json` in the current folder and try to use the property -of the name field. +a `package.json` in the current folder and use the `name` property. ## CONFIGURATION diff --git a/lib/bugs.js b/lib/bugs.js index bcbf2bebb10..604748a97b4 100644 --- a/lib/bugs.js +++ b/lib/bugs.js @@ -7,6 +7,9 @@ var npm = require("./npm.js") , registry = npm.registry , log = require("npmlog") , opener = require("opener") + , path = require("path") + , readJson = require("read-package-json") + , fs = require("fs") bugs.completion = function (opts, cb) { if (opts.conf.argv.remain.length > 2) return cb() @@ -16,28 +19,43 @@ bugs.completion = function (opts, cb) { } function bugs (args, cb) { - if (!args.length) return cb(bugs.usage) - var n = args[0].split("@").shift() + var n = args.length && args[0].split("@").shift() || '.' + fs.stat(n, function (er, s) { + if (er && er.code === "ENOENT") return callRegistry(n, cb) + else if (er) return cb (er) + if (!s.isDirectory()) return callRegistry(n, cb) + readJson(path.resolve(n, "package.json"), function(er, d) { + if (er) return cb(err) + getUrlAndOpen(d, cb) + }) + }) +} + +function getUrlAndOpen (d, cb) { + var bugs = d.bugs + , repo = d.repository || d.repositories + , url + if (bugs) { + url = (typeof url === "string") ? bugs : bugs.url + } else if (repo) { + if (Array.isArray(repo)) repo = repo.shift() + if (repo.hasOwnProperty("url")) repo = repo.url + log.verbose("repository", repo) + if (bugs && bugs.match(/^(https?:\/\/|git(:\/\/|@))github.com/)) { + url = bugs.replace(/^git(@|:\/\/)/, "https://") + .replace(/^https?:\/\/github.com:/, "https://github.com/") + .replace(/\.git$/, '')+"/issues" + } + } + if (!url) { + url = "https://npmjs.org/package/" + d.name + } + opener(url, { command: npm.config.get("browser") }, cb) +} + +function callRegistry (n, cb) { registry.get(n + "/latest", 3600, function (er, d) { if (er) return cb(er) - var bugs = d.bugs - , repo = d.repository || d.repositories - , url - if (bugs) { - url = (typeof bugs === "string") ? bugs : bugs.url - } else if (repo) { - if (Array.isArray(repo)) repo = repo.shift() - if (repo.hasOwnProperty("url")) repo = repo.url - log.verbose("repository", repo) - if (repo && repo.match(/^(https?:\/\/|git(:\/\/|@))github.com/)) { - url = repo.replace(/^git(@|:\/\/)/, "https://") - .replace(/^https?:\/\/github.com:/, "https://github.com/") - .replace(/\.git$/, '')+"/issues" - } - } - if (!url) { - url = "https://npmjs.org/package/" + d.name - } - opener(url, { command: npm.config.get("browser") }, cb) + getUrlAndOpen (d, cb) }) } diff --git a/lib/docs.js b/lib/docs.js index b48646a56fc..0f59572ac8e 100644 --- a/lib/docs.js +++ b/lib/docs.js @@ -25,7 +25,7 @@ function docs (args, cb) { var project = args[0] || '.' , package = path.resolve(process.cwd(), "package.json") - if (project === '.') { + if (project === '.' || project === './') { try { var json = require(package) if (!json.name) throw new Error('package.json does not have a valid "name" property')