1- 'use strict'
1+ import * as assert from 'assert'
2+ import * as util from 'util'
23
3- const util = require ( 'util' )
4- const assert = require ( 'assert' )
4+ import StackOutputFile from './file'
55
6- const File = require ( './file.js' )
7-
8- class Plugin {
9- constructor ( serverless , options ) {
10- this . options = options
11- this . serverless = serverless
6+ class StackOutputPlugin {
7+ public hooks : { }
8+ private output : OutputConfig
129
10+ constructor ( private serverless : Serverless , private options : Serverless . Options ) {
1311 this . hooks = {
14- 'after:deploy:deploy' : ( ) => this . process ( )
12+ 'after:deploy:deploy' : this . process . bind ( this )
1513 }
14+
15+ this . output = this . serverless . service . custom . output
1616 }
1717
1818 get file ( ) {
@@ -24,47 +24,47 @@ class Plugin {
2424 }
2525
2626 get stackName ( ) {
27- return this . serverless . service . service + '-' + this . serverless . getProvider ( 'aws' ) . getStage ( )
27+ return this . serverless . service . getServiceName ( ) + '-' + this . serverless . getProvider ( 'aws' ) . getStage ( )
2828 }
2929
30- hasConfig ( key ) {
31- return ! ! this . serverless . service . custom . output && ! ! this . serverless . service . custom . output [ key ]
30+ private hasConfig ( key : string ) {
31+ return ! ! this . output && ! ! this . output [ key ]
3232 }
3333
34- hasHandler ( ) {
34+ private hasHandler ( ) {
3535 return this . hasConfig ( 'handler' )
3636 }
3737
38- hasFile ( ) {
38+ private hasFile ( ) {
3939 return this . hasConfig ( 'file' )
4040 }
4141
42- getConfig ( key ) {
43- return this . serverless . config . servicePath + '/' + this . serverless . service . custom . output [ key ]
42+ private getConfig ( key : string ) {
43+ return this . serverless . config . servicePath + '/' + this . output [ key ]
4444 }
4545
46- callHandler ( data ) {
46+ private callHandler ( data : { } ) {
4747 const splits = this . handler . split ( '.' )
48- const func = splits . pop ( )
48+ const func = splits . pop ( ) || ''
4949
50- return new Promise ( resolve => {
50+ return new Promise ( ( resolve ) => {
5151 require ( splits . join ( '.' ) ) [ func ] ( data )
5252
5353 resolve ( )
5454 } )
5555 }
5656
57- saveFile ( data ) {
58- const f = new File ( this . file )
57+ private saveFile ( data : { } ) {
58+ const f = new StackOutputFile ( this . file )
5959
60- return new Promise ( resolve => {
60+ return new Promise ( ( resolve ) => {
6161 f . save ( data )
6262
6363 resolve ( )
6464 } )
6565 }
6666
67- fetch ( ) {
67+ private fetch ( ) : Promise < StackDescriptionList > {
6868 return this . serverless . getProvider ( 'aws' ) . request (
6969 'CloudFormation' ,
7070 'describeStacks' ,
@@ -76,21 +76,24 @@ class Plugin {
7676 )
7777 }
7878
79- beautify ( data ) {
80- return data . Stacks . pop ( ) . Outputs . reduce (
81- ( obj , item ) => Object . assign ( obj , { [ item . OutputKey ] : item . OutputValue } ) ,
79+ private beautify ( data : { Stacks : Array < { Outputs : Array < { } > } > } ) {
80+ const stack = data . Stacks . pop ( ) || { Outputs : [ ] }
81+ const output = stack . Outputs || [ ]
82+
83+ return output . reduce (
84+ ( obj : { } , item : StackOutputPair ) => Object . assign ( obj , { [ item . OutputKey ] : item . OutputValue } ) ,
8285 { }
8386 )
8487 }
8588
86- handle ( data ) {
87- let promises = [ ]
89+ private handle ( data : { } ) {
90+ const promises = [ ]
8891
8992 if ( this . hasHandler ( ) ) {
9093 promises . push (
9194 this . callHandler ( data ) . then (
9295 ( ) => this . serverless . cli . log (
93- util . format ( 'Stack Output processed with handler: %s' , this . serverless . service . custom . output . handler )
96+ util . format ( 'Stack Output processed with handler: %s' , this . output . handler )
9497 )
9598 )
9699 )
@@ -100,7 +103,7 @@ class Plugin {
100103 promises . push (
101104 this . saveFile ( data ) . then (
102105 ( ) => this . serverless . cli . log (
103- util . format ( 'Stack Output saved to file: %s' , this . serverless . service . custom . output . file )
106+ util . format ( 'Stack Output saved to file: %s' , this . output . file )
104107 )
105108 )
106109 )
@@ -109,7 +112,7 @@ class Plugin {
109112 return Promise . all ( promises )
110113 }
111114
112- validate ( ) {
115+ private validate ( ) {
113116 assert ( this . serverless , 'Invalid serverless configuration' )
114117 assert ( this . serverless . service , 'Invalid serverless configuration' )
115118 assert ( this . serverless . service . provider , 'Invalid serverless configuration' )
@@ -119,19 +122,21 @@ class Plugin {
119122 assert ( this . options && ! this . options . noDeploy , 'Skipping deployment with --noDeploy flag' )
120123 }
121124
122- process ( ) {
125+ private process ( ) {
126+ console . log ( 'running stack-output-plugin' )
127+
123128 return Promise . resolve ( ) . then (
124129 ( ) => this . validate ( )
125130 ) . then (
126131 ( ) => this . fetch ( )
127132 ) . then (
128- res => this . beautify ( res )
133+ ( res : StackDescriptionList ) => this . beautify ( res )
129134 ) . then (
130- res => this . handle ( res )
135+ ( res ) => this . handle ( res )
131136 ) . catch (
132- err => this . serverless . cli . log ( util . format ( 'Cannot process Stack Output: %s!' , err . message ) )
137+ ( err ) => this . serverless . cli . log ( util . format ( 'Cannot process Stack Output: %s!' , err . message ) )
133138 )
134139 }
135140}
136141
137- module . exports = Plugin
142+ module . exports = StackOutputPlugin
0 commit comments