Skip to content

Add ability to handle import of .ts or .js files #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function makeDefaultTypescriptConfig() {
preserveConstEnums: true,
strictNullChecks: true,
sourceMap: true,
allowJs: true,
target: ts.ScriptTarget.ES5,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
lib: ['lib.es2015.d.ts'],
Expand Down Expand Up @@ -53,7 +54,21 @@ export function extractFileNames(cwd: string, provider: string, functions?: { [k
const fnName = _.last(h.split('.'))
const fnNameLastAppearanceIndex = h.lastIndexOf(fnName)
// replace only last instance to allow the same name for file and handler
return h.substring(0, fnNameLastAppearanceIndex) + 'ts'
const fileName = h.substring(0, fnNameLastAppearanceIndex)

// Check if the .ts files exists. If so return that to watch
if (fs.existsSync(path.join(cwd, fileName + 'ts'))) {
return fileName + 'ts'
}

// Check if the .js files exists. If so return that to watch
if (fs.existsSync(path.join(cwd, fileName + 'js'))) {
return fileName + 'js'
}

// Can't find the files. Watch will have an exception anyway. So throw one with error.
console.log(`Cannot locate handler - ${fileName} not found`)
throw new Error('Typescript compilation failed. Please ensure handlers exists with ext .ts or .js')
})
}

Expand Down
Empty file added tests/assets/hello.ts
Empty file.
Empty file added tests/assets/jsfile.js
Empty file.
Empty file added tests/assets/world.ts
Empty file.
14 changes: 7 additions & 7 deletions tests/typescript.extractFileName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import * as path from 'path'

const functions: { [key: string]: ServerlessFunction } = {
hello: {
handler: 'my-folder/hello.handler',
handler: 'tests/assets/hello.handler',
package: {
include: [],
exclude: []
}
},
world: {
handler: 'my-folder/my-subfolder/world.handler',
handler: 'tests/assets/world.handler',
package: {
include: [],
exclude: []
}
},
create: {
handler: 'create.create',
js: {
handler: 'tests/assets/jsfile.create',
package: {
include: [],
exclude: []
Expand All @@ -32,9 +32,9 @@ describe('extractFileName', () => {
extractFileNames(process.cwd(), 'aws', functions),
).toEqual(
[
'my-folder/hello.ts',
'my-folder/my-subfolder/world.ts',
'create.ts',
'tests/assets/hello.ts',
'tests/assets/world.ts',
'tests/assets/jsfile.js',
],
)
})
Expand Down