docs.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. module.exports = docs
  2. var openUrl = require('./utils/open-url')
  3. var log = require('npmlog')
  4. var fetchPackageMetadata = require('./fetch-package-metadata.js')
  5. var usage = require('./utils/usage')
  6. docs.usage = usage(
  7. 'docs',
  8. 'npm docs <pkgname>' +
  9. '\nnpm docs .'
  10. )
  11. docs.completion = function (opts, cb) {
  12. // FIXME: there used to be registry completion here, but it stopped making
  13. // sense somewhere around 50,000 packages on the registry
  14. cb()
  15. }
  16. function docs (args, cb) {
  17. if (!args || !args.length) args = ['.']
  18. var pending = args.length
  19. log.silly('docs', args)
  20. args.forEach(function (proj) {
  21. getDoc(proj, function (err) {
  22. if (err) {
  23. return cb(err)
  24. }
  25. --pending || cb()
  26. })
  27. })
  28. }
  29. function getDoc (project, cb) {
  30. log.silly('getDoc', project)
  31. fetchPackageMetadata(project, '.', {fullMetadata: true}, function (er, d) {
  32. if (er) return cb(er)
  33. var url = d.homepage
  34. if (!url) url = 'https://www.npmjs.org/package/' + d.name
  35. return openUrl(url, 'docs available at the following URL', cb)
  36. })
  37. }