2019-01-29 14:43:00 -08:00
|
|
|
'use strict'
|
|
|
|
|
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')
|
2019-01-29 14:43:00 -08:00
|
|
|
const npm = require('./npm.js')
|
|
|
|
const output = require('./utils/output.js')
|
|
|
|
const usage = require('./utils/usage.js')
|
2020-10-02 17:52:19 -04:00
|
|
|
const getItentity = require('./utils/get-identity')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2016-05-06 15:22:02 -07:00
|
|
|
star.usage = usage(
|
|
|
|
'star',
|
|
|
|
'npm star [<pkg>...]\n' +
|
|
|
|
'npm unstar [<pkg>...]'
|
|
|
|
)
|
2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
star.completion = function (opts, cb) {
|
2015-03-06 02:57:32 -06:00
|
|
|
// FIXME: there used to be registry completion here, but it stopped making
|
|
|
|
// sense somewhere around 50,000 packages on the registry
|
|
|
|
cb()
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|
|
|
|
|
2019-01-29 14:43:00 -08:00
|
|
|
module.exports = star
|
2011-11-21 09:48:45 -08:00
|
|
|
function star (args, cb) {
|
2020-10-02 17:52:19 -04:00
|
|
|
const opts = npm.flatOptions
|
|
|
|
return Promise.resolve().then(() => {
|
2020-11-01 07:54:36 +01:00
|
|
|
if (!args.length)
|
|
|
|
throw new Error(star.usage)
|
2020-10-02 17:52:19 -04:00
|
|
|
// 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,
|
2019-01-29 14:43:00 -08:00
|
|
|
spec: pkg,
|
2020-10-02 17:52:19 -04:00
|
|
|
query: { write: true },
|
2020-11-01 07:54:36 +01:00
|
|
|
preferOnline: true,
|
|
|
|
}),
|
2019-01-29 14:43:00 -08:00
|
|
|
]).then(([username, fullData]) => {
|
2020-11-01 07:54:36 +01:00
|
|
|
if (!username)
|
|
|
|
throw new Error('You need to be logged in!')
|
2019-01-29 14:43:00 -08:00
|
|
|
const body = {
|
|
|
|
_id: fullData._id,
|
|
|
|
_rev: fullData._rev,
|
2020-11-01 07:54:36 +01:00
|
|
|
users: fullData.users || {},
|
2019-01-29 14:43:00 -08:00
|
|
|
}
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
if (!unstar) {
|
2019-01-29 14:43:00 -08:00
|
|
|
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)
|
2014-09-24 14:41:07 -07:00
|
|
|
}
|
2020-10-02 17:52:19 -04:00
|
|
|
return fetch.json(pkg.escapedName, {
|
|
|
|
...opts,
|
2019-01-29 14:43:00 -08:00
|
|
|
spec: pkg,
|
|
|
|
method: 'PUT',
|
2020-11-01 07:54:36 +01:00
|
|
|
body,
|
2020-10-02 17:52:19 -04:00
|
|
|
})
|
2019-01-29 14:43:00 -08:00
|
|
|
}).then(data => {
|
2020-10-02 17:52:19 -04:00
|
|
|
output(show + ' ' + pkg.name)
|
2019-01-29 14:43:00 -08:00
|
|
|
log.verbose('star', data)
|
|
|
|
return data
|
2014-09-24 14:41:07 -07:00
|
|
|
})
|
2020-10-02 17:52:19 -04:00
|
|
|
}))
|
|
|
|
}).then(() => cb(), cb)
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|