From b720e7e1919a915daf24d48ca843ad4c42d9b97c Mon Sep 17 00:00:00 2001 From: Sean Dawson Date: Tue, 28 Jan 2020 14:38:20 +1000 Subject: [PATCH] Only process functions if runtime is node - This change fixes an issue where serverless-plugin-typescript would try to process functions that used different runtimes. - Typescript functions will only run on node runtimes so it makes sense to exclude functions on other runtimes from processing --- src/Serverless.d.ts | 2 + src/index.ts | 1 + src/typescript.ts | 8 +- tests/typescript.extractFileName.test.ts | 93 +++++++++++++++++++----- 4 files changed, 84 insertions(+), 20 deletions(-) diff --git a/src/Serverless.d.ts b/src/Serverless.d.ts index ec7d7049..1a47388d 100644 --- a/src/Serverless.d.ts +++ b/src/Serverless.d.ts @@ -11,6 +11,7 @@ declare namespace Serverless { service: { provider: { name: string + runtime?: string } functions: { [key: string]: Serverless.Function @@ -31,6 +32,7 @@ declare namespace Serverless { interface Function { handler: string package: Serverless.Package + runtime?: string } interface Package { diff --git a/src/index.ts b/src/index.ts index a249ce58..d55c469e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,6 +92,7 @@ export class TypeScriptPlugin { return typescript.extractFileNames( this.originalServicePath, this.serverless.service.provider.name, + this.serverless.service.provider.runtime, this.functions ) } diff --git a/src/typescript.ts b/src/typescript.ts index 22f7354a..a13089bc 100644 --- a/src/typescript.ts +++ b/src/typescript.ts @@ -18,7 +18,7 @@ export function makeDefaultTypescriptConfig() { return defaultTypescriptConfig } -export function extractFileNames(cwd: string, provider: string, functions?: { [key: string]: Serverless.Function }): string[] { +export function extractFileNames(cwd: string, provider: string, globalRuntime?: string, functions?: { [key: string]: Serverless.Function }): string[] { // The Google provider will use the entrypoint not from the definition of the // handler function, but instead from the package.json:main field, or via a // index.js file. This check reads the current package.json in the same way @@ -46,7 +46,13 @@ export function extractFileNames(cwd: string, provider: string, functions?: { [k } } + const runtimeIsNode = (runtime: string) => runtime.toLowerCase().startsWith('node') + const shouldProcessFunction = (fn: Serverless.Function) => + (fn.runtime !== undefined && runtimeIsNode(fn.runtime)) || + (fn.runtime === undefined && (globalRuntime === undefined || runtimeIsNode(globalRuntime))) + return _.values(functions) + .filter(shouldProcessFunction) .map(fn => fn.handler) .map(h => { const fnName = _.last(h.split('.')) diff --git a/tests/typescript.extractFileName.test.ts b/tests/typescript.extractFileName.test.ts index 82fac1d8..90046cfb 100644 --- a/tests/typescript.extractFileName.test.ts +++ b/tests/typescript.extractFileName.test.ts @@ -14,7 +14,8 @@ const functions: { [key: string]: Serverless.Function } = { package: { include: [], exclude: [] - } + }, + runtime: 'nodejs12.x' }, js: { handler: 'tests/assets/jsfile.create', @@ -23,28 +24,82 @@ const functions: { [key: string]: Serverless.Function } = { exclude: [] } }, + notActuallyTypescript: { + handler: 'tests/assets/jsfile.create', + package: { + include: [], + exclude: [] + }, + runtime: 'go1.x' + }, } describe('extractFileName', () => { - it('get function filenames from serverless service for a non-google provider', () => { - expect( - extractFileNames(process.cwd(), 'aws', functions), - ).toEqual( - [ - 'tests/assets/hello.ts', - 'tests/assets/world.ts', - 'tests/assets/jsfile.js', - ], - ) + describe('when the provider runtime is Node', () => { + it('can get function filenames from serverless service for a non-google provider', () => { + expect( + extractFileNames(process.cwd(), 'aws', 'nodejs10.x', functions), + ).toEqual( + [ + 'tests/assets/hello.ts', + 'tests/assets/world.ts', + 'tests/assets/jsfile.js', + ], + ) + }) + + it('can get function filename from serverless service for a google provider', () => { + expect( + extractFileNames(path.join(process.cwd(), 'example'), 'google', 'nodejs') + ).toEqual( + [ + 'handler.ts' + ] + ) + }) + }) + describe('when the provider runtime is not node', () => { + it('can get function filenames from serverless service for a non-google provider', () => { + expect( + extractFileNames(process.cwd(), 'aws', 'python2.7', functions), + ).toEqual( + [ + 'tests/assets/world.ts', + ], + ) + }) + + it('can get function filename from serverless service for a google provider', () => { + expect( + extractFileNames(path.join(process.cwd(), 'example'), 'google', 'python37') + ).toEqual( + [ + 'handler.ts' + ] + ) + }) }) + describe('when the provider runtime is undefined', () => { + it('can get function filenames from serverless service for a non-google provider', () => { + expect( + extractFileNames(process.cwd(), 'aws', undefined, functions), + ).toEqual( + [ + 'tests/assets/hello.ts', + 'tests/assets/world.ts', + 'tests/assets/jsfile.js', + ], + ) + }) - it('get function filename from serverless service for a google provider', () => { - expect( - extractFileNames(path.join(process.cwd(), 'example'), 'google') - ).toEqual( - [ - 'handler.ts' - ] - ) + it('can get function filename from serverless service for a google provider', () => { + expect( + extractFileNames(path.join(process.cwd(), 'example'), 'google', undefined) + ).toEqual( + [ + 'handler.ts' + ] + ) + }) }) })