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

Commit

Permalink
Allow npm bugs for the current directory
Browse files Browse the repository at this point in the history
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
evanlucas authored and domenic committed Dec 23, 2013
1 parent 8b4bb48 commit d04cf64
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
4 changes: 3 additions & 1 deletion doc/cli/npm-bugs.md
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion doc/cli/npm-docs.md
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions doc/cli/repo.md
Expand Up @@ -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

Expand Down
60 changes: 39 additions & 21 deletions lib/bugs.js
Expand Up @@ -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()
Expand All @@ -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)
})
}
2 changes: 1 addition & 1 deletion lib/docs.js
Expand Up @@ -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')
Expand Down

0 comments on commit d04cf64

Please sign in to comment.