xref: /third_party/node/deps/npm/lib/commands/star.js (revision 1cb0ef41)
1const fetch = require('npm-registry-fetch')
2const npa = require('npm-package-arg')
3const log = require('../utils/log-shim')
4const getIdentity = require('../utils/get-identity')
5
6const BaseCommand = require('../base-command.js')
7class Star extends BaseCommand {
8  static description = 'Mark your favorite packages'
9  static name = 'star'
10  static usage = ['[<package-spec>...]']
11  static params = [
12    'registry',
13    'unicode',
14    'otp',
15  ]
16
17  static ignoreImplicitWorkspace = false
18
19  async exec (args) {
20    if (!args.length) {
21      throw this.usageError()
22    }
23
24    // if we're unstarring, then show an empty star image
25    // otherwise, show the full star image
26    const unicode = this.npm.config.get('unicode')
27    const full = unicode ? '\u2605 ' : '(*)'
28    const empty = unicode ? '\u2606 ' : '( )'
29    const show = this.name === 'star' ? full : empty
30
31    const pkgs = args.map(npa)
32    const username = await getIdentity(this.npm, this.npm.flatOptions)
33
34    for (const pkg of pkgs) {
35      const fullData = await fetch.json(pkg.escapedName, {
36        ...this.npm.flatOptions,
37        spec: pkg,
38        query: { write: true },
39        preferOnline: true,
40      })
41
42      const body = {
43        _id: fullData._id,
44        _rev: fullData._rev,
45        users: fullData.users || {},
46      }
47
48      if (this.name === 'star') {
49        log.info('star', 'starring', body._id)
50        body.users[username] = true
51        log.verbose('star', 'starring', body)
52      } else {
53        delete body.users[username]
54        log.info('unstar', 'unstarring', body._id)
55        log.verbose('unstar', 'unstarring', body)
56      }
57
58      const data = await fetch.json(pkg.escapedName, {
59        ...this.npm.flatOptions,
60        spec: pkg,
61        method: 'PUT',
62        body,
63      })
64
65      this.npm.output(show + ' ' + pkg.name)
66      log.verbose('star', data)
67      return data
68    }
69  }
70}
71module.exports = Star
72