From adc2df3f5ad4c8275e861d8b510f8d52b937f984 Mon Sep 17 00:00:00 2001 From: Spencer von der Ohe Date: Mon, 1 Aug 2022 21:32:31 -0600 Subject: [PATCH 1/4] Update dev deps so tsc works --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 5c48cccc..a8accc67 100644 --- a/package.json +++ b/package.json @@ -53,13 +53,13 @@ "@types/jest": "24.0.12", "@types/lodash": "4.14.123", "@types/node": "12.6.2", - "jest": "24.5.0", + "jest": "28.1.3", "mock-fs": "4.9.0", "rimraf": "2.6.3", "standard-version": "^9.3.2", - "ts-jest": "24.0.2", + "ts-jest": "28.0.7", "tslint": "5.14.0", - "typescript": "^3.9.10" + "typescript": "^4.7.4" }, "dependencies": { "fs-extra": "^7.0.1", From bafa3e2d86387dfc7d0901de4b33828a93cb18ab Mon Sep 17 00:00:00 2001 From: Spencer von der Ohe Date: Mon, 1 Aug 2022 21:32:53 -0600 Subject: [PATCH 2/4] Skip non-nodejs functions --- src/Serverless.d.ts | 2 + src/index.ts | 17 ++++-- tests/typescript.extractFileName.test.ts | 3 ++ tests/typescript.pluginSkipNonNode.test.ts | 61 ++++++++++++++++++++++ 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 tests/typescript.pluginSkipNonNode.test.ts 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..ef560e34 100644 --- a/src/index.ts +++ b/src/index.ts @@ -93,14 +93,25 @@ export class TypeScriptPlugin { get functions() { const { options } = this const { service } = this.serverless + const functions = service.functions || {} - if (options.function) { + const nodeFunctions = Object.keys(functions) + .filter(functionKey => { + const runtime = functions[functionKey].runtime || service.provider.runtime + return runtime.includes('nodejs') + }) + .reduce((obj, key) => { + obj[key] = functions[key] + return obj + }, {}) + + 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 From ed4af0fb08c2de3cf2c0e77b417f99fedc1e25da Mon Sep 17 00:00:00 2001 From: Spencer von der Ohe Date: Tue, 2 Aug 2022 11:57:44 -0600 Subject: [PATCH 3/4] Roll back jest dependencies (to support Node 10) --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a8accc67..a5039ce3 100644 --- a/package.json +++ b/package.json @@ -53,11 +53,11 @@ "@types/jest": "24.0.12", "@types/lodash": "4.14.123", "@types/node": "12.6.2", - "jest": "28.1.3", + "jest": "24.5.0", "mock-fs": "4.9.0", "rimraf": "2.6.3", "standard-version": "^9.3.2", - "ts-jest": "28.0.7", + "ts-jest": "24.0.2", "tslint": "5.14.0", "typescript": "^4.7.4" }, From 0fa668b99627f326e7f3062f0b4e54a726fb23dc Mon Sep 17 00:00:00 2001 From: Spencer von der Ohe Date: Tue, 2 Aug 2022 11:58:08 -0600 Subject: [PATCH 4/4] Simplify nodeFunctions logic --- src/index.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index ef560e34..b6100044 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,15 +95,13 @@ export class TypeScriptPlugin { const { service } = this.serverless const functions = service.functions || {} - const nodeFunctions = Object.keys(functions) - .filter(functionKey => { - const runtime = functions[functionKey].runtime || service.provider.runtime - return runtime.includes('nodejs') - }) - .reduce((obj, key) => { - obj[key] = functions[key] - return obj - }, {}) + 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 {