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

Commit

Permalink
Merge pull request #1 from KingDarBoja/agiledigital/merge-master
Browse files Browse the repository at this point in the history
fix: only process function if runtime is NodeJS
  • Loading branch information
KingDarBoja authored Feb 23, 2020
2 parents dfb5625 + 8916044 commit f4314bb
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
],
"guard-for-in": "off",
"id-blacklist": [
"error",
"off",
"any",
"Number",
"number",
Expand Down
24 changes: 15 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path'
import * as fs from 'fs-extra'
import * as _ from 'lodash'
import * as globby from 'globby'
import globby from 'globby'

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

export class TypeScriptPlugin {
class TypeScriptPlugin {
private originalServicePath: string
private isWatching: boolean

Expand Down Expand Up @@ -80,13 +80,17 @@ export class TypeScriptPlugin {
const { options } = this
const { service } = this.serverless

if (options.function) {
return {
[options.function]: service.functions[this.options.function]
}
}
const allFunctions = options.function ? {
[options.function]: service.functions[this.options.function]
} : service.functions

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

get rootFileNames(): string[] {
Expand Down Expand Up @@ -238,7 +242,7 @@ export class TypeScriptPlugin {
}

if (service.package.individually) {
const functionNames = service.getAllFunctions()
const functionNames = Object.keys(this.functions)
functionNames.forEach(name => {
service.functions[name].package.artifact = path.join(
this.originalServicePath,
Expand Down Expand Up @@ -278,3 +282,5 @@ export class TypeScriptPlugin {
})
}
}

export = TypeScriptPlugin
8 changes: 5 additions & 3 deletions src/serverlessTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type ServerlessTSFunctionMap = Record<string, ServerlessTSFunction>

export interface ServerlessTSInstance {
cli: {
log(str: string): void
Expand All @@ -12,15 +14,14 @@ export interface ServerlessTSInstance {
export interface ServerlessTSService {
provider: {
name: string
runtime?: string
}
custom: {
typeScript: {
tsconfigFilePath: string | undefined
}
}
functions: {
[key: string]: ServerlessTSFunction
}
functions: ServerlessTSFunctionMap
package: ServerlessTSPackage
getAllFunctions(): string[]
}
Expand All @@ -34,6 +35,7 @@ export interface ServerlessTSOptions {
export interface ServerlessTSFunction {
handler: string
package: ServerlessTSPackage
runtime?: string
}

export interface ServerlessTSPackage {
Expand Down
111 changes: 111 additions & 0 deletions tests/index.functions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import TypeScriptPlugin from '../src'
import { ServerlessTSInstance, ServerlessTSFunctionMap } from '../src/serverlessTypes'

const createInstance = (functions: ServerlessTSFunctionMap, globalRuntime?: string): ServerlessTSInstance => ({
cli: {
log: jest.fn()
},
config: {
servicePath: 'servicePath'
},
service: {
provider: {
name: 'aws',
runtime: globalRuntime
},
package: {
individually: true,
include: [],
exclude: []
},
functions,
getAllFunctions: jest.fn()
},
pluginManager: {
spawn: jest.fn()
}
})

describe('functions', () => {
const functions: ServerlessTSFunctionMap = {
hello: {
handler: 'tests/assets/hello.handler',
package: {
include: [],
exclude: []
}
},
world: {
handler: 'tests/assets/world.handler',
runtime: 'nodejs12.x',
package: {
include: [],
exclude: []
},
},
js: {
handler: 'tests/assets/jsfile.create',
package: {
include: [],
exclude: []
}
},
notActuallyTypescript: {
handler: 'tests/assets/jsfile.create',
package: {
include: [],
exclude: []
},
runtime: 'go1.x'
},
}

describe('when the provider runtime is Node', () => {
it('can get filter out non node based functions', () => {
const slsInstance = createInstance(functions, 'nodejs10.x')
const plugin = new TypeScriptPlugin(slsInstance, {})

expect(
Object.values(plugin.functions).map(fn => fn.handler),
).toEqual(
[
'tests/assets/hello.handler',
'tests/assets/world.handler',
'tests/assets/jsfile.create',
],
)
})
})

describe('when the provider runtime is not Node', () => {
it('can get filter out non node based functions', () => {
const slsInstance = createInstance(functions, 'python2.7')
const plugin = new TypeScriptPlugin(slsInstance, {})

expect(
Object.values(plugin.functions).map(fn => fn.handler),
).toEqual(
[
'tests/assets/world.handler',
],
)
})
})

describe('when the provider runtime is undefined', () => {
it('can get filter out non node based functions', () => {
const slsInstance = createInstance(functions)
const plugin = new TypeScriptPlugin(slsInstance, {})

expect(
Object.values(plugin.functions).map(fn => fn.handler),
).toEqual(
[
'tests/assets/hello.handler',
'tests/assets/world.handler',
'tests/assets/jsfile.create',
],
)
})
})
})
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"target": "es6",
"declaration": true,
"sourceMap": true,
"outDir": "dist"
"outDir": "dist",
"esModuleInterop": true
},
"include": ["src/**/*.ts"],
"exclude": [
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3456,9 +3456,9 @@ type-check@~0.3.2:
prelude-ls "~1.1.2"

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

uglify-js@^3.1.4:
version "3.5.2"
Expand Down

0 comments on commit f4314bb

Please sign in to comment.