stars.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. const npmFetch = require('npm-registry-fetch')
  2. const { log, output } = require('proc-log')
  3. const getIdentity = require('../utils/get-identity.js')
  4. const BaseCommand = require('../base-cmd.js')
  5. class Stars extends BaseCommand {
  6. static description = 'View packages marked as favorites'
  7. static name = 'stars'
  8. static usage = ['[<user>]']
  9. static params = ['registry']
  10. static ignoreImplicitWorkspace = false
  11. async exec ([user]) {
  12. try {
  13. if (!user) {
  14. user = await getIdentity(this.npm, this.npm.flatOptions)
  15. }
  16. const { rows } = await npmFetch.json('/-/_view/starredByUser', {
  17. ...this.npm.flatOptions,
  18. query: { key: `"${user}"` },
  19. })
  20. if (rows.length === 0) {
  21. log.warn('stars', 'user has not starred any packages')
  22. }
  23. for (const row of rows) {
  24. output.standard(row.value)
  25. }
  26. } catch (err) {
  27. if (err.code === 'ENEEDAUTH') {
  28. log.warn('stars', 'auth is required to look up your username')
  29. }
  30. throw err
  31. }
  32. }
  33. }
  34. module.exports = Stars