|
1 | | -module.exports = stars |
2 | | - |
3 | | -stars.usage = 'npm stars [<user>]' |
4 | | - |
5 | | -var npm = require('./npm.js') |
6 | | -var log = require('npmlog') |
7 | | -var mapToRegistry = require('./utils/map-to-registry.js') |
8 | | -var output = require('./utils/output.js') |
| 1 | +'use strict' |
9 | 2 |
|
10 | | -function stars (args, cb) { |
11 | | - npm.commands.whoami([], true, function (er, username) { |
12 | | - var name = args.length === 1 ? args[0] : username |
| 3 | +const BB = require('bluebird') |
13 | 4 |
|
14 | | - if (er) { |
15 | | - if (er.code === 'ENEEDAUTH' && !name) { |
16 | | - var needAuth = new Error("'npm stars' on your own user account requires auth") |
17 | | - needAuth.code = 'ENEEDAUTH' |
18 | | - return cb(needAuth) |
19 | | - } |
20 | | - |
21 | | - if (er.code !== 'ENEEDAUTH') return cb(er) |
22 | | - } |
| 5 | +const npmConfig = require('./config/figgy-config.js') |
| 6 | +const fetch = require('libnpm/fetch') |
| 7 | +const log = require('npmlog') |
| 8 | +const output = require('./utils/output.js') |
| 9 | +const whoami = require('./whoami.js') |
23 | 10 |
|
24 | | - mapToRegistry('', npm.config, function (er, uri, auth) { |
25 | | - if (er) return cb(er) |
| 11 | +stars.usage = 'npm stars [<user>]' |
26 | 12 |
|
27 | | - var params = { |
28 | | - username: name, |
29 | | - auth: auth |
| 13 | +module.exports = stars |
| 14 | +function 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)) |
30 | 26 | } |
31 | | - npm.registry.stars(uri, params, showstars) |
32 | 27 | }) |
33 | | - }) |
34 | | - |
35 | | - function showstars (er, data) { |
36 | | - if (er) return cb(er) |
37 | | - |
38 | | - if (data.rows.length === 0) { |
39 | | - log.warn('stars', 'user has not starred any packages.') |
40 | | - } else { |
41 | | - data.rows.forEach(function (a) { |
42 | | - output(a.value) |
| 28 | + }).catch(err => { |
| 29 | + if (err.code === 'ENEEDAUTH') { |
| 30 | + throw Object.assign(new Error("'npm starts' on your own user account requires auth"), { |
| 31 | + code: 'ENEEDAUTH' |
43 | 32 | }) |
| 33 | + } else { |
| 34 | + throw err |
44 | 35 | } |
45 | | - cb() |
46 | | - } |
| 36 | + }).nodeify(cb) |
47 | 37 | } |
0 commit comments