Skip to content

Commit 6cd5e45

Browse files
author
John Reeves
committed
Skip functions that do not use node runtime. Allows mixed runtime projects.
1 parent 6c15f35 commit 6cd5e45

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

Diff for: 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
@@ -30,6 +31,7 @@ declare namespace Serverless {
3031

3132
interface Function {
3233
handler: string
34+
runtime: string
3335
package: Serverless.Package
3436
}
3537

Diff for: src/typeScriptPlugin.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,24 @@ export class TypeScriptPlugin {
9292
return typescript.extractFileNames(
9393
this.originalServicePath,
9494
this.serverless.service.provider.name,
95-
this.functions
95+
this.nodeFunctions
9696
)
9797
}
9898

99+
get nodeFunctions() {
100+
const { service } = this.serverless
101+
const functions = this.functions
102+
103+
// filter out functions that have a non-node runtime because they can't even typescript
104+
return Object.keys(this.functions)
105+
.map(fn => ({fn, runtime: functions[fn].runtime || service.provider.runtime}))
106+
.filter(fnObj => fnObj.runtime.match(/nodejs/))
107+
.reduce((prev, cur) => ({
108+
...prev,
109+
[cur.fn]: service.functions[cur.fn]
110+
}), {} as {[key: string]: Serverless.Function})
111+
}
112+
99113
prepare() {
100114
// exclude serverless-plugin-typescript
101115
for (const fnName in this.functions) {
@@ -234,7 +248,7 @@ export class TypeScriptPlugin {
234248
}
235249

236250
if (service.package.individually) {
237-
const functionNames = service.getAllFunctions()
251+
const functionNames = Object.keys(this.nodeFunctions)
238252
functionNames.forEach(name => {
239253
service.functions[name].package.artifact = path.join(
240254
this.originalServicePath,

Diff for: tests/TypeScriptPlugin.test.ts

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {TypeScriptPlugin} from '../src/typeScriptPlugin'
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+
package: {
18+
individually: true,
19+
include: [],
20+
exclude: []
21+
},
22+
functions: {
23+
func1: {
24+
handler: 'java-fn',
25+
runtime: 'java8',
26+
package: {
27+
include: [],
28+
exclude: []
29+
}
30+
},
31+
func2: {
32+
handler: 'node-fn',
33+
runtime: 'nodejs99',
34+
package: {
35+
include: [],
36+
exclude: []
37+
}
38+
}
39+
},
40+
41+
getAllFunctions: jest.fn()
42+
},
43+
pluginManager: {
44+
spawn: jest.fn()
45+
}
46+
}
47+
48+
const plugin = new TypeScriptPlugin(slsInstance, {})
49+
50+
expect(
51+
Object.keys(plugin.nodeFunctions)
52+
).toEqual(
53+
[
54+
'func2'
55+
],
56+
)
57+
})
58+
})

Diff for: tests/typescript.extractFileName.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ 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: []
1011
}
1112
},
1213
world: {
1314
handler: 'tests/assets/world.handler',
15+
runtime: 'nodejs10.1',
1416
package: {
1517
include: [],
1618
exclude: []
1719
}
1820
},
1921
js: {
2022
handler: 'tests/assets/jsfile.create',
23+
runtime: 'nodejs10.1',
2124
package: {
2225
include: [],
2326
exclude: []

0 commit comments

Comments
 (0)