From b6db865924b4de0f5e4283c1db9d6d6f65763433 Mon Sep 17 00:00:00 2001 From: Chris Moyer Date: Tue, 23 May 2017 12:51:05 -0400 Subject: [PATCH 1/2] Support for invoke local --- src/index.ts | 38 +++++++++++++++++++++++++++++++++++++- src/types.ts | 4 +++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index e07a28ce..47cd0405 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ class ServerlessPlugin { serverless: ServerlessInstance options: ServerlessOptions + commands:{ [key: string]: any } hooks: { [key: string]: Function } constructor(serverless: ServerlessInstance, options: ServerlessOptions) { @@ -25,6 +26,31 @@ class ServerlessPlugin { this.hooks = { 'before:deploy:createDeploymentArtifacts': this.beforeCreateDeploymentArtifacts.bind(this), 'after:deploy:createDeploymentArtifacts': this.afterCreateDeploymentArtifacts.bind(this), + 'before:invoke:local:invoke': this.beforeCreateDeploymentArtifacts.bind(this), + 'after:invoke:local:invoke': this.cleanup.bind(this), + } + this.commands = { + ts: { + commands: { + invoke: { + usage: 'Run a function locally from the tsc output bundle', + lifecycleEvents: [ + 'invoke', + ], + options: { + function: { + usage: 'Name of the function', + shortcut: 'f', + required: true, + }, + path: { + usage: 'Path to JSON file holding input data', + shortcut: 'p', + }, + }, + }, + }, + }, } } @@ -54,7 +80,9 @@ class ServerlessPlugin { await typescript.run(tsFileNames, tsconfig) // include node_modules into build - fs.symlinkSync(path.resolve('node_modules'), path.resolve(path.join(buildFolder, 'node_modules'))) + if (!fs.existsSync(path.resolve(path.join(buildFolder, 'node_modules')))) { + fs.symlinkSync(path.resolve('node_modules'), path.resolve(path.join(buildFolder, 'node_modules'))) + } // include any "extras" from the "include" section if (this.serverless.service.package.include && this.serverless.service.package.include.length > 0){ @@ -90,6 +118,14 @@ class ServerlessPlugin { // Remove temp build folder fs.removeSync(path.join(this.originalServicePath, buildFolder)) } + + async cleanup(): Promise { + // Restore service path + this.serverless.config.servicePath = this.originalServicePath + // Remove temp build folder + fs.removeSync(path.join(this.originalServicePath, buildFolder)) + } + } module.exports = ServerlessPlugin diff --git a/src/types.ts b/src/types.ts index a7d8740f..8a7110ad 100644 --- a/src/types.ts +++ b/src/types.ts @@ -8,11 +8,13 @@ export interface ServerlessInstance { service: { functions: { [key: string]: ServerlessFunction } package: ServerlessPackage + getFunction: (name: string) => any } } export interface ServerlessOptions { - + function?: string + extraServicePath?: string } export interface ServerlessFunction { From 6d0f84b2c4985f22eebc69b6b24dbf1c26670436 Mon Sep 17 00:00:00 2001 From: Chris Moyer Date: Wed, 24 May 2017 09:31:33 -0400 Subject: [PATCH 2/2] Reduce redundant code by calling cleanup after the "after" function. --- src/index.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 47cd0405..a70763f5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -104,9 +104,6 @@ class ServerlessPlugin { } async afterCreateDeploymentArtifacts(): Promise { - // Restore service path - this.serverless.config.servicePath = this.originalServicePath - // Copy .build to .serverless await fs.copy( path.join(this.originalServicePath, buildFolder, serverlessFolder), @@ -115,8 +112,8 @@ class ServerlessPlugin { this.serverless.service.package.artifact = path.join(this.originalServicePath, serverlessFolder, path.basename(this.serverless.service.package.artifact)) - // Remove temp build folder - fs.removeSync(path.join(this.originalServicePath, buildFolder)) + // Cleanup after everything is copied + await this.cleanup(); } async cleanup(): Promise {