stars.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const log = require('npmlog')
  2. const fetch = require('npm-registry-fetch')
  3. const getIdentity = require('./utils/get-identity.js')
  4. const BaseCommand = require('./base-command.js')
  5. class Stars extends BaseCommand {
  6. /* istanbul ignore next - see test/lib/load-all-commands.js */
  7. static get description () {
  8. return 'View packages marked as favorites'
  9. }
  10. /* istanbul ignore next - see test/lib/load-all-commands.js */
  11. static get name () {
  12. return 'stars'
  13. }
  14. /* istanbul ignore next - see test/lib/load-all-commands.js */
  15. static get usage () {
  16. return ['[<user>]']
  17. }
  18. /* istanbul ignore next - see test/lib/load-all-commands.js */
  19. static get params () {
  20. return [
  21. 'registry',
  22. ]
  23. }
  24. exec (args, cb) {
  25. this.stars(args).then(() => cb()).catch(er => {
  26. if (er.code === 'ENEEDAUTH')
  27. log.warn('stars', 'auth is required to look up your username')
  28. cb(er)
  29. })
  30. }
  31. async stars ([user]) {
  32. if (!user)
  33. user = await getIdentity(this.npm, this.npm.flatOptions)
  34. const { rows } = await fetch.json('/-/_view/starredByUser', {
  35. ...this.npm.flatOptions,
  36. query: { key: `"${user}"` },
  37. })
  38. if (rows.length === 0)
  39. log.warn('stars', 'user has not starred any packages')
  40. for (const row of rows)
  41. this.npm.output(row.value)
  42. }
  43. }
  44. module.exports = Stars