33const BbPromise = require ( 'bluebird' ) ;
44const path = require ( 'path' ) ;
55const webpack = require ( 'webpack' ) ;
6+ const utils = require ( './utils' ) ;
67
78module . exports = {
8- loadFunction ( stats ) {
9- const functionName = this . options . function ;
9+ loadHandler ( stats ) {
1010 const handlerFilePath = path . join (
1111 stats . compilation . options . output . path ,
1212 stats . compilation . options . output . filename
1313 ) ;
14- purgeCache ( handlerFilePath ) ;
15- const handler = require ( handlerFilePath ) ;
16-
17- return handler [ functionName ] ;
14+ utils . purgeCache ( handlerFilePath ) ;
15+ return require ( handlerFilePath ) ;
1816 } ,
1917
2018 getEvent ( ) {
@@ -23,11 +21,10 @@ module.exports = {
2321 : null ; // TODO use get-stdin instead
2422 } ,
2523
26- getContext ( ) {
27- const functionName = this . options . function ;
24+ getContext ( functionName ) {
2825 return {
29- awsRequestId : guid ( ) ,
30- invokeid : guid ( ) ,
26+ awsRequestId : utils . guid ( ) ,
27+ invokeid : utils . guid ( ) ,
3128 logGroupName : `/aws/lambda/${ functionName } ` ,
3229 logStreamName : '2016/02/14/[HEAD]13370a84ca4ed8b77c427af260' ,
3330 functionVersion : '$LATEST' ,
@@ -42,9 +39,9 @@ module.exports = {
4239
4340 this . serverless . cli . log ( `Run function ${ functionName } ...` ) ;
4441
45- const handler = this . loadFunction ( stats ) ;
42+ const handler = this . loadHandler ( stats ) [ functionName ] ;
4643 const event = this . getEvent ( ) ;
47- const context = this . getContext ( ) ;
44+ const context = this . getContext ( functionName ) ;
4845
4946 return new BbPromise ( ( resolve , reject ) => handler (
5047 event ,
@@ -70,9 +67,9 @@ module.exports = {
7067 throw err ;
7168 }
7269 this . serverless . cli . log ( `Run function ${ functionName } ...` ) ;
73- const handler = this . loadFunction ( stats ) ;
70+ const handler = this . loadHandler ( stats ) [ functionName ] ;
7471 const event = this . getEvent ( ) ;
75- const context = this . getContext ( ) ;
72+ const context = this . getContext ( functionName ) ;
7673 handler (
7774 event ,
7875 context ,
@@ -89,35 +86,3 @@ module.exports = {
8986 return BbPromise . resolve ( ) ;
9087 } ,
9188} ;
92-
93- function guid ( ) {
94- function s4 ( ) {
95- return Math . floor ( ( 1 + Math . random ( ) ) * 0x10000 )
96- . toString ( 16 )
97- . substring ( 1 ) ;
98- }
99- return s4 ( ) + s4 ( ) + '-' + s4 ( ) + '-' + s4 ( ) + '-' + s4 ( ) + '-' + s4 ( ) + s4 ( ) + s4 ( ) ;
100- }
101-
102- function purgeCache ( moduleName ) {
103- searchCache ( moduleName , function ( mod ) {
104- delete require . cache [ mod . id ] ;
105- } ) ;
106- Object . keys ( module . constructor . _pathCache ) . forEach ( function ( cacheKey ) {
107- if ( cacheKey . indexOf ( moduleName ) > 0 ) {
108- delete module . constructor . _pathCache [ cacheKey ] ;
109- }
110- } ) ;
111- }
112-
113- function searchCache ( moduleName , callback ) {
114- var mod = require . resolve ( moduleName ) ;
115- if ( mod && ( ( mod = require . cache [ mod ] ) !== undefined ) ) {
116- ( function traverse ( mod ) {
117- mod . children . forEach ( function ( child ) {
118- traverse ( child ) ;
119- } ) ;
120- callback ( mod ) ;
121- } ( mod ) ) ;
122- }
123- }
0 commit comments