diff --git a/package.json b/package.json index 5c48cccc..a5039ce3 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "standard-version": "^9.3.2", "ts-jest": "24.0.2", "tslint": "5.14.0", - "typescript": "^3.9.10" + "typescript": "^4.7.4" }, "dependencies": { "fs-extra": "^7.0.1", diff --git a/src/Serverless.d.ts b/src/Serverless.d.ts index 3b05529e..dade3099 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 @@ -38,6 +39,7 @@ declare namespace Serverless { interface Function { handler: string package: Serverless.Package + runtime?: string } interface Layer { diff --git a/src/index.ts b/src/index.ts index d4ebc0b8..b6100044 100644 --- a/src/index.ts +++ b/src/index.ts @@ -93,14 +93,23 @@ export class TypeScriptPlugin { get functions() { const { options } = this const { service } = this.serverless + const functions = service.functions || {} - if (options.function) { + const nodeFunctions = {} + for (const [name, functionObject] of Object.entries(functions)) { + const runtime = functions[name].runtime || service.provider.runtime + if (runtime.includes('nodejs')) { + nodeFunctions[name] = functionObject + } + } + + if (options.function && nodeFunctions[options.function]) { return { - [options.function]: service.functions[this.options.function] + [options.function]: nodeFunctions[options.function] } } - return service.functions + return nodeFunctions } get rootFileNames() { diff --git a/tests/typescript.extractFileName.test.ts b/tests/typescript.extractFileName.test.ts index 6a235504..5fb25fe9 100644 --- a/tests/typescript.extractFileName.test.ts +++ b/tests/typescript.extractFileName.test.ts @@ -4,6 +4,7 @@ import * as path from 'path' const functions: { [key: string]: Serverless.Function } = { hello: { handler: 'tests/assets/hello.handler', + runtime: 'nodejs10.1', package: { include: [], exclude: [], @@ -12,6 +13,7 @@ const functions: { [key: string]: Serverless.Function } = { }, world: { handler: 'tests/assets/world.handler', + runtime: 'nodejs10.1', package: { include: [], exclude: [], @@ -20,6 +22,7 @@ const functions: { [key: string]: Serverless.Function } = { }, js: { handler: 'tests/assets/jsfile.create', + runtime: 'nodejs10.1', package: { include: [], exclude: [], diff --git a/tests/typescript.pluginSkipNonNode.test.ts b/tests/typescript.pluginSkipNonNode.test.ts new file mode 100644 index 00000000..12cb361a --- /dev/null +++ b/tests/typescript.pluginSkipNonNode.test.ts @@ -0,0 +1,61 @@ +import * as TypeScriptPlugin from '../src/index' + +describe('TypeScriptPlugin', () => { + it('rootFileNames includes only node runtimes', () => { + const slsInstance: Serverless.Instance = { + cli: { + log: jest.fn() + }, + config: { + servicePath: 'servicePath' + }, + service: { + provider: { + name: 'aws', + runtime: 'nodejs99' + }, + functions: { + func1: { + handler: 'java-fn', + runtime: 'python3.9', + package:{ + exclude: [], + include: [], + patterns: [] + } + }, + func2: { + handler: 'node-fn', + runtime: 'nodejs16', + package:{ + exclude: [], + include: [], + patterns: [] + } + } + }, + package: { + exclude: [], + include: [], + patterns: [] + }, + layers: {}, + getAllLayers: jest.fn(), + getAllFunctions: jest.fn() + }, + pluginManager: { + spawn: jest.fn() + } + } + + const plugin = new (TypeScriptPlugin as any)(slsInstance, {}) + + expect( + Object.keys(plugin.functions) + ).toEqual( + [ + 'func2' + ], + ) + }) +}) \ No newline at end of file