From 2786834c0257b8bb1bbb115f1ce7060abaab2e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Thu, 5 Apr 2018 23:49:21 -0700 Subject: [PATCH] star: stop using npm-registry-client --- lib/config/figgy-config.js | 10 ++--- lib/star.js | 82 ++++++++++++++++++++++++++------------ 2 files changed, 60 insertions(+), 32 deletions(-) diff --git a/lib/config/figgy-config.js b/lib/config/figgy-config.js index 92102d5119165..3ad5009c7d103 100644 --- a/lib/config/figgy-config.js +++ b/lib/config/figgy-config.js @@ -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 @@ -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 }) diff --git a/lib/star.js b/lib/star.js index f19cb4b07bebb..44a762b15c0c0 100644 --- a/lib/star.js +++ b/lib/star.js @@ -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', @@ -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) }