stars.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict'
  2. const BB = require('bluebird')
  3. const npmConfig = require('./config/figgy-config.js')
  4. const fetch = require('libnpm/fetch')
  5. const log = require('npmlog')
  6. const output = require('./utils/output.js')
  7. const whoami = require('./whoami.js')
  8. stars.usage = 'npm stars [<user>]'
  9. module.exports = stars
  10. function stars ([user], cb) {
  11. const opts = npmConfig()
  12. return BB.try(() => {
  13. return (user ? BB.resolve(user) : whoami([], true, () => {})).then(usr => {
  14. return fetch.json('/-/_view/starredByUser', opts.concat({
  15. query: {key: `"${usr}"`} // WHY. WHY THE ""?!
  16. }))
  17. }).then(data => data.rows).then(stars => {
  18. if (stars.length === 0) {
  19. log.warn('stars', 'user has not starred any packages.')
  20. } else {
  21. stars.forEach(s => output(s.value))
  22. }
  23. })
  24. }).catch(err => {
  25. if (err.code === 'ENEEDAUTH') {
  26. throw Object.assign(new Error("'npm stars' on your own user account requires auth"), {
  27. code: 'ENEEDAUTH'
  28. })
  29. } else {
  30. throw err
  31. }
  32. }).nodeify(cb)
  33. }