Skip to content

Commit ba2754b

Browse files
authored
fix: Do not attempt to process non nodejs functions (#277)
1 parent 4469a04 commit ba2754b

5 files changed

+79
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"standard-version": "^9.3.2",
6060
"ts-jest": "24.0.2",
6161
"tslint": "5.14.0",
62-
"typescript": "^3.9.10"
62+
"typescript": "^4.7.4"
6363
},
6464
"dependencies": {
6565
"fs-extra": "^7.0.1",

src/Serverless.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare namespace Serverless {
1111
service: {
1212
provider: {
1313
name: string
14+
runtime?: string
1415
}
1516
functions: {
1617
[key: string]: Serverless.Function
@@ -38,6 +39,7 @@ declare namespace Serverless {
3839
interface Function {
3940
handler: string
4041
package: Serverless.Package
42+
runtime?: string
4143
}
4244

4345
interface Layer {

src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,23 @@ export class TypeScriptPlugin {
9393
get functions() {
9494
const { options } = this
9595
const { service } = this.serverless
96+
const functions = service.functions || {}
9697

97-
if (options.function) {
98+
const nodeFunctions = {}
99+
for (const [name, functionObject] of Object.entries(functions)) {
100+
const runtime = functions[name].runtime || service.provider.runtime
101+
if (runtime.includes('nodejs')) {
102+
nodeFunctions[name] = functionObject
103+
}
104+
}
105+
106+
if (options.function && nodeFunctions[options.function]) {
98107
return {
99-
[options.function]: service.functions[this.options.function]
108+
[options.function]: nodeFunctions[options.function]
100109
}
101110
}
102111

103-
return service.functions
112+
return nodeFunctions
104113
}
105114

106115
get rootFileNames() {

tests/typescript.extractFileName.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from 'path'
44
const functions: { [key: string]: Serverless.Function } = {
55
hello: {
66
handler: 'tests/assets/hello.handler',
7+
runtime: 'nodejs10.1',
78
package: {
89
include: [],
910
exclude: [],
@@ -12,6 +13,7 @@ const functions: { [key: string]: Serverless.Function } = {
1213
},
1314
world: {
1415
handler: 'tests/assets/world.handler',
16+
runtime: 'nodejs10.1',
1517
package: {
1618
include: [],
1719
exclude: [],
@@ -20,6 +22,7 @@ const functions: { [key: string]: Serverless.Function } = {
2022
},
2123
js: {
2224
handler: 'tests/assets/jsfile.create',
25+
runtime: 'nodejs10.1',
2326
package: {
2427
include: [],
2528
exclude: [],
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import * as TypeScriptPlugin from '../src/index'
2+
3+
describe('TypeScriptPlugin', () => {
4+
it('rootFileNames includes only node runtimes', () => {
5+
const slsInstance: Serverless.Instance = {
6+
cli: {
7+
log: jest.fn()
8+
},
9+
config: {
10+
servicePath: 'servicePath'
11+
},
12+
service: {
13+
provider: {
14+
name: 'aws',
15+
runtime: 'nodejs99'
16+
},
17+
functions: {
18+
func1: {
19+
handler: 'java-fn',
20+
runtime: 'python3.9',
21+
package:{
22+
exclude: [],
23+
include: [],
24+
patterns: []
25+
}
26+
},
27+
func2: {
28+
handler: 'node-fn',
29+
runtime: 'nodejs16',
30+
package:{
31+
exclude: [],
32+
include: [],
33+
patterns: []
34+
}
35+
}
36+
},
37+
package: {
38+
exclude: [],
39+
include: [],
40+
patterns: []
41+
},
42+
layers: {},
43+
getAllLayers: jest.fn(),
44+
getAllFunctions: jest.fn()
45+
},
46+
pluginManager: {
47+
spawn: jest.fn()
48+
}
49+
}
50+
51+
const plugin = new (TypeScriptPlugin as any)(slsInstance, {})
52+
53+
expect(
54+
Object.keys(plugin.functions)
55+
).toEqual(
56+
[
57+
'func2'
58+
],
59+
)
60+
})
61+
})

0 commit comments

Comments
 (0)