22
33const BB = require ( 'bluebird' )
44
5- const crypto = require ( 'crypto' )
6- const hookApi = require ( 'libnpmhook' )
7- const log = require ( 'npmlog' )
8- const npm = require ( './npm.js' )
5+ const hookApi = require ( 'libnpm/hook' )
6+ const npmConfig = require ( './config/figgy-config.js' )
97const output = require ( './utils/output.js' )
8+ const otplease = require ( './utils/otplease.js' )
109const pudding = require ( 'figgy-pudding' )
1110const relativeDate = require ( 'tiny-relative-date' )
1211const Table = require ( 'cli-table3' )
@@ -25,106 +24,116 @@ hook.completion = (opts, cb) => {
2524 return cb ( null , [ ] ) // fill in this array with completion values
2625}
2726
28- const npmSession = crypto . randomBytes ( 8 ) . toString ( 'hex' )
29- const hookConfig = pudding ( )
30- function config ( ) {
31- return hookConfig ( {
32- refer : npm . refer ,
33- projectScope : npm . projectScope ,
34- log,
35- npmSession
36- } , npm . config )
37- }
27+ const HookConfig = pudding ( {
28+ json : { } ,
29+ loglevel : { } ,
30+ parseable : { } ,
31+ silent : { } ,
32+ unicode : { }
33+ } )
3834
3935module . exports = ( args , cb ) => BB . try ( ( ) => hook ( args ) ) . nodeify ( cb )
4036function hook ( args ) {
41- switch ( args [ 0 ] ) {
42- case 'add' :
43- return add ( args [ 1 ] , args [ 2 ] , args [ 3 ] )
44- case 'ls' :
45- return ls ( args [ 1 ] )
46- case 'rm' :
47- return rm ( args [ 1 ] )
48- case 'update' :
49- case 'up' :
50- return update ( args [ 1 ] , args [ 2 ] , args [ 3 ] )
51- }
37+ return otplease ( npmConfig ( ) , opts => {
38+ opts = HookConfig ( opts )
39+ switch ( args [ 0 ] ) {
40+ case 'add' :
41+ return add ( args [ 1 ] , args [ 2 ] , args [ 3 ] , opts )
42+ case 'ls' :
43+ return ls ( args [ 1 ] , opts )
44+ case 'rm' :
45+ return rm ( args [ 1 ] , opts )
46+ case 'update' :
47+ case 'up' :
48+ return update ( args [ 1 ] , args [ 2 ] , args [ 3 ] , opts )
49+ }
50+ } )
5251}
5352
54- function add ( pkg , uri , secret ) {
55- return hookApi . add ( pkg , uri , secret , config ( ) )
56- . then ( ( hook ) => {
57- if ( npm . config . get ( 'json' ) ) {
58- output ( JSON . stringify ( hook , null , 2 ) )
59- } else {
60- output ( `+ ${ hookName ( hook ) } ${
61- npm . config . get ( 'unicode' ) ? ' ➜ ' : ' -> '
62- } ${ hook . endpoint } `)
63- }
64- } )
53+ function add ( pkg , uri , secret , opts ) {
54+ return hookApi . add ( pkg , uri , secret , opts ) . then ( hook => {
55+ if ( opts . json ) {
56+ output ( JSON . stringify ( hook , null , 2 ) )
57+ } else if ( opts . parseable ) {
58+ output ( Object . keys ( hook ) . join ( '\t' ) )
59+ output ( Object . keys ( hook ) . map ( k => hook [ k ] ) . join ( '\t' ) )
60+ } else if ( ! opts . silent && opts . loglevel !== 'silent' ) {
61+ output ( `+ ${ hookName ( hook ) } ${
62+ opts . unicode ? ' ➜ ' : ' -> '
63+ } ${ hook . endpoint } `)
64+ }
65+ } )
6566}
6667
67- function ls ( pkg ) {
68- return hookApi . ls ( pkg , config ( ) )
69- . then ( ( hooks ) => {
70- if ( npm . config . get ( 'json' ) ) {
71- output ( JSON . stringify ( hooks , null , 2 ) )
72- } else if ( ! hooks . length ) {
73- output ( "You don't have any hooks configured yet." )
68+ function ls ( pkg , opts ) {
69+ return hookApi . ls ( opts . concat ( { package : pkg } ) ) . then ( hooks => {
70+ if ( opts . json ) {
71+ output ( JSON . stringify ( hooks , null , 2 ) )
72+ } else if ( opts . parseable ) {
73+ output ( Object . keys ( hooks [ 0 ] ) . join ( '\t' ) )
74+ hooks . forEach ( hook => {
75+ output ( Object . keys ( hook ) . map ( k => hook [ k ] ) . join ( '\t' ) )
76+ } )
77+ } else if ( ! hooks . length ) {
78+ output ( "You don't have any hooks configured yet." )
79+ } else if ( ! opts . silent && opts . loglevel !== 'silent' ) {
80+ if ( hooks . length === 1 ) {
81+ output ( 'You have one hook configured.' )
7482 } else {
75- if ( hooks . length === 1 ) {
76- output ( 'You have one hook configured.' )
77- } else {
78- output ( `You have ${ hooks . length } hooks configured.` )
79- }
80- const table = new Table ( { head : [ 'id' , 'target' , 'endpoint' ] } )
81- hooks . forEach ( ( hook ) => {
83+ output ( `You have ${ hooks . length } hooks configured.` )
84+ }
85+ const table = new Table ( { head : [ 'id' , 'target' , 'endpoint' ] } )
86+ hooks . forEach ( ( hook ) => {
87+ table . push ( [
88+ { rowSpan : 2 , content : hook . id } ,
89+ hookName ( hook ) ,
90+ hook . endpoint
91+ ] )
92+ if ( hook . last_delivery ) {
8293 table . push ( [
83- { rowSpan : 2 , content : hook . id } ,
84- hookName ( hook ) ,
85- hook . endpoint
94+ {
95+ colSpan : 1 ,
96+ content : `triggered ${ relativeDate ( hook . last_delivery ) } `
97+ } ,
98+ hook . response_code
8699 ] )
87- if ( hook . last_delivery ) {
88- table . push ( [
89- {
90- colSpan : 1 ,
91- content : `triggered ${ relativeDate ( hook . last_delivery ) } `
92- } ,
93- hook . response_code
94- ] )
95- } else {
96- table . push ( [ { colSpan : 2 , content : 'never triggered' } ] )
97- }
98- } )
99- output ( table . toString ( ) )
100- }
101- } )
100+ } else {
101+ table . push ( [ { colSpan : 2 , content : 'never triggered' } ] )
102+ }
103+ } )
104+ output ( table . toString ( ) )
105+ }
106+ } )
102107}
103108
104- function rm ( id ) {
105- return hookApi . rm ( id , config ( ) )
106- . then ( ( hook ) => {
107- if ( npm . config . get ( 'json' ) ) {
108- output ( JSON . stringify ( hook , null , 2 ) )
109- } else {
110- output ( `- ${ hookName ( hook ) } ${
111- npm . config . get ( 'unicode' ) ? ' ✘ ' : ' X '
112- } ${ hook . endpoint } `)
113- }
114- } )
109+ function rm ( id , opts ) {
110+ return hookApi . rm ( id , opts ) . then ( hook => {
111+ if ( opts . json ) {
112+ output ( JSON . stringify ( hook , null , 2 ) )
113+ } else if ( opts . parseable ) {
114+ output ( Object . keys ( hook ) . join ( '\t' ) )
115+ output ( Object . keys ( hook ) . map ( k => hook [ k ] ) . join ( '\t' ) )
116+ } else if ( ! opts . silent && opts . loglevel !== 'silent' ) {
117+ output ( `- ${ hookName ( hook ) } ${
118+ opts . unicode ? ' ✘ ' : ' X '
119+ } ${ hook . endpoint } `)
120+ }
121+ } )
115122}
116123
117- function update ( id , uri , secret ) {
118- return hookApi . update ( id , uri , secret , config ( ) )
119- . then ( ( hook ) => {
120- if ( npm . config . get ( 'json' ) ) {
121- output ( JSON . stringify ( hook , null , 2 ) )
122- } else {
123- output ( `+ ${ hookName ( hook ) } ${
124- npm . config . get ( 'unicode' ) ? ' ➜ ' : ' -> '
125- } ${ hook . endpoint } `)
126- }
127- } )
124+ function update ( id , uri , secret , opts ) {
125+ return hookApi . update ( id , uri , secret , opts ) . then ( hook => {
126+ if ( opts . json ) {
127+ output ( JSON . stringify ( hook , null , 2 ) )
128+ } else if ( opts . parseable ) {
129+ output ( Object . keys ( hook ) . join ( '\t' ) )
130+ output ( Object . keys ( hook ) . map ( k => hook [ k ] ) . join ( '\t' ) )
131+ } else if ( ! opts . silent && opts . loglevel !== 'silent' ) {
132+ output ( `+ ${ hookName ( hook ) } ${
133+ opts . unicode ? ' ➜ ' : ' -> '
134+ } ${ hook . endpoint } `)
135+ }
136+ } )
128137}
129138
130139function hookName ( hook ) {
0 commit comments