Skip to content

Commit aef5a0d

Browse files
author
Sean Dawson
committed
fix: 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
1 parent 424fe49 commit aef5a0d

File tree

4 files changed

+84
-20
lines changed

4 files changed

+84
-20
lines changed

src/Serverless.d.ts

+2
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
@@ -31,6 +32,7 @@ declare namespace Serverless {
3132
interface Function {
3233
handler: string
3334
package: Serverless.Package
35+
runtime?: string
3436
}
3537

3638
interface Package {

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export class TypeScriptPlugin {
9292
return typescript.extractFileNames(
9393
this.originalServicePath,
9494
this.serverless.service.provider.name,
95+
this.serverless.service.provider.runtime,
9596
this.functions
9697
)
9798
}

src/typescript.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function makeDefaultTypescriptConfig() {
1818
return defaultTypescriptConfig
1919
}
2020

21-
export function extractFileNames(cwd: string, provider: string, functions?: { [key: string]: Serverless.Function }): string[] {
21+
export function extractFileNames(cwd: string, provider: string, globalRuntime?: string, functions?: { [key: string]: Serverless.Function }): string[] {
2222
// The Google provider will use the entrypoint not from the definition of the
2323
// handler function, but instead from the package.json:main field, or via a
2424
// 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
4646
}
4747
}
4848

49+
const runtimeIsNode = (runtime: string) => runtime.toLowerCase().startsWith('node')
50+
const shouldProcessFunction = (fn: Serverless.Function) =>
51+
(fn.runtime !== undefined && runtimeIsNode(fn.runtime)) ||
52+
(fn.runtime === undefined && (globalRuntime === undefined || runtimeIsNode(globalRuntime)))
53+
4954
return _.values(functions)
55+
.filter(shouldProcessFunction)
5056
.map(fn => fn.handler)
5157
.map(h => {
5258
const fnName = _.last(h.split('.'))

tests/typescript.extractFileName.test.ts

+74-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const functions: { [key: string]: Serverless.Function } = {
1414
package: {
1515
include: [],
1616
exclude: []
17-
}
17+
},
18+
runtime: 'nodejs12.x'
1819
},
1920
js: {
2021
handler: 'tests/assets/jsfile.create',
@@ -23,28 +24,82 @@ const functions: { [key: string]: Serverless.Function } = {
2324
exclude: []
2425
}
2526
},
27+
notActuallyTypescript: {
28+
handler: 'tests/assets/jsfile.create',
29+
package: {
30+
include: [],
31+
exclude: []
32+
},
33+
runtime: 'go1.x'
34+
},
2635
}
2736

2837
describe('extractFileName', () => {
29-
it('get function filenames from serverless service for a non-google provider', () => {
30-
expect(
31-
extractFileNames(process.cwd(), 'aws', functions),
32-
).toEqual(
33-
[
34-
'tests/assets/hello.ts',
35-
'tests/assets/world.ts',
36-
'tests/assets/jsfile.js',
37-
],
38-
)
38+
describe('when the provider runtime is Node', () => {
39+
it('can get function filenames from serverless service for a non-google provider', () => {
40+
expect(
41+
extractFileNames(process.cwd(), 'aws', 'nodejs10.x', functions),
42+
).toEqual(
43+
[
44+
'tests/assets/hello.ts',
45+
'tests/assets/world.ts',
46+
'tests/assets/jsfile.js',
47+
],
48+
)
49+
})
50+
51+
it('can get function filename from serverless service for a google provider', () => {
52+
expect(
53+
extractFileNames(path.join(process.cwd(), 'example'), 'google', 'nodejs')
54+
).toEqual(
55+
[
56+
'handler.ts'
57+
]
58+
)
59+
})
60+
})
61+
describe('when the provider runtime is not node', () => {
62+
it('can get function filenames from serverless service for a non-google provider', () => {
63+
expect(
64+
extractFileNames(process.cwd(), 'aws', 'python2.7', functions),
65+
).toEqual(
66+
[
67+
'tests/assets/world.ts',
68+
],
69+
)
70+
})
71+
72+
it('can get function filename from serverless service for a google provider', () => {
73+
expect(
74+
extractFileNames(path.join(process.cwd(), 'example'), 'google', 'python37')
75+
).toEqual(
76+
[
77+
'handler.ts'
78+
]
79+
)
80+
})
3981
})
82+
describe('when the provider runtime is undefined', () => {
83+
it('can get function filenames from serverless service for a non-google provider', () => {
84+
expect(
85+
extractFileNames(process.cwd(), 'aws', undefined, functions),
86+
).toEqual(
87+
[
88+
'tests/assets/hello.ts',
89+
'tests/assets/world.ts',
90+
'tests/assets/jsfile.js',
91+
],
92+
)
93+
})
4094

41-
it('get function filename from serverless service for a google provider', () => {
42-
expect(
43-
extractFileNames(path.join(process.cwd(), 'example'), 'google')
44-
).toEqual(
45-
[
46-
'handler.ts'
47-
]
48-
)
95+
it('can get function filename from serverless service for a google provider', () => {
96+
expect(
97+
extractFileNames(path.join(process.cwd(), 'example'), 'google', undefined)
98+
).toEqual(
99+
[
100+
'handler.ts'
101+
]
102+
)
103+
})
49104
})
50105
})

0 commit comments

Comments
 (0)