Skip to content

Commit

Permalink
fix: only process functions if runtime is node
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Sean Dawson committed Jan 28, 2020
1 parent 424fe49 commit aef5a0d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/Serverless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare namespace Serverless {
service: {
provider: {
name: string
runtime?: string
}
functions: {
[key: string]: Serverless.Function
Expand All @@ -31,6 +32,7 @@ declare namespace Serverless {
interface Function {
handler: string
package: Serverless.Package
runtime?: string
}

interface Package {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class TypeScriptPlugin {
return typescript.extractFileNames(
this.originalServicePath,
this.serverless.service.provider.name,
this.serverless.service.provider.runtime,
this.functions
)
}
Expand Down
8 changes: 7 additions & 1 deletion src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function makeDefaultTypescriptConfig() {
return defaultTypescriptConfig
}

export function extractFileNames(cwd: string, provider: string, functions?: { [key: string]: Serverless.Function }): string[] {
export function extractFileNames(cwd: string, provider: string, globalRuntime?: string, functions?: { [key: string]: Serverless.Function }): string[] {
// The Google provider will use the entrypoint not from the definition of the
// handler function, but instead from the package.json:main field, or via a
// index.js file. This check reads the current package.json in the same way
Expand Down Expand Up @@ -46,7 +46,13 @@ export function extractFileNames(cwd: string, provider: string, functions?: { [k
}
}

const runtimeIsNode = (runtime: string) => runtime.toLowerCase().startsWith('node')
const shouldProcessFunction = (fn: Serverless.Function) =>
(fn.runtime !== undefined && runtimeIsNode(fn.runtime)) ||
(fn.runtime === undefined && (globalRuntime === undefined || runtimeIsNode(globalRuntime)))

return _.values(functions)
.filter(shouldProcessFunction)
.map(fn => fn.handler)
.map(h => {
const fnName = _.last(h.split('.'))
Expand Down
93 changes: 74 additions & 19 deletions tests/typescript.extractFileName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const functions: { [key: string]: Serverless.Function } = {
package: {
include: [],
exclude: []
}
},
runtime: 'nodejs12.x'
},
js: {
handler: 'tests/assets/jsfile.create',
Expand All @@ -23,28 +24,82 @@ const functions: { [key: string]: Serverless.Function } = {
exclude: []
}
},
notActuallyTypescript: {
handler: 'tests/assets/jsfile.create',
package: {
include: [],
exclude: []
},
runtime: 'go1.x'
},
}

describe('extractFileName', () => {
it('get function filenames from serverless service for a non-google provider', () => {
expect(
extractFileNames(process.cwd(), 'aws', functions),
).toEqual(
[
'tests/assets/hello.ts',
'tests/assets/world.ts',
'tests/assets/jsfile.js',
],
)
describe('when the provider runtime is Node', () => {
it('can get function filenames from serverless service for a non-google provider', () => {
expect(
extractFileNames(process.cwd(), 'aws', 'nodejs10.x', functions),
).toEqual(
[
'tests/assets/hello.ts',
'tests/assets/world.ts',
'tests/assets/jsfile.js',
],
)
})

it('can get function filename from serverless service for a google provider', () => {
expect(
extractFileNames(path.join(process.cwd(), 'example'), 'google', 'nodejs')
).toEqual(
[
'handler.ts'
]
)
})
})
describe('when the provider runtime is not node', () => {
it('can get function filenames from serverless service for a non-google provider', () => {
expect(
extractFileNames(process.cwd(), 'aws', 'python2.7', functions),
).toEqual(
[
'tests/assets/world.ts',
],
)
})

it('can get function filename from serverless service for a google provider', () => {
expect(
extractFileNames(path.join(process.cwd(), 'example'), 'google', 'python37')
).toEqual(
[
'handler.ts'
]
)
})
})
describe('when the provider runtime is undefined', () => {
it('can get function filenames from serverless service for a non-google provider', () => {
expect(
extractFileNames(process.cwd(), 'aws', undefined, functions),
).toEqual(
[
'tests/assets/hello.ts',
'tests/assets/world.ts',
'tests/assets/jsfile.js',
],
)
})

it('get function filename from serverless service for a google provider', () => {
expect(
extractFileNames(path.join(process.cwd(), 'example'), 'google')
).toEqual(
[
'handler.ts'
]
)
it('can get function filename from serverless service for a google provider', () => {
expect(
extractFileNames(path.join(process.cwd(), 'example'), 'google', undefined)
).toEqual(
[
'handler.ts'
]
)
})
})
})

0 comments on commit aef5a0d

Please sign in to comment.