Skip to content

Commit

Permalink
fix: Do not attempt to process non nodejs functions (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzy4o authored Aug 2, 2022
1 parent 4469a04 commit ba2754b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"standard-version": "^9.3.2",
"ts-jest": "24.0.2",
"tslint": "5.14.0",
"typescript": "^3.9.10"
"typescript": "^4.7.4"
},
"dependencies": {
"fs-extra": "^7.0.1",
Expand Down
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 Down Expand Up @@ -38,6 +39,7 @@ declare namespace Serverless {
interface Function {
handler: string
package: Serverless.Package
runtime?: string
}

interface Layer {
Expand Down
15 changes: 12 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,23 @@ export class TypeScriptPlugin {
get functions() {
const { options } = this
const { service } = this.serverless
const functions = service.functions || {}

if (options.function) {
const nodeFunctions = {}
for (const [name, functionObject] of Object.entries(functions)) {
const runtime = functions[name].runtime || service.provider.runtime
if (runtime.includes('nodejs')) {
nodeFunctions[name] = functionObject
}
}

if (options.function && nodeFunctions[options.function]) {
return {
[options.function]: service.functions[this.options.function]
[options.function]: nodeFunctions[options.function]
}
}

return service.functions
return nodeFunctions
}

get rootFileNames() {
Expand Down
3 changes: 3 additions & 0 deletions tests/typescript.extractFileName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path'
const functions: { [key: string]: Serverless.Function } = {
hello: {
handler: 'tests/assets/hello.handler',
runtime: 'nodejs10.1',
package: {
include: [],
exclude: [],
Expand All @@ -12,6 +13,7 @@ const functions: { [key: string]: Serverless.Function } = {
},
world: {
handler: 'tests/assets/world.handler',
runtime: 'nodejs10.1',
package: {
include: [],
exclude: [],
Expand All @@ -20,6 +22,7 @@ const functions: { [key: string]: Serverless.Function } = {
},
js: {
handler: 'tests/assets/jsfile.create',
runtime: 'nodejs10.1',
package: {
include: [],
exclude: [],
Expand Down
61 changes: 61 additions & 0 deletions tests/typescript.pluginSkipNonNode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import * as TypeScriptPlugin from '../src/index'

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'
},
functions: {
func1: {
handler: 'java-fn',
runtime: 'python3.9',
package:{
exclude: [],
include: [],
patterns: []
}
},
func2: {
handler: 'node-fn',
runtime: 'nodejs16',
package:{
exclude: [],
include: [],
patterns: []
}
}
},
package: {
exclude: [],
include: [],
patterns: []
},
layers: {},
getAllLayers: jest.fn(),
getAllFunctions: jest.fn()
},
pluginManager: {
spawn: jest.fn()
}
}

const plugin = new (TypeScriptPlugin as any)(slsInstance, {})

expect(
Object.keys(plugin.functions)
).toEqual(
[
'func2'
],
)
})
})

0 comments on commit ba2754b

Please sign in to comment.