Skip to content

Commit

Permalink
Skip functions that do not use node runtime. Allows mixed runtime pro…
Browse files Browse the repository at this point in the history
…jects.
  • Loading branch information
John Reeves committed Feb 3, 2020
1 parent 6c15f35 commit 6cd5e45
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 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 @@ -30,6 +31,7 @@ declare namespace Serverless {

interface Function {
handler: string
runtime: string
package: Serverless.Package
}

Expand Down
18 changes: 16 additions & 2 deletions src/typeScriptPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,24 @@ export class TypeScriptPlugin {
return typescript.extractFileNames(
this.originalServicePath,
this.serverless.service.provider.name,
this.functions
this.nodeFunctions
)
}

get nodeFunctions() {
const { service } = this.serverless
const functions = this.functions

// filter out functions that have a non-node runtime because they can't even typescript
return Object.keys(this.functions)
.map(fn => ({fn, runtime: functions[fn].runtime || service.provider.runtime}))
.filter(fnObj => fnObj.runtime.match(/nodejs/))
.reduce((prev, cur) => ({
...prev,
[cur.fn]: service.functions[cur.fn]
}), {} as {[key: string]: Serverless.Function})
}

prepare() {
// exclude serverless-plugin-typescript
for (const fnName in this.functions) {
Expand Down Expand Up @@ -234,7 +248,7 @@ export class TypeScriptPlugin {
}

if (service.package.individually) {
const functionNames = service.getAllFunctions()
const functionNames = Object.keys(this.nodeFunctions)
functionNames.forEach(name => {
service.functions[name].package.artifact = path.join(
this.originalServicePath,
Expand Down
58 changes: 58 additions & 0 deletions tests/TypeScriptPlugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {TypeScriptPlugin} from '../src/typeScriptPlugin'

describe('TypeScriptPlugin', () => {
it('rootFileNames includes only node runtimes', () => {
const slsInstance: Serverless.Instance = {
cli: {
log: jest.fn()
},
config: {
servicePath: 'servicePath'
},
service: {
provider: {
name: 'aws',
runtime: 'nodejs99'
},
package: {
individually: true,
include: [],
exclude: []
},
functions: {
func1: {
handler: 'java-fn',
runtime: 'java8',
package: {
include: [],
exclude: []
}
},
func2: {
handler: 'node-fn',
runtime: 'nodejs99',
package: {
include: [],
exclude: []
}
}
},

getAllFunctions: jest.fn()
},
pluginManager: {
spawn: jest.fn()
}
}

const plugin = new TypeScriptPlugin(slsInstance, {})

expect(
Object.keys(plugin.nodeFunctions)
).toEqual(
[
'func2'
],
)
})
})
3 changes: 3 additions & 0 deletions tests/typescript.extractFileName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import * as path from 'path'
const functions: { [key: string]: Serverless.Function } = {
hello: {
handler: 'tests/assets/hello.handler',
runtime: 'nodejs10.1',
package: {
include: [],
exclude: []
}
},
world: {
handler: 'tests/assets/world.handler',
runtime: 'nodejs10.1',
package: {
include: [],
exclude: []
}
},
js: {
handler: 'tests/assets/jsfile.create',
runtime: 'nodejs10.1',
package: {
include: [],
exclude: []
Expand Down

0 comments on commit 6cd5e45

Please sign in to comment.