summaryrefslogtreecommitdiff
path: root/deps/npm/lib/stars.js
blob: d1c2b73fb1827632d7bb9efb202d40c08de85b34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const log = require('npmlog')
const fetch = require('npm-registry-fetch')

const npm = require('./npm.js')
const output = require('./utils/output.js')
const getIdentity = require('./utils/get-identity.js')
const usageUtil = require('./utils/usage.js')

const usage = usageUtil('stars', 'npm stars [<user>]')

const cmd = (args, cb) => stars(args).then(() => cb()).catch(cb)

const stars = (args) => {
  return stars_(args).catch(er => {
    if (er.code === 'ENEEDAUTH')
      log.warn('stars', 'auth is required to look up your username')

    throw er
  })
}

const stars_ = async ([user = getIdentity(npm.flatOptions)]) => {
  const { rows } = await fetch.json('/-/_view/starredByUser', {
    ...npm.flatOptions,
    query: { key: `"${await user}"` },
  })

  if (rows.length === 0)
    log.warn('stars', 'user has not starred any packages')

  for (const row of rows)
    output(row.value)
}

module.exports = Object.assign(cmd, { usage })