Skip to content

Commit

Permalink
feat: ignore function definition image (#215)
Browse files Browse the repository at this point in the history
* feat: ignore functions with images
* chore: refactor logic
* refactor: allow type narrowing before isNodeFunction
* refactor: remove comment

closes #212
  • Loading branch information
samchungy authored Oct 24, 2021
1 parent ef15d1c commit 4d4a53f
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,32 @@ export class EsbuildServerlessPlugin implements ServerlessPlugin {
return Boolean(runtimeMatcher?.[runtime]);
}

/**
* Checks if the function has a handler
* @param {Serverless.FunctionDefinitionHandler | Serverless.FunctionDefinitionImage} func the function to be checked
* @returns {boolean} true if the function has a handler
*/
private isFunctionDefinitionHandler(
func: Serverless.FunctionDefinitionHandler | Serverless.FunctionDefinitionImage
): func is Serverless.FunctionDefinitionHandler {
return Boolean((func as Serverless.FunctionDefinitionHandler)?.handler);
}

get functions(): Record<string, Serverless.FunctionDefinitionHandler> {
let functions: typeof this.serverless.service.functions;
if (this.options.function) {
//only return the function if it's a node function:
const func = this.serverless.service.getFunction(this.options.function) as Serverless.FunctionDefinitionHandler;
return this.isNodeFunction(func) ? { [this.options.function]: func } : {};
functions = {
[this.options.function]: this.serverless.service.getFunction(this.options.function),
};
} else {
functions = this.serverless.service.functions;
}

// ignore all functions with a different runtime than nodejs:
const nodeFunctions: Record<string, Serverless.FunctionDefinitionHandler> = {};
const functions = this.serverless.service.functions;
for(const funcName in functions) {
for (const funcName in functions) {
const func = functions[funcName] as Serverless.FunctionDefinitionHandler;
if (this.isNodeFunction(func)) {
if (this.isFunctionDefinitionHandler(func) && this.isNodeFunction(func)) {
nodeFunctions[funcName] = func;
}
}
Expand Down

0 comments on commit 4d4a53f

Please sign in to comment.