diff options
author | Ruy Adorno <ruyadorno@hotmail.com> | 2020-11-20 00:12:44 +0300 |
---|---|---|
committer | nlf <quitlahok@gmail.com> | 2020-11-20 21:56:59 +0300 |
commit | 5fc56b6dbcc7d7d1463a761abb67d2fc16ad3657 (patch) | |
tree | 8ac3686c50d2709c33f6eedf42db6c03ffcd9806 /lib/star.js | |
parent | e1a2837809a76896523cdfcbce7537e46f71d67e (diff) |
fix: npm unstar
- Refactored lib/star.js
- Fixes `npm unstar` by adding a lib/unstar.js alias/cmd
- Add tests for lib/star.js and lib/unstar.js
Fixes: https://github.com/npm/statusboard/issues/174
PR-URL: https://github.com/npm/cli/pull/2204
Credit: @ruyadorno
Close: #2204
Reviewed-by: @nlf
Diffstat (limited to 'lib/star.js')
-rw-r--r-- | lib/star.js | 120 |
1 files changed, 61 insertions, 59 deletions
diff --git a/lib/star.js b/lib/star.js index 15beef373..85d14d0e4 100644 --- a/lib/star.js +++ b/lib/star.js @@ -3,73 +3,75 @@ const fetch = require('npm-registry-fetch') const log = require('npmlog') const npa = require('npm-package-arg') + const npm = require('./npm.js') const output = require('./utils/output.js') -const usage = require('./utils/usage.js') -const getItentity = require('./utils/get-identity') +const usageUtil = require('./utils/usage.js') +const getIdentity = require('./utils/get-identity') +const completion = require('./utils/completion/none.js') -star.usage = usage( +const usage = usageUtil( 'star', 'npm star [<pkg>...]\n' + 'npm unstar [<pkg>...]' ) -star.completion = function (opts, cb) { - // FIXME: there used to be registry completion here, but it stopped making - // sense somewhere around 50,000 packages on the registry - cb() -} +const cmd = (args, cb) => star(args).then(() => cb()).catch(cb) + +const star = async args => { + if (!args.length) + throw new Error(usage) + + // if we're unstarring, then show an empty star image + // otherwise, show the full star image + const { unicode } = npm.flatOptions + const unstar = npm.config.get('star.unstar') + const full = unicode ? '\u2605 ' : '(*)' + const empty = unicode ? '\u2606 ' : '( )' + const show = unstar ? empty : full + + const pkgs = args.map(npa) + for (const pkg of pkgs) { + const [username, fullData] = await Promise.all([ + getIdentity(npm.flatOptions), + fetch.json(pkg.escapedName, { + ...npm.flatOptions, + spec: pkg, + query: { write: true }, + preferOnline: true, + }), + ]) -module.exports = star -function star (args, cb) { - const opts = npm.flatOptions - return Promise.resolve().then(() => { - if (!args.length) - throw new Error(star.usage) - // if we're unstarring, then show an empty star image - // otherwise, show the full star image - const unstar = /^un/.test(npm.command) - const full = opts.unicode ? '\u2605 ' : '(*)' - const empty = opts.unicode ? '\u2606 ' : '( )' - const show = unstar ? empty : full - return Promise.all(args.map(npa).map(pkg => { - return Promise.all([ - getItentity(opts), - fetch.json(pkg.escapedName, { - ...opts, - spec: pkg, - query: { write: true }, - preferOnline: 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 || {}, - } + if (!username) + throw new Error('You need to be logged in!') - if (!unstar) { - 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) - } - return fetch.json(pkg.escapedName, { - ...opts, - spec: pkg, - method: 'PUT', - body, - }) - }).then(data => { - output(show + ' ' + pkg.name) - log.verbose('star', data) - return data - }) - })) - }).then(() => cb(), cb) + const body = { + _id: fullData._id, + _rev: fullData._rev, + users: fullData.users || {}, + } + + if (!unstar) { + log.info('star', 'starring', body._id) + body.users[username] = true + log.verbose('star', 'starring', body) + } else { + delete body.users[username] + log.info('unstar', 'unstarring', body._id) + log.verbose('unstar', 'unstarring', body) + } + + const data = await fetch.json(pkg.escapedName, { + ...npm.flatOptions, + spec: pkg, + method: 'PUT', + body, + }) + + output(show + ' ' + pkg.name) + log.verbose('star', data) + return data + } } + +module.exports = Object.assign(cmd, { completion, usage }) |