star.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const npmFetch = require('npm-registry-fetch')
  2. const npa = require('npm-package-arg')
  3. const { log, output } = require('proc-log')
  4. const getIdentity = require('../utils/get-identity')
  5. const BaseCommand = require('../base-cmd.js')
  6. class Star extends BaseCommand {
  7. static description = 'Mark your favorite packages'
  8. static name = 'star'
  9. static usage = ['[<package-spec>...]']
  10. static params = [
  11. 'registry',
  12. 'unicode',
  13. 'otp',
  14. ]
  15. static ignoreImplicitWorkspace = false
  16. async exec (args) {
  17. if (!args.length) {
  18. throw this.usageError()
  19. }
  20. // if we're unstarring, then show an empty star image
  21. // otherwise, show the full star image
  22. const unicode = this.npm.config.get('unicode')
  23. const full = unicode ? '\u2605 ' : '(*)'
  24. const empty = unicode ? '\u2606 ' : '( )'
  25. const show = this.name === 'star' ? full : empty
  26. const pkgs = args.map(npa)
  27. const username = await getIdentity(this.npm, this.npm.flatOptions)
  28. for (const pkg of pkgs) {
  29. const fullData = await npmFetch.json(pkg.escapedName, {
  30. ...this.npm.flatOptions,
  31. spec: pkg,
  32. query: { write: true },
  33. preferOnline: true,
  34. })
  35. const body = {
  36. _id: fullData._id,
  37. _rev: fullData._rev,
  38. users: fullData.users || {},
  39. }
  40. if (this.name === 'star') {
  41. log.info('star', 'starring', body._id)
  42. body.users[username] = true
  43. log.verbose('star', 'starring', body)
  44. } else {
  45. delete body.users[username]
  46. log.info('unstar', 'unstarring', body._id)
  47. log.verbose('unstar', 'unstarring', body)
  48. }
  49. const data = await npmFetch.json(pkg.escapedName, {
  50. ...this.npm.flatOptions,
  51. spec: pkg,
  52. method: 'PUT',
  53. body,
  54. })
  55. output.standard(show + ' ' + pkg.name)
  56. log.verbose('star', data)
  57. return data
  58. }
  59. }
  60. }
  61. module.exports = Star