2025-01-10 08:20:27 -08:00
|
|
|
const npmFetch = require('npm-registry-fetch')
|
2020-10-02 17:52:19 -04:00
|
|
|
const npa = require('npm-package-arg')
|
2024-04-30 23:53:22 -07:00
|
|
|
const { log, output } = require('proc-log')
|
2021-11-04 20:42:47 +00:00
|
|
|
const getIdentity = require('../utils/get-identity')
|
2024-04-30 23:53:22 -07:00
|
|
|
const BaseCommand = require('../base-cmd.js')
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-11 17:54:23 -05:00
|
|
|
class Star extends BaseCommand {
|
2021-11-18 20:58:02 +00:00
|
|
|
static description = 'Mark your favorite packages'
|
|
|
|
static name = 'star'
|
2022-06-24 18:21:50 -07:00
|
|
|
static usage = ['[<package-spec>...]']
|
2021-11-18 20:58:02 +00:00
|
|
|
static params = [
|
|
|
|
'registry',
|
|
|
|
'unicode',
|
2022-05-11 17:17:18 +00:00
|
|
|
'otp',
|
2021-11-18 20:58:02 +00:00
|
|
|
]
|
2021-05-20 15:54:50 -04:00
|
|
|
|
2022-03-03 21:38:08 +00:00
|
|
|
static ignoreImplicitWorkspace = false
|
|
|
|
|
2021-11-04 20:42:47 +00:00
|
|
|
async exec (args) {
|
2021-11-18 20:58:02 +00:00
|
|
|
if (!args.length) {
|
2021-11-04 20:42:47 +00:00
|
|
|
throw this.usageError()
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
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 full = unicode ? '\u2605 ' : '(*)'
|
|
|
|
const empty = unicode ? '\u2606 ' : '( )'
|
2022-05-11 17:17:18 +00:00
|
|
|
const show = this.name === 'star' ? full : empty
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
const pkgs = args.map(npa)
|
2022-05-11 17:17:18 +00:00
|
|
|
const username = await getIdentity(this.npm, this.npm.flatOptions)
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2022-05-11 17:17:18 +00:00
|
|
|
for (const pkg of pkgs) {
|
2025-01-10 08:20:27 -08:00
|
|
|
const fullData = await npmFetch.json(pkg.escapedName, {
|
2022-05-11 17:17:18 +00:00
|
|
|
...this.npm.flatOptions,
|
|
|
|
spec: pkg,
|
|
|
|
query: { write: true },
|
|
|
|
preferOnline: true,
|
|
|
|
})
|
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 || {},
|
|
|
|
}
|
|
|
|
|
2022-05-11 17:17:18 +00:00
|
|
|
if (this.name === 'star') {
|
2021-03-04 17:40:28 -05:00
|
|
|
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
|
|
|
|
2025-01-10 08:20:27 -08:00
|
|
|
const data = await npmFetch.json(pkg.escapedName, {
|
2021-03-04 17:40:28 -05:00
|
|
|
...this.npm.flatOptions,
|
|
|
|
spec: pkg,
|
|
|
|
method: 'PUT',
|
|
|
|
body,
|
|
|
|
})
|
2020-11-20 15:29:49 -05:00
|
|
|
|
2024-04-30 23:53:22 -07:00
|
|
|
output.standard(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
|
|
|
}
|
2024-04-30 23:53:22 -07:00
|
|
|
|
2021-03-04 17:40:28 -05:00
|
|
|
module.exports = Star
|