Skip to content

Commit f6725f3

Browse files
authored
Merge pull request #14 from kopertop/feature/invoke-local
Support for invoke local
2 parents 31a971f + 6d0f84b commit f6725f3

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/index.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ServerlessPlugin {
1616

1717
serverless: ServerlessInstance
1818
options: ServerlessOptions
19+
commands:{ [key: string]: any }
1920
hooks: { [key: string]: Function }
2021

2122
constructor(serverless: ServerlessInstance, options: ServerlessOptions) {
@@ -25,6 +26,31 @@ class ServerlessPlugin {
2526
this.hooks = {
2627
'before:deploy:createDeploymentArtifacts': this.beforeCreateDeploymentArtifacts.bind(this),
2728
'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+
},
2854
}
2955
}
3056

@@ -54,7 +80,9 @@ class ServerlessPlugin {
5480
await typescript.run(tsFileNames, tsconfig)
5581

5682
// 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+
}
5886

5987
// include any "extras" from the "include" section
6088
if (this.serverless.service.package.include && this.serverless.service.package.include.length > 0){
@@ -76,9 +104,6 @@ class ServerlessPlugin {
76104
}
77105

78106
async afterCreateDeploymentArtifacts(): Promise<void> {
79-
// Restore service path
80-
this.serverless.config.servicePath = this.originalServicePath
81-
82107
// Copy .build to .serverless
83108
await fs.copy(
84109
path.join(this.originalServicePath, buildFolder, serverlessFolder),
@@ -87,9 +112,17 @@ class ServerlessPlugin {
87112

88113
this.serverless.service.package.artifact = path.join(this.originalServicePath, serverlessFolder, path.basename(this.serverless.service.package.artifact))
89114

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
90122
// Remove temp build folder
91123
fs.removeSync(path.join(this.originalServicePath, buildFolder))
92124
}
125+
93126
}
94127

95128
module.exports = ServerlessPlugin

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ export interface ServerlessInstance {
88
service: {
99
functions: { [key: string]: ServerlessFunction }
1010
package: ServerlessPackage
11+
getFunction: (name: string) => any
1112
}
1213
}
1314

1415
export interface ServerlessOptions {
15-
16+
function?: string
17+
extraServicePath?: string
1618
}
1719

1820
export interface ServerlessFunction {

0 commit comments

Comments
 (0)