star.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict'
  2. const BB = require('bluebird')
  3. const fetch = require('libnpm/fetch')
  4. const figgyPudding = require('figgy-pudding')
  5. const log = require('npmlog')
  6. const npa = require('libnpm/parse-arg')
  7. const npm = require('./npm.js')
  8. const npmConfig = require('./config/figgy-config.js')
  9. const output = require('./utils/output.js')
  10. const usage = require('./utils/usage.js')
  11. const whoami = require('./whoami.js')
  12. const StarConfig = figgyPudding({
  13. 'unicode': {}
  14. })
  15. star.usage = usage(
  16. 'star',
  17. 'npm star [<pkg>...]\n' +
  18. 'npm unstar [<pkg>...]'
  19. )
  20. star.completion = function (opts, cb) {
  21. // FIXME: there used to be registry completion here, but it stopped making
  22. // sense somewhere around 50,000 packages on the registry
  23. cb()
  24. }
  25. module.exports = star
  26. function star (args, cb) {
  27. const opts = StarConfig(npmConfig())
  28. return BB.try(() => {
  29. if (!args.length) throw new Error(star.usage)
  30. let s = opts.unicode ? '\u2605 ' : '(*)'
  31. const u = opts.unicode ? '\u2606 ' : '( )'
  32. const using = !(npm.command.match(/^un/))
  33. if (!using) s = u
  34. return BB.map(args.map(npa), pkg => {
  35. return BB.all([
  36. whoami([pkg], true, () => {}),
  37. fetch.json(pkg.escapedName, opts.concat({
  38. spec: pkg,
  39. query: {write: true},
  40. 'prefer-online': true
  41. }))
  42. ]).then(([username, fullData]) => {
  43. if (!username) { throw new Error('You need to be logged in!') }
  44. const body = {
  45. _id: fullData._id,
  46. _rev: fullData._rev,
  47. users: fullData.users || {}
  48. }
  49. if (using) {
  50. log.info('star', 'starring', body._id)
  51. body.users[username] = true
  52. log.verbose('star', 'starring', body)
  53. } else {
  54. delete body.users[username]
  55. log.info('star', 'unstarring', body._id)
  56. log.verbose('star', 'unstarring', body)
  57. }
  58. return fetch.json(pkg.escapedName, opts.concat({
  59. spec: pkg,
  60. method: 'PUT',
  61. body
  62. }))
  63. }).then(data => {
  64. output(s + ' ' + pkg.name)
  65. log.verbose('star', data)
  66. return data
  67. })
  68. })
  69. }).nodeify(cb)
  70. }