Skip to content

Commit

Permalink
Merge pull request #79 from ro-savage/add-js-support
Browse files Browse the repository at this point in the history
Add ability to handle import of .ts or .js files, closes #78
  • Loading branch information
JackCuthbert authored Jul 12, 2019
2 parents e137a99 + f1a3960 commit 02b1efa
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
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

0 comments on commit 02b1efa

Please sign in to comment.