1- import * as assert from 'assert'
2- import * as util from 'util'
1+ import * as assert from 'assert'
2+ import * as util from 'util'
33
44import StackOutputFile from './file'
55
6- class StackOutputPlugin {
6+ export default class StackOutputPlugin {
77 public hooks : { }
88 private output : OutputConfig
99
10- constructor ( private serverless : Serverless , private options : Serverless . Options ) {
10+ constructor (
11+ private serverless : Serverless ,
12+ private options : Serverless . Options
13+ ) {
1114 this . hooks = {
1215 'after:deploy:deploy' : this . process . bind ( this )
1316 }
@@ -24,7 +27,10 @@ class StackOutputPlugin {
2427 }
2528
2629 get stackName ( ) {
27- return this . serverless . service . getServiceName ( ) + '-' + this . serverless . getProvider ( 'aws' ) . getStage ( )
30+ return util . format ( '%s-%s' ,
31+ this . serverless . service . getServiceName ( ) ,
32+ this . serverless . getProvider ( 'aws' ) . getStage ( )
33+ )
2834 }
2935
3036 private hasConfig ( key : string ) {
@@ -40,76 +46,84 @@ class StackOutputPlugin {
4046 }
4147
4248 private getConfig ( key : string ) {
43- return this . serverless . config . servicePath + '/' + this . output [ key ]
49+ return util . format ( '%s/%s' ,
50+ this . serverless . config . servicePath ,
51+ this . output [ key ]
52+ )
4453 }
4554
46- private callHandler ( data : { } ) {
55+ private callHandler ( data : object ) {
4756 const splits = this . handler . split ( '.' )
4857 const func = splits . pop ( ) || ''
58+ const file = splits . join ( '.' )
4959
50- return new Promise ( ( resolve ) => {
51- require ( splits . join ( '.' ) ) [ func ] ( data , this . serverless )
60+ require ( file ) [ func ] (
61+ data ,
62+ this . serverless ,
63+ this . options
64+ )
5265
53- resolve ( )
54- } )
66+ return Promise . resolve ( )
5567 }
5668
57- private saveFile ( data : { } ) {
69+ private saveFile ( data : object ) {
5870 const f = new StackOutputFile ( this . file )
5971
60- return new Promise ( ( resolve ) => {
61- f . save ( data )
62-
63- resolve ( )
64- } )
72+ return f . save ( data )
6573 }
6674
6775 private fetch ( ) : Promise < StackDescriptionList > {
6876 return this . serverless . getProvider ( 'aws' ) . request (
6977 'CloudFormation' ,
7078 'describeStacks' ,
71- {
72- StackName : this . stackName
73- } ,
79+ { StackName : this . stackName } ,
7480 this . serverless . getProvider ( 'aws' ) . getStage ( ) ,
7581 this . serverless . getProvider ( 'aws' ) . getRegion ( )
7682 )
7783 }
7884
79- private beautify ( data : { Stacks : Array < { Outputs : Array < { } > } > } ) {
85+ private beautify ( data : { Stacks : Array < { Outputs : StackOutputPair [ ] } > } ) {
8086 const stack = data . Stacks . pop ( ) || { Outputs : [ ] }
8187 const output = stack . Outputs || [ ]
8288
8389 return output . reduce (
84- ( obj : { } , item : StackOutputPair ) => Object . assign ( obj , { [ item . OutputKey ] : item . OutputValue } ) ,
85- { }
90+ ( obj , item : StackOutputPair ) => (
91+ Object . assign ( obj , { [ item . OutputKey ] : item . OutputValue } )
92+ ) , { }
8693 )
8794 }
8895
89- private handle ( data : { } ) {
90- const promises = [ ]
96+ private handle ( data : object ) {
97+ return Promise . all (
98+ [
99+ this . handleHandler ( data ) ,
100+ this . handleFile ( data )
101+ ]
102+ )
103+ }
91104
92- if ( this . hasHandler ( ) ) {
93- promises . push (
94- this . callHandler ( data ) . then (
95- ( ) => this . serverless . cli . log (
96- util . format ( 'Stack Output processed with handler: %s' , this . output . handler )
97- )
105+ private handleHandler ( data : object ) {
106+ return this . hasHandler ( ) ? (
107+ this . callHandler (
108+ data
109+ ) . then (
110+ ( ) => this . serverless . cli . log (
111+ util . format ( 'Stack Output processed with handler: %s' , this . output . handler )
98112 )
99113 )
100- }
114+ ) : Promise . resolve ( )
115+ }
101116
102- if ( this . hasFile ( ) ) {
103- promises . push (
104- this . saveFile ( data ) . then (
105- ( ) => this . serverless . cli . log (
106- util . format ( 'Stack Output saved to file: %s' , this . output . file )
107- )
117+ private handleFile ( data : object ) {
118+ return this . hasFile ( ) ? (
119+ this . saveFile (
120+ data
121+ ) . then (
122+ ( ) => this . serverless . cli . log (
123+ util . format ( 'Stack Output saved to file: %s' , this . output . file )
108124 )
109125 )
110- }
111-
112- return Promise . all ( promises )
126+ ) : Promise . resolve ( )
113127 }
114128
115129 private validate ( ) {
@@ -123,20 +137,19 @@ class StackOutputPlugin {
123137 }
124138
125139 private process ( ) {
126- console . log ( 'running stack-output-plugin' )
127-
128- return Promise . resolve ( ) . then (
140+ Promise . resolve ( )
141+ . then (
129142 ( ) => this . validate ( )
130143 ) . then (
131144 ( ) => this . fetch ( )
132145 ) . then (
133- ( res : StackDescriptionList ) => this . beautify ( res )
146+ ( res ) => this . beautify ( res )
134147 ) . then (
135148 ( res ) => this . handle ( res )
136149 ) . catch (
137- ( err ) => this . serverless . cli . log ( util . format ( 'Cannot process Stack Output: %s!' , err . message ) )
150+ ( err ) => this . serverless . cli . log (
151+ util . format ( 'Cannot process Stack Output: %s!' , err . message )
152+ )
138153 )
139154 }
140155}
141-
142- module . exports = StackOutputPlugin
0 commit comments