2020-10-02 17:52:19 -04:00
|
|
|
const fetch = require('npm-registry-fetch')
|
2019-01-29 14:43:00 -08:00
|
|
|
const log = require('npmlog')
|
2020-10-02 17:52:19 -04:00
|
|
|
const npa = require('npm-package-arg')
|
2020-11-20 15:29:49 -05:00
|
|
|
|
|
|
|
const getIdentity = require('./utils/get-identity')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
const BaseCommand = require('./base-command.js')
|
|
|
|
class Star extends BaseCommand {
|
2021-03-23 14:58:11 -04:00
|
|
|
static get description () {
|
|
|
|
return 'Mark your favorite packages'
|
|
|
|
}
|
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
|
|
static get name () {
|
|
|
|
return 'star'
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
/* istanbul ignore next - see test/lib/load-all-commands.js */
|
|
|
|
static get usage () {
|
|
|
|
return ['[<pkg>...]']
|
2021-03-04 17:40:28 -05:00
|
|
|
}
|
2020-11-20 15:29:49 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
exec (args, cb) {
|
|
|
|
this.star(args).then(() => cb()).catch(cb)
|
|
|
|
}
|
2020-11-20 15:29:49 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
async star (args) {
|
|
|
|
if (!args.length)
|
|
|
|
throw new Error(this.usage)
|
2020-11-20 15:29:49 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
// if we're unstarring, then show an empty star image
|
|
|
|
// otherwise, show the full star image
|
2021-03-23 14:58:11 -04:00
|
|
|
const unicode = this.npm.config.get('unicode')
|
2021-03-04 17:40:28 -05:00
|
|
|
const unstar = this.npm.config.get('star.unstar')
|
|
|
|
const full = unicode ? '\u2605 ' : '(*)'
|
|
|
|
const empty = unicode ? '\u2606 ' : '( )'
|
|
|
|
const show = unstar ? empty : full
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
const pkgs = args.map(npa)
|
|
|
|
for (const pkg of pkgs) {
|
|
|
|
const [username, fullData] = await Promise.all([
|
|
|
|
getIdentity(this.npm, this.npm.flatOptions),
|
|
|
|
fetch.json(pkg.escapedName, {
|
|
|
|
...this.npm.flatOptions,
|
|
|
|
spec: pkg,
|
|
|
|
query: { write: true },
|
|
|
|
preferOnline: true,
|
|
|
|
}),
|
|
|
|
])
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
if (!username)
|
|
|
|
throw new Error('You need to be logged in!')
|
2020-11-20 15:29:49 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
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)
|
|
|
|
}
|
2020-11-20 15:29:49 -05:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
const data = await fetch.json(pkg.escapedName, {
|
|
|
|
...this.npm.flatOptions,
|
|
|
|
spec: pkg,
|
|
|
|
method: 'PUT',
|
|
|
|
body,
|
|
|
|
})
|
2020-11-20 15:29:49 -05:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
this.npm.output(show + ' ' + pkg.name)
|
2021-03-04 17:40:28 -05:00
|
|
|
log.verbose('star', data)
|
|
|
|
return data
|
|
|
|
}
|
2020-11-20 15:29:49 -05:00
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|
2021-03-04 17:40:28 -05:00
|
|
|
module.exports = Star
|