1- module . exports = star
1+ 'use strict'
2+
3+ const BB = require ( 'bluebird' )
4+
5+ const npmConfig = require ( './config/figgy-config.js' )
6+ const fetch = require ( 'npm-registry-fetch' )
7+ const figgyPudding = require ( 'figgy-pudding' )
8+ const log = require ( 'npmlog' )
9+ const npa = require ( 'npm-package-arg' )
10+ const npm = require ( './npm.js' )
11+ const output = require ( './utils/output.js' )
12+ const usage = require ( './utils/usage.js' )
13+ const whoami = require ( './whoami.js' )
214
3- var npm = require ( './npm.js' )
4- var log = require ( 'npmlog' )
5- var asyncMap = require ( 'slide' ) . asyncMap
6- var mapToRegistry = require ( './utils/map-to-registry.js' )
7- var usage = require ( './utils/usage' )
8- var output = require ( './utils/output.js' )
15+ const StarConfig = figgyPudding ( {
16+ 'unicode' : { }
17+ } )
918
1019star . usage = usage (
1120 'star' ,
@@ -19,27 +28,50 @@ star.completion = function (opts, cb) {
1928 cb ( )
2029}
2130
31+ module . exports = star
2232function star ( args , cb ) {
23- if ( ! args . length ) return cb ( star . usage )
24- var s = npm . config . get ( 'unicode' ) ? '\u2605 ' : '(*)'
25- var u = npm . config . get ( 'unicode' ) ? '\u2606 ' : '( )'
26- var using = ! ( npm . command . match ( / ^ u n / ) )
27- if ( ! using ) s = u
28- asyncMap ( args , function ( pkg , cb ) {
29- mapToRegistry ( pkg , npm . config , function ( er , uri , auth ) {
30- if ( er ) return cb ( er )
33+ const config = StarConfig ( npmConfig ( ) )
34+ return BB . try ( ( ) => {
35+ if ( ! args . length ) throw new Error ( star . usage )
36+ let s = config . get ( 'unicode' ) ? '\u2605 ' : '(*)'
37+ const u = config . get ( 'unicode' ) ? '\u2606 ' : '( )'
38+ const using = ! ( npm . command . match ( / ^ u n / ) )
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 , config . 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+ }
3155
32- var params = {
33- starred : using ,
34- auth : auth
35- }
36- npm . registry . star ( uri , params , function ( er , data , raw , req ) {
37- if ( ! er ) {
38- output ( s + ' ' + pkg )
39- log . verbose ( 'star' , data )
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 )
4064 }
41- cb ( er , data , raw , req )
65+ return fetch . json ( pkg . escapedName , config . 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
4274 } )
4375 } )
44- } , cb )
76+ } ) . nodeify ( cb )
4577}
0 commit comments