UNPKG

2.16 kBJavaScriptView Raw
1'use strict'
2
3const BB = require('bluebird')
4
5const fetch = require('libnpm/fetch')
6const figgyPudding = require('figgy-pudding')
7const log = require('npmlog')
8const npa = require('libnpm/parse-arg')
9const npm = require('./npm.js')
10const npmConfig = require('./config/figgy-config.js')
11const output = require('./utils/output.js')
12const usage = require('./utils/usage.js')
13const whoami = require('./whoami.js')
14
15const StarConfig = figgyPudding({
16 'unicode': {}
17})
18
19star.usage = usage(
20 'star',
21 'npm star [<pkg>...]\n' +
22 'npm unstar [<pkg>...]'
23)
24
25star.completion = function (opts, cb) {
26 // FIXME: there used to be registry completion here, but it stopped making
27 // sense somewhere around 50,000 packages on the registry
28 cb()
29}
30
31module.exports = star
32function star (args, cb) {
33 const opts = StarConfig(npmConfig())
34 return BB.try(() => {
35 if (!args.length) throw new Error(star.usage)
36 let s = opts.unicode ? '\u2605 ' : '(*)'
37 const u = opts.unicode ? '\u2606 ' : '( )'
38 const using = !(npm.command.match(/^un/))
39 if (!using) s = u
40 return BB.map(args.map(npa), pkg => {
41 return BB.all([
42 whoami([pkg], true, () => {}),
43 fetch.json(pkg.escapedName, opts.concat({
44 spec: pkg,
45 query: {write: true},
46 'prefer-online': true
47 }))
48 ]).then(([username, fullData]) => {
49 if (!username) { throw new Error('You need to be logged in!') }
50 const body = {
51 _id: fullData._id,
52 _rev: fullData._rev,
53 users: fullData.users || {}
54 }
55
56 if (using) {
57 log.info('star', 'starring', body._id)
58 body.users[username] = true
59 log.verbose('star', 'starring', body)
60 } else {
61 delete body.users[username]
62 log.info('star', 'unstarring', body._id)
63 log.verbose('star', 'unstarring', body)
64 }
65 return fetch.json(pkg.escapedName, opts.concat({
66 spec: pkg,
67 method: 'PUT',
68 body
69 }))
70 }).then(data => {
71 output(s + ' ' + pkg.name)
72 log.verbose('star', data)
73 return data
74 })
75 })
76 }).nodeify(cb)
77}