1 | 'use strict'
|
2 |
|
3 | const BB = require('bluebird')
|
4 |
|
5 | const fetch = require('libnpm/fetch')
|
6 | const figgyPudding = require('figgy-pudding')
|
7 | const log = require('npmlog')
|
8 | const npa = require('libnpm/parse-arg')
|
9 | const npm = require('./npm.js')
|
10 | const npmConfig = require('./config/figgy-config.js')
|
11 | const output = require('./utils/output.js')
|
12 | const usage = require('./utils/usage.js')
|
13 | const whoami = require('./whoami.js')
|
14 |
|
15 | const StarConfig = figgyPudding({
|
16 | 'unicode': {}
|
17 | })
|
18 |
|
19 | star.usage = usage(
|
20 | 'star',
|
21 | 'npm star [<pkg>...]\n' +
|
22 | 'npm unstar [<pkg>...]'
|
23 | )
|
24 |
|
25 | star.completion = function (opts, cb) {
|
26 |
|
27 |
|
28 | cb()
|
29 | }
|
30 |
|
31 | module.exports = star
|
32 | function 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 | }
|