@@ -18,7 +18,35 @@ export function makeDefaultTypescriptConfig() {
1818 return defaultTypescriptConfig
1919}
2020
21- export function extractFileNames ( functions : { [ key : string ] : ServerlessFunction } ) : string [ ] {
21+ export function extractFileNames ( cwd : string , provider : string , functions ?: { [ key : string ] : ServerlessFunction } ) : string [ ] {
22+
23+ // The Google provider will use the entrypoint not from the definition of the
24+ // handler function, but instead from the package.json:main field, or via a
25+ // index.js file. This check reads the current package.json in the same way
26+ // that we already read the tsconfig.json file, by inspecting the current
27+ // working directory. If the packageFile does not contain a valid main, then
28+ // it instead selects the index.js file.
29+ if ( provider === 'google' ) {
30+ const packageFilePath = path . join ( cwd , 'package.json' )
31+ if ( fs . existsSync ( packageFilePath ) ) {
32+
33+ // Load in the package.json file.
34+ const packageFile = JSON . parse ( fs . readFileSync ( packageFilePath ) . toString ( ) )
35+
36+ // Either grab the package.json:main field, or use the index.ts file.
37+ // (This will be transpiled to index.js).
38+ const main = packageFile . main ? packageFile . main . replace ( / \. j s $ / , '.ts' ) : 'index.ts'
39+
40+ // Check that the file indeed exists.
41+ if ( ! fs . existsSync ( path . join ( cwd , main ) ) ) {
42+ console . log ( `Cannot locate entrypoint, ${ main } not found` )
43+ throw new Error ( 'Typescript compilation failed' )
44+ }
45+
46+ return [ main ]
47+ }
48+ }
49+
2250 return _ . values ( functions )
2351 . map ( fn => fn . handler )
2452 . map ( h => {
0 commit comments