Permalink
Browse files

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.
  • Loading branch information...
1 parent 8b4bb48 commit d04cf6483932c693452f3f778c2fa90f6153a4af @evanlucas evanlucas committed with domenic Dec 20, 2013
Showing with 48 additions and 26 deletions.
  1. +3 −1 doc/cli/npm-bugs.md
  2. +4 −1 doc/cli/npm-docs.md
  3. +1 −2 doc/cli/repo.md
  4. +39 −21 lib/bugs.js
  5. +1 −1 lib/docs.js
View
4 doc/cli/npm-bugs.md
@@ -4,12 +4,14 @@ npm-bugs(1) -- Bugs for a package in a web browser maybe
## SYNOPSIS
npm bugs <pkgname>
+ 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
View
5 doc/cli/npm-docs.md
@@ -4,13 +4,16 @@ npm-docs(1) -- Docs for a package in a web browser maybe
## SYNOPSIS
npm docs <pkgname>
+ npm docs (with no args in a package dir)
npm home <pkgname>
+ 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
View
3 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
View
60 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)
})
}
View
2 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')

0 comments on commit d04cf64

Please sign in to comment.