|
3 | 3 | const fetch = require('npm-registry-fetch')
|
4 | 4 | const log = require('npmlog')
|
5 | 5 | const npa = require('npm-package-arg')
|
| 6 | + |
6 | 7 | const npm = require('./npm.js')
|
7 | 8 | const output = require('./utils/output.js')
|
8 |
| -const usage = require('./utils/usage.js') |
9 |
| -const getItentity = require('./utils/get-identity') |
| 9 | +const usageUtil = require('./utils/usage.js') |
| 10 | +const getIdentity = require('./utils/get-identity') |
| 11 | +const completion = require('./utils/completion/none.js') |
10 | 12 |
|
11 |
| -star.usage = usage( |
| 13 | +const usage = usageUtil( |
12 | 14 | 'star',
|
13 | 15 | 'npm star [<pkg>...]\n' +
|
14 | 16 | 'npm unstar [<pkg>...]'
|
15 | 17 | )
|
16 | 18 |
|
17 |
| -star.completion = function (opts, cb) { |
18 |
| - // FIXME: there used to be registry completion here, but it stopped making |
19 |
| - // sense somewhere around 50,000 packages on the registry |
20 |
| - cb() |
21 |
| -} |
| 19 | +const cmd = (args, cb) => star(args).then(() => cb()).catch(cb) |
| 20 | + |
| 21 | +const star = async args => { |
| 22 | + if (!args.length) |
| 23 | + throw new Error(usage) |
| 24 | + |
| 25 | + // if we're unstarring, then show an empty star image |
| 26 | + // otherwise, show the full star image |
| 27 | + const { unicode } = npm.flatOptions |
| 28 | + const unstar = npm.config.get('star.unstar') |
| 29 | + const full = unicode ? '\u2605 ' : '(*)' |
| 30 | + const empty = unicode ? '\u2606 ' : '( )' |
| 31 | + const show = unstar ? empty : full |
| 32 | + |
| 33 | + const pkgs = args.map(npa) |
| 34 | + for (const pkg of pkgs) { |
| 35 | + const [username, fullData] = await Promise.all([ |
| 36 | + getIdentity(npm.flatOptions), |
| 37 | + fetch.json(pkg.escapedName, { |
| 38 | + ...npm.flatOptions, |
| 39 | + spec: pkg, |
| 40 | + query: { write: true }, |
| 41 | + preferOnline: true, |
| 42 | + }), |
| 43 | + ]) |
22 | 44 |
|
23 |
| -module.exports = star |
24 |
| -function star (args, cb) { |
25 |
| - const opts = npm.flatOptions |
26 |
| - return Promise.resolve().then(() => { |
27 |
| - if (!args.length) |
28 |
| - throw new Error(star.usage) |
29 |
| - // if we're unstarring, then show an empty star image |
30 |
| - // otherwise, show the full star image |
31 |
| - const unstar = /^un/.test(npm.command) |
32 |
| - const full = opts.unicode ? '\u2605 ' : '(*)' |
33 |
| - const empty = opts.unicode ? '\u2606 ' : '( )' |
34 |
| - const show = unstar ? empty : full |
35 |
| - return Promise.all(args.map(npa).map(pkg => { |
36 |
| - return Promise.all([ |
37 |
| - getItentity(opts), |
38 |
| - fetch.json(pkg.escapedName, { |
39 |
| - ...opts, |
40 |
| - spec: pkg, |
41 |
| - query: { write: true }, |
42 |
| - preferOnline: true, |
43 |
| - }), |
44 |
| - ]).then(([username, fullData]) => { |
45 |
| - if (!username) |
46 |
| - throw new Error('You need to be logged in!') |
47 |
| - const body = { |
48 |
| - _id: fullData._id, |
49 |
| - _rev: fullData._rev, |
50 |
| - users: fullData.users || {}, |
51 |
| - } |
| 45 | + if (!username) |
| 46 | + throw new Error('You need to be logged in!') |
52 | 47 |
|
53 |
| - if (!unstar) { |
54 |
| - log.info('star', 'starring', body._id) |
55 |
| - body.users[username] = true |
56 |
| - log.verbose('star', 'starring', body) |
57 |
| - } else { |
58 |
| - delete body.users[username] |
59 |
| - log.info('star', 'unstarring', body._id) |
60 |
| - log.verbose('star', 'unstarring', body) |
61 |
| - } |
62 |
| - return fetch.json(pkg.escapedName, { |
63 |
| - ...opts, |
64 |
| - spec: pkg, |
65 |
| - method: 'PUT', |
66 |
| - body, |
67 |
| - }) |
68 |
| - }).then(data => { |
69 |
| - output(show + ' ' + pkg.name) |
70 |
| - log.verbose('star', data) |
71 |
| - return data |
72 |
| - }) |
73 |
| - })) |
74 |
| - }).then(() => cb(), cb) |
| 48 | + const body = { |
| 49 | + _id: fullData._id, |
| 50 | + _rev: fullData._rev, |
| 51 | + users: fullData.users || {}, |
| 52 | + } |
| 53 | + |
| 54 | + if (!unstar) { |
| 55 | + log.info('star', 'starring', body._id) |
| 56 | + body.users[username] = true |
| 57 | + log.verbose('star', 'starring', body) |
| 58 | + } else { |
| 59 | + delete body.users[username] |
| 60 | + log.info('unstar', 'unstarring', body._id) |
| 61 | + log.verbose('unstar', 'unstarring', body) |
| 62 | + } |
| 63 | + |
| 64 | + const data = await fetch.json(pkg.escapedName, { |
| 65 | + ...npm.flatOptions, |
| 66 | + spec: pkg, |
| 67 | + method: 'PUT', |
| 68 | + body, |
| 69 | + }) |
| 70 | + |
| 71 | + output(show + ' ' + pkg.name) |
| 72 | + log.verbose('star', data) |
| 73 | + return data |
| 74 | + } |
75 | 75 | }
|
| 76 | + |
| 77 | +module.exports = Object.assign(cmd, { completion, usage }) |
0 commit comments