1
1
/* eslint-disable standard/no-callback-literal */
2
- var mapToRegistry = require ( './utils/map-to-registry.js' )
3
- var npm = require ( './npm' )
4
- var output = require ( './utils/output.js' )
2
+
3
+ const BB = require ( 'bluebird' )
4
+
5
+ const npmConfig = require ( './config/figgy-config.js' )
6
+ const npmFetch = require ( 'npm-registry-fetch' )
7
+ const output = require ( './utils/output.js' )
8
+ const usage = require ( './utils/usage' )
9
+ const validate = require ( 'aproba' )
5
10
6
11
module . exports = team
7
12
8
13
team . subcommands = [ 'create' , 'destroy' , 'add' , 'rm' , 'ls' , 'edit' ]
9
14
10
- team . usage =
15
+ team . usage = usage (
16
+ 'team' ,
11
17
'npm team create <scope:team>\n' +
12
18
'npm team destroy <scope:team>\n' +
13
19
'npm team add <scope:team> <user>\n' +
14
20
'npm team rm <scope:team> <user>\n' +
15
21
'npm team ls <scope>|<scope:team>\n' +
16
22
'npm team edit <scope:team>'
23
+ )
24
+
25
+ function UsageError ( ) {
26
+ throw Object . assign ( new Error ( team . usage ) , { code : 'EUSAGE' } )
27
+ }
17
28
18
29
team . completion = function ( opts , cb ) {
19
30
var argv = opts . conf . argv . remain
@@ -33,24 +44,62 @@ team.completion = function (opts, cb) {
33
44
}
34
45
}
35
46
36
- function team ( args , cb ) {
47
+ const eu = encodeURIComponent
48
+
49
+ function team ( [ cmd , entity = '' , user = '' ] , cb ) {
37
50
// Entities are in the format <scope>:<team>
38
- var cmd = args . shift ( )
39
- var entity = ( args . shift ( ) || '' ) . split ( ':' )
40
- return mapToRegistry ( '/' , npm . config , function ( err , uri , auth ) {
41
- if ( err ) { return cb ( err ) }
42
- try {
43
- return npm . registry . team ( cmd , uri , {
44
- auth : auth ,
45
- scope : entity [ 0 ] . replace ( / ^ @ / , '' ) , // '@' prefix on scope is optional.
46
- team : entity [ 1 ] ,
47
- user : args . shift ( )
48
- } , function ( err , data ) {
49
- ! err && data && output ( JSON . stringify ( data , undefined , 2 ) )
50
- cb ( err , data )
51
- } )
52
- } catch ( e ) {
53
- cb ( e . message + '\n\nUsage:\n' + team . usage )
51
+ return BB . try ( ( ) => {
52
+ let [ scope , team ] = entity . split ( ':' )
53
+ scope = scope . replace ( / ^ @ / , '' )
54
+ const opts = npmConfig ( ) . concat ( {
55
+ scope : `@${ scope } `
56
+ } )
57
+ let uri
58
+ switch ( cmd ) {
59
+ case 'create' :
60
+ validate ( 'SS' , [ scope , team ] )
61
+ uri = `/-/org/${ eu ( scope ) } /team`
62
+ return npmFetch . json ( uri , opts . concat ( {
63
+ method : 'PUT' ,
64
+ body : { name : team }
65
+ } ) )
66
+ case 'ls' :
67
+ if ( team ) {
68
+ validate ( 'SS' , [ scope , team ] )
69
+ uri = `/-/team/${ eu ( scope ) } /${ eu ( team ) } /user`
70
+ } else {
71
+ validate ( 'S' , [ scope ] )
72
+ uri = `/-/org/${ eu ( scope ) } /team`
73
+ }
74
+ return npmFetch . json ( uri , opts . concat ( { query : { format : 'cli' } } ) )
75
+ case 'destroy' :
76
+ validate ( 'SS' , [ scope , team ] )
77
+ uri = `/-/team/${ eu ( scope ) } /${ eu ( team ) } `
78
+ return npmFetch . json ( uri , opts . concat ( { method : 'DELETE' } ) )
79
+ case 'add' :
80
+ validate ( 'SSS' , [ scope , team , user ] )
81
+ uri = `/-/team/${ eu ( scope ) } /${ eu ( team ) } /user`
82
+ return npmFetch ( uri , opts . concat ( {
83
+ method : 'PUT' ,
84
+ body : { user}
85
+ } ) ) . then ( ( ) => ( { } ) )
86
+ case 'rm' :
87
+ validate ( 'SSS' , [ scope , team , user ] )
88
+ uri = `/-/team/${ eu ( scope ) } /${ eu ( team ) } /user`
89
+ return npmFetch ( uri , opts . concat ( {
90
+ method : 'DELETE' ,
91
+ body : { user}
92
+ } ) ) . then ( ( ) => ( { } ) )
93
+ case 'edit' :
94
+ throw new Error ( '`npm team edit` is not implemented yet.' )
95
+ default :
96
+ UsageError ( )
54
97
}
55
- } )
98
+ } ) . then (
99
+ data => {
100
+ data && output ( JSON . stringify ( data , undefined , 2 ) )
101
+ cb ( null , data )
102
+ } ,
103
+ err => err . code === 'EUSAGE' ? cb ( err . message ) : cb ( err )
104
+ )
56
105
}
0 commit comments