diff --git a/src/typescript.ts b/src/typescript.ts index f18e59b4..02ac9585 100644 --- a/src/typescript.ts +++ b/src/typescript.ts @@ -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'], @@ -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') }) } diff --git a/tests/assets/hello.ts b/tests/assets/hello.ts new file mode 100644 index 00000000..e69de29b diff --git a/tests/assets/jsfile.js b/tests/assets/jsfile.js new file mode 100644 index 00000000..e69de29b diff --git a/tests/assets/world.ts b/tests/assets/world.ts new file mode 100644 index 00000000..e69de29b diff --git a/tests/typescript.extractFileName.test.ts b/tests/typescript.extractFileName.test.ts index 13ca2339..2c2cfbe5 100644 --- a/tests/typescript.extractFileName.test.ts +++ b/tests/typescript.extractFileName.test.ts @@ -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: [] @@ -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', ], ) })