Skip to content

Commit

Permalink
star: stop using npm-registry-client
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Dec 10, 2018
1 parent 6cd87d1 commit 2786834
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
10 changes: 3 additions & 7 deletions lib/config/figgy-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ const path = require('path')
const npmSession = crypto.randomBytes(8).toString('hex')
log.verbose('npm-session', npmSession)

const NpmConfig = figgyPudding({
log: {default: () => log},
'prefer-offline': {},
'prefer-online': {}
})
const NpmConfig = figgyPudding({})

let baseConfig

Expand Down Expand Up @@ -44,12 +40,12 @@ function mkConfig (...providers) {
}
let conf = baseConfig.concat(...providers)
// Adapt some other configs if missing
if (!('prefer-online' in conf)) {
if (npm.config.get('prefer-online') === undefined) {
conf = conf.concat({
'prefer-online': npm.config.get('cache-max') <= 0
})
}
if (!('prefer-offline' in conf)) {
if (npm.config.get('prefer-online') === undefined) {
conf = conf.concat({
'prefer-online': npm.config.get('cache-min') >= 9999
})
Expand Down
82 changes: 57 additions & 25 deletions lib/star.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
module.exports = star
'use strict'

const BB = require('bluebird')

const fetch = require('libnpm/fetch')
const figgyPudding = require('figgy-pudding')
const log = require('npmlog')
const npa = require('libnpm/parse-arg')
const npm = require('./npm.js')
const npmConfig = require('./config/figgy-config.js')
const output = require('./utils/output.js')
const usage = require('./utils/usage.js')
const whoami = require('./whoami.js')

var npm = require('./npm.js')
var log = require('npmlog')
var asyncMap = require('slide').asyncMap
var mapToRegistry = require('./utils/map-to-registry.js')
var usage = require('./utils/usage')
var output = require('./utils/output.js')
const StarConfig = figgyPudding({
'unicode': {}
})

star.usage = usage(
'star',
Expand All @@ -19,27 +28,50 @@ star.completion = function (opts, cb) {
cb()
}

module.exports = star
function star (args, cb) {
if (!args.length) return cb(star.usage)
var s = npm.config.get('unicode') ? '\u2605 ' : '(*)'
var u = npm.config.get('unicode') ? '\u2606 ' : '( )'
var using = !(npm.command.match(/^un/))
if (!using) s = u
asyncMap(args, function (pkg, cb) {
mapToRegistry(pkg, npm.config, function (er, uri, auth) {
if (er) return cb(er)
const opts = StarConfig(npmConfig())
return BB.try(() => {
if (!args.length) throw new Error(star.usage)
let s = opts.unicode ? '\u2605 ' : '(*)'
const u = opts.unicode ? '\u2606 ' : '( )'
const using = !(npm.command.match(/^un/))
if (!using) s = u
return BB.map(args.map(npa), pkg => {
return BB.all([
whoami([pkg], true, () => {}),
fetch.json(pkg.escapedName, opts.concat({
spec: pkg,
query: {write: true},
'prefer-online': true
}))
]).then(([username, fullData]) => {
if (!username) { throw new Error('You need to be logged in!') }
const body = {
_id: fullData._id,
_rev: fullData._rev,
users: fullData.users || {}
}

var params = {
starred: using,
auth: auth
}
npm.registry.star(uri, params, function (er, data, raw, req) {
if (!er) {
output(s + ' ' + pkg)
log.verbose('star', data)
if (using) {
log.info('star', 'starring', body._id)
body.users[username] = true
log.verbose('star', 'starring', body)
} else {
delete body.users[username]
log.info('star', 'unstarring', body._id)
log.verbose('star', 'unstarring', body)
}
cb(er, data, raw, req)
return fetch.json(pkg.escapedName, opts.concat({
spec: pkg,
method: 'PUT',
body
}))
}).then(data => {
output(s + ' ' + pkg.name)
log.verbose('star', data)
return data
})
})
}, cb)
}).nodeify(cb)
}

0 comments on commit 2786834

Please sign in to comment.