@@ -16,6 +16,7 @@ class ServerlessPlugin {
16
16
17
17
serverless : ServerlessInstance
18
18
options : ServerlessOptions
19
+ commands :{ [ key : string ] : any }
19
20
hooks : { [ key : string ] : Function }
20
21
21
22
constructor ( serverless : ServerlessInstance , options : ServerlessOptions ) {
@@ -25,6 +26,31 @@ class ServerlessPlugin {
25
26
this . hooks = {
26
27
'before:deploy:createDeploymentArtifacts' : this . beforeCreateDeploymentArtifacts . bind ( this ) ,
27
28
'after:deploy:createDeploymentArtifacts' : this . afterCreateDeploymentArtifacts . bind ( this ) ,
29
+ 'before:invoke:local:invoke' : this . beforeCreateDeploymentArtifacts . bind ( this ) ,
30
+ 'after:invoke:local:invoke' : this . cleanup . bind ( this ) ,
31
+ }
32
+ this . commands = {
33
+ ts : {
34
+ commands : {
35
+ invoke : {
36
+ usage : 'Run a function locally from the tsc output bundle' ,
37
+ lifecycleEvents : [
38
+ 'invoke' ,
39
+ ] ,
40
+ options : {
41
+ function : {
42
+ usage : 'Name of the function' ,
43
+ shortcut : 'f' ,
44
+ required : true ,
45
+ } ,
46
+ path : {
47
+ usage : 'Path to JSON file holding input data' ,
48
+ shortcut : 'p' ,
49
+ } ,
50
+ } ,
51
+ } ,
52
+ } ,
53
+ } ,
28
54
}
29
55
}
30
56
@@ -54,7 +80,9 @@ class ServerlessPlugin {
54
80
await typescript . run ( tsFileNames , tsconfig )
55
81
56
82
// include node_modules into build
57
- fs . symlinkSync ( path . resolve ( 'node_modules' ) , path . resolve ( path . join ( buildFolder , 'node_modules' ) ) )
83
+ if ( ! fs . existsSync ( path . resolve ( path . join ( buildFolder , 'node_modules' ) ) ) ) {
84
+ fs . symlinkSync ( path . resolve ( 'node_modules' ) , path . resolve ( path . join ( buildFolder , 'node_modules' ) ) )
85
+ }
58
86
59
87
// include any "extras" from the "include" section
60
88
if ( this . serverless . service . package . include && this . serverless . service . package . include . length > 0 ) {
@@ -76,9 +104,6 @@ class ServerlessPlugin {
76
104
}
77
105
78
106
async afterCreateDeploymentArtifacts ( ) : Promise < void > {
79
- // Restore service path
80
- this . serverless . config . servicePath = this . originalServicePath
81
-
82
107
// Copy .build to .serverless
83
108
await fs . copy (
84
109
path . join ( this . originalServicePath , buildFolder , serverlessFolder ) ,
@@ -87,9 +112,17 @@ class ServerlessPlugin {
87
112
88
113
this . serverless . service . package . artifact = path . join ( this . originalServicePath , serverlessFolder , path . basename ( this . serverless . service . package . artifact ) )
89
114
115
+ // Cleanup after everything is copied
116
+ await this . cleanup ( ) ;
117
+ }
118
+
119
+ async cleanup ( ) : Promise < void > {
120
+ // Restore service path
121
+ this . serverless . config . servicePath = this . originalServicePath
90
122
// Remove temp build folder
91
123
fs . removeSync ( path . join ( this . originalServicePath , buildFolder ) )
92
124
}
125
+
93
126
}
94
127
95
128
module . exports = ServerlessPlugin
0 commit comments