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