Skip to content

Commit 2786834

Browse files
committed
star: stop using npm-registry-client
1 parent 6cd87d1 commit 2786834

File tree

2 files changed

+60
-32
lines changed

2 files changed

+60
-32
lines changed

lib/config/figgy-config.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ const path = require('path')
1212
const npmSession = crypto.randomBytes(8).toString('hex')
1313
log.verbose('npm-session', npmSession)
1414

15-
const NpmConfig = figgyPudding({
16-
log: {default: () => log},
17-
'prefer-offline': {},
18-
'prefer-online': {}
19-
})
15+
const NpmConfig = figgyPudding({})
2016

2117
let baseConfig
2218

@@ -44,12 +40,12 @@ function mkConfig (...providers) {
4440
}
4541
let conf = baseConfig.concat(...providers)
4642
// Adapt some other configs if missing
47-
if (!('prefer-online' in conf)) {
43+
if (npm.config.get('prefer-online') === undefined) {
4844
conf = conf.concat({
4945
'prefer-online': npm.config.get('cache-max') <= 0
5046
})
5147
}
52-
if (!('prefer-offline' in conf)) {
48+
if (npm.config.get('prefer-online') === undefined) {
5349
conf = conf.concat({
5450
'prefer-online': npm.config.get('cache-min') >= 9999
5551
})

lib/star.js

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
module.exports = star
1+
'use strict'
2+
3+
const BB = require('bluebird')
4+
5+
const fetch = require('libnpm/fetch')
6+
const figgyPudding = require('figgy-pudding')
7+
const log = require('npmlog')
8+
const npa = require('libnpm/parse-arg')
9+
const npm = require('./npm.js')
10+
const npmConfig = require('./config/figgy-config.js')
11+
const output = require('./utils/output.js')
12+
const usage = require('./utils/usage.js')
13+
const whoami = require('./whoami.js')
214

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')
15+
const StarConfig = figgyPudding({
16+
'unicode': {}
17+
})
918

1019
star.usage = usage(
1120
'star',
@@ -19,27 +28,50 @@ star.completion = function (opts, cb) {
1928
cb()
2029
}
2130

31+
module.exports = star
2232
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)
33+
const opts = StarConfig(npmConfig())
34+
return BB.try(() => {
35+
if (!args.length) throw new Error(star.usage)
36+
let s = opts.unicode ? '\u2605 ' : '(*)'
37+
const u = opts.unicode ? '\u2606 ' : '( )'
38+
const using = !(npm.command.match(/^un/))
39+
if (!using) s = u
40+
return BB.map(args.map(npa), pkg => {
41+
return BB.all([
42+
whoami([pkg], true, () => {}),
43+
fetch.json(pkg.escapedName, opts.concat({
44+
spec: pkg,
45+
query: {write: true},
46+
'prefer-online': true
47+
}))
48+
]).then(([username, fullData]) => {
49+
if (!username) { throw new Error('You need to be logged in!') }
50+
const body = {
51+
_id: fullData._id,
52+
_rev: fullData._rev,
53+
users: fullData.users || {}
54+
}
3155

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)
56+
if (using) {
57+
log.info('star', 'starring', body._id)
58+
body.users[username] = true
59+
log.verbose('star', 'starring', body)
60+
} else {
61+
delete body.users[username]
62+
log.info('star', 'unstarring', body._id)
63+
log.verbose('star', 'unstarring', body)
4064
}
41-
cb(er, data, raw, req)
65+
return fetch.json(pkg.escapedName, opts.concat({
66+
spec: pkg,
67+
method: 'PUT',
68+
body
69+
}))
70+
}).then(data => {
71+
output(s + ' ' + pkg.name)
72+
log.verbose('star', data)
73+
return data
4274
})
4375
})
44-
}, cb)
76+
}).nodeify(cb)
4577
}

0 commit comments

Comments
 (0)