Skip to content

Commit d3fd7d6

Browse files
author
Sean Dawson
committed
Merge remote-tracking branch 'vectorjohn/master'
- Merge in the changes that vectorjohn made in a parallel PR (filtering functions at the plugin level) - Use lodash's pickBy to simplify some logic - Fix an issue with mixed export syntax (export vs module.exports) - Removed redundant tests Fixes #198
2 parents aef5a0d + 6cd5e45 commit d3fd7d6

6 files changed

+140
-137
lines changed

src/Serverless.d.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
declare namespace Serverless {
2+
23
interface Instance {
34
cli: {
45
log(str: string): void
@@ -8,18 +9,7 @@ declare namespace Serverless {
89
servicePath: string
910
}
1011

11-
service: {
12-
provider: {
13-
name: string
14-
runtime?: string
15-
}
16-
functions: {
17-
[key: string]: Serverless.Function
18-
}
19-
package: Serverless.Package
20-
getAllFunctions(): string[]
21-
}
22-
12+
service: Service
2313
pluginManager: PluginManager
2414
}
2515

@@ -42,6 +32,18 @@ declare namespace Serverless {
4232
individually?: boolean
4333
}
4434

35+
type FunctionMap = Record<string, Serverless.Function>
36+
37+
interface Service {
38+
provider: {
39+
name: string
40+
runtime?: string
41+
}
42+
functions: FunctionMap
43+
package: Serverless.Package
44+
getAllFunctions(): string[]
45+
}
46+
4547
interface PluginManager {
4648
spawn(command: string): Promise<void>
4749
}

src/index.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,22 @@ export class TypeScriptPlugin {
7979
const { options } = this
8080
const { service } = this.serverless
8181

82-
if (options.function) {
83-
return {
84-
[options.function]: service.functions[this.options.function]
85-
}
86-
}
87-
88-
return service.functions
82+
const allFunctions = options.function ? {
83+
[options.function]: service.functions[this.options.function]
84+
} : service.functions
85+
86+
// Ensure we only handle runtimes that support Typescript
87+
return _.pickBy(allFunctions, ({runtime}) => {
88+
const resolvedRuntime = runtime || service.provider.runtime
89+
// If runtime is not specified on the function or provider, default to previous behaviour
90+
return resolvedRuntime === undefined ? true : resolvedRuntime.match(/^node/)
91+
})
8992
}
9093

9194
get rootFileNames() {
9295
return typescript.extractFileNames(
9396
this.originalServicePath,
9497
this.serverless.service.provider.name,
95-
this.serverless.service.provider.runtime,
9698
this.functions
9799
)
98100
}
@@ -235,7 +237,7 @@ export class TypeScriptPlugin {
235237
}
236238

237239
if (service.package.individually) {
238-
const functionNames = service.getAllFunctions()
240+
const functionNames = Object.keys(this.functions)
239241
functionNames.forEach(name => {
240242
service.functions[name].package.artifact = path.join(
241243
this.originalServicePath,
@@ -276,4 +278,4 @@ export class TypeScriptPlugin {
276278
}
277279
}
278280

279-
module.exports = TypeScriptPlugin
281+
export default TypeScriptPlugin

src/typescript.ts

+1-7
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, globalRuntime?: string, functions?: { [key: string]: Serverless.Function }): string[] {
21+
export function extractFileNames(cwd: string, provider: 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,13 +46,7 @@ export function extractFileNames(cwd: string, provider: string, globalRuntime?:
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-
5449
return _.values(functions)
55-
.filter(shouldProcessFunction)
5650
.map(fn => fn.handler)
5751
.map(h => {
5852
const fnName = _.last(h.split('.'))

tests/index.functions.test.ts

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import TypeScriptPlugin from '../src'
2+
3+
const createInstance = (functions: Serverless.FunctionMap, globalRuntime?: string): Serverless.Instance => ({
4+
cli: {
5+
log: jest.fn()
6+
},
7+
config: {
8+
servicePath: 'servicePath'
9+
},
10+
service: {
11+
provider: {
12+
name: 'aws',
13+
runtime: globalRuntime
14+
},
15+
package: {
16+
individually: true,
17+
include: [],
18+
exclude: []
19+
},
20+
functions,
21+
getAllFunctions: jest.fn()
22+
},
23+
pluginManager: {
24+
spawn: jest.fn()
25+
}
26+
})
27+
28+
describe('functions', () => {
29+
const functions: Serverless.FunctionMap = {
30+
hello: {
31+
handler: 'tests/assets/hello.handler',
32+
package: {
33+
include: [],
34+
exclude: []
35+
}
36+
},
37+
world: {
38+
handler: 'tests/assets/world.handler',
39+
runtime: 'nodejs12.x',
40+
package: {
41+
include: [],
42+
exclude: []
43+
},
44+
},
45+
js: {
46+
handler: 'tests/assets/jsfile.create',
47+
package: {
48+
include: [],
49+
exclude: []
50+
}
51+
},
52+
notActuallyTypescript: {
53+
handler: 'tests/assets/jsfile.create',
54+
package: {
55+
include: [],
56+
exclude: []
57+
},
58+
runtime: 'go1.x'
59+
},
60+
}
61+
62+
describe('when the provider runtime is Node', () => {
63+
it('can get filter out non node based functions', () => {
64+
const slsInstance = createInstance(functions, 'nodejs10.x')
65+
const plugin = new TypeScriptPlugin(slsInstance, {})
66+
67+
expect(
68+
Object.values(plugin.functions).map(fn => fn.handler),
69+
).toEqual(
70+
[
71+
'tests/assets/hello.handler',
72+
'tests/assets/world.handler',
73+
'tests/assets/jsfile.create',
74+
],
75+
)
76+
})
77+
})
78+
79+
describe('when the provider runtime is not Node', () => {
80+
it('can get filter out non node based functions', () => {
81+
const slsInstance = createInstance(functions, 'python2.7')
82+
const plugin = new TypeScriptPlugin(slsInstance, {})
83+
84+
expect(
85+
Object.values(plugin.functions).map(fn => fn.handler),
86+
).toEqual(
87+
[
88+
'tests/assets/world.handler',
89+
],
90+
)
91+
})
92+
})
93+
94+
describe('when the provider runtime is undefined', () => {
95+
it('can get filter out non node based functions', () => {
96+
const slsInstance = createInstance(functions)
97+
const plugin = new TypeScriptPlugin(slsInstance, {})
98+
99+
expect(
100+
Object.values(plugin.functions).map(fn => fn.handler),
101+
).toEqual(
102+
[
103+
'tests/assets/hello.handler',
104+
'tests/assets/world.handler',
105+
'tests/assets/jsfile.create',
106+
],
107+
)
108+
})
109+
})
110+
})

tests/typescript.extractFileName.test.ts

-105
This file was deleted.

yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -3456,9 +3456,9 @@ type-check@~0.3.2:
34563456
prelude-ls "~1.1.2"
34573457

34583458
typescript@^3.4.1:
3459-
version "3.4.1"
3460-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.1.tgz#b6691be11a881ffa9a05765a205cb7383f3b63c6"
3461-
integrity sha512-3NSMb2VzDQm8oBTLH6Nj55VVtUEpe/rgkIzMir0qVoLyjDZlnMBva0U6vDiV3IH+sl/Yu6oP5QwsAQtHPmDd2Q==
3459+
version "3.7.5"
3460+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.5.tgz#0692e21f65fd4108b9330238aac11dd2e177a1ae"
3461+
integrity sha512-/P5lkRXkWHNAbcJIiHPfRoKqyd7bsyCma1hZNUGfn20qm64T6ZBlrzprymeu918H+mB/0rIg2gGK/BXkhhYgBw==
34623462

34633463
uglify-js@^3.1.4:
34643464
version "3.5.2"

0 commit comments

Comments
 (0)