Skip to content
This repository was archived by the owner on Dec 28, 2021. It is now read-only.

Commit f4314bb

Browse files
authored
Merge pull request #1 from KingDarBoja/agiledigital/merge-master
fix: only process function if runtime is NodeJS
2 parents dfb5625 + 8916044 commit f4314bb

File tree

6 files changed

+137
-17
lines changed

6 files changed

+137
-17
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
],
7979
"guard-for-in": "off",
8080
"id-blacklist": [
81-
"error",
81+
"off",
8282
"any",
8383
"Number",
8484
"number",

src/index.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from 'path'
22
import * as fs from 'fs-extra'
33
import * as _ from 'lodash'
4-
import * as globby from 'globby'
4+
import globby from 'globby'
55

66
import { ServerlessTSInstance, ServerlessTSOptions, ServerlessTSFunction } from './serverlessTypes'
77
import { extractFileNames, getTypescriptConfig, run } from './typescript'
@@ -10,7 +10,7 @@ import { watchFiles } from './watchFiles'
1010
const SERVERLESS_FOLDER = '.serverless'
1111
const BUILD_FOLDER = '.build'
1212

13-
export class TypeScriptPlugin {
13+
class TypeScriptPlugin {
1414
private originalServicePath: string
1515
private isWatching: boolean
1616

@@ -80,13 +80,17 @@ export class TypeScriptPlugin {
8080
const { options } = this
8181
const { service } = this.serverless
8282

83-
if (options.function) {
84-
return {
85-
[options.function]: service.functions[this.options.function]
86-
}
87-
}
83+
const allFunctions = options.function ? {
84+
[options.function]: service.functions[this.options.function]
85+
} : service.functions
8886

89-
return service.functions
87+
// Ensure we only handle runtimes that support Typescript
88+
return _.pickBy(allFunctions, ({runtime}) => {
89+
const resolvedRuntime = runtime || service.provider.runtime
90+
// If runtime is not specified on the function or provider, default to previous behaviour
91+
const regexRuntime = /^node/
92+
return resolvedRuntime === undefined ? true : regexRuntime.exec(resolvedRuntime)
93+
})
9094
}
9195

9296
get rootFileNames(): string[] {
@@ -238,7 +242,7 @@ export class TypeScriptPlugin {
238242
}
239243

240244
if (service.package.individually) {
241-
const functionNames = service.getAllFunctions()
245+
const functionNames = Object.keys(this.functions)
242246
functionNames.forEach(name => {
243247
service.functions[name].package.artifact = path.join(
244248
this.originalServicePath,
@@ -278,3 +282,5 @@ export class TypeScriptPlugin {
278282
})
279283
}
280284
}
285+
286+
export = TypeScriptPlugin

src/serverlessTypes.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export type ServerlessTSFunctionMap = Record<string, ServerlessTSFunction>
2+
13
export interface ServerlessTSInstance {
24
cli: {
35
log(str: string): void
@@ -12,15 +14,14 @@ export interface ServerlessTSInstance {
1214
export interface ServerlessTSService {
1315
provider: {
1416
name: string
17+
runtime?: string
1518
}
1619
custom: {
1720
typeScript: {
1821
tsconfigFilePath: string | undefined
1922
}
2023
}
21-
functions: {
22-
[key: string]: ServerlessTSFunction
23-
}
24+
functions: ServerlessTSFunctionMap
2425
package: ServerlessTSPackage
2526
getAllFunctions(): string[]
2627
}
@@ -34,6 +35,7 @@ export interface ServerlessTSOptions {
3435
export interface ServerlessTSFunction {
3536
handler: string
3637
package: ServerlessTSPackage
38+
runtime?: string
3739
}
3840

3941
export interface ServerlessTSPackage {

tests/index.functions.test.ts

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

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"target": "es6",
55
"declaration": true,
66
"sourceMap": true,
7-
"outDir": "dist"
7+
"outDir": "dist",
8+
"esModuleInterop": true
89
},
910
"include": ["src/**/*.ts"],
1011
"exclude": [

yarn.lock

Lines changed: 3 additions & 3 deletions
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)