Skip to content

Commit 11c07b3

Browse files
committed
star: stop using npm-registry-client
1 parent 7047ffd commit 11c07b3

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

lib/star.js

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
module.exports = star
1+
'use strict'
2+
3+
const BB = require('bluebird')
24

3-
var npm = require('./npm.js')
4-
var log = require('npmlog')
5-
var asyncMap = require('slide').asyncMap
6-
var mapToRegistry = require('./utils/map-to-registry.js')
7-
var usage = require('./utils/usage')
8-
var output = require('./utils/output.js')
5+
const config = require('./config/figgy-config.js')
6+
const fetch = require('npm-registry-fetch')
7+
const log = require('npmlog')
8+
const npa = require('npm-package-arg')
9+
const npm = require('./npm.js')
10+
const output = require('./utils/output.js')
11+
const usage = require('./utils/usage.js')
12+
const whoami = require('./whoami.js')
913

1014
star.usage = usage(
1115
'star',
@@ -19,27 +23,49 @@ star.completion = function (opts, cb) {
1923
cb()
2024
}
2125

26+
module.exports = star
2227
function star (args, cb) {
23-
if (!args.length) return cb(star.usage)
24-
var s = npm.config.get('unicode') ? '\u2605 ' : '(*)'
25-
var u = npm.config.get('unicode') ? '\u2606 ' : '( )'
26-
var using = !(npm.command.match(/^un/))
27-
if (!using) s = u
28-
asyncMap(args, function (pkg, cb) {
29-
mapToRegistry(pkg, npm.config, function (er, uri, auth) {
30-
if (er) return cb(er)
28+
return BB.try(() => {
29+
if (!args.length) throw new Error(star.usage)
30+
let s = npm.config.get('unicode') ? '\u2605 ' : '(*)'
31+
const u = npm.config.get('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, config({
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+
}
3149

32-
var params = {
33-
starred: using,
34-
auth: auth
35-
}
36-
npm.registry.star(uri, params, function (er, data, raw, req) {
37-
if (!er) {
38-
output(s + ' ' + pkg)
39-
log.verbose('star', data)
50+
if (using) {
51+
log.info('star', 'starring', body._id)
52+
body.users[username] = true
53+
log.verbose('star', 'starring', body)
54+
} else {
55+
delete body.users[username]
56+
log.info('star', 'unstarring', body._id)
57+
log.verbose('star', 'unstarring', body)
4058
}
41-
cb(er, data, raw, req)
59+
return fetch.json(pkg.escapedName, config({
60+
spec: pkg,
61+
method: 'PUT',
62+
body
63+
}))
64+
}).then(data => {
65+
output(s + ' ' + pkg.name)
66+
log.verbose('star', data)
67+
return data
4268
})
4369
})
44-
}, cb)
70+
}).nodeify(cb)
4571
}

0 commit comments

Comments
 (0)