@@ -4,6 +4,14 @@ const BbPromise = require('bluebird');
44const fs = require ( 'fs' ) ;
55const path = require ( 'path' ) ;
66
7+ const RUNTIMES_EXTENSIONS = {
8+ node8 : [ 'ts' , 'js' ] ,
9+ node10 : [ 'ts' , 'js' ] ,
10+ python : [ 'py' ] ,
11+ python3 : [ 'py' ] ,
12+ golang : [ 'go' ] ,
13+ } ;
14+
715module . exports = {
816 validate ( ) {
917 return BbPromise . bind ( this )
@@ -55,18 +63,43 @@ module.exports = {
5563 let containerNames = [ ] ;
5664
5765 const currentErrors = Array . isArray ( errors ) ? errors : [ ] ;
58- let functionErrors = [ ] ;
66+ const functionErrors = [ ] ;
5967 let containers = [ ] ;
6068
61- const functions = this . serverless . service . functions ;
69+ const { functions } = this . serverless . service ;
6270 if ( functions && Object . keys ( functions ) . length !== 0 ) {
6371 functionNames = Object . keys ( functions ) ;
6472
73+ // Check that runtime is authorized
74+ const extensions = RUNTIMES_EXTENSIONS [ this . runtime ] ;
75+ if ( ! extensions ) {
76+ const availableRuntimesMessage = Object . keys ( RUNTIMES_EXTENSIONS ) . join ( ', ' ) ;
77+ functionErrors . push ( `Runtime ${ this . runtime } is not supported. Function runtime must be one of the following: ${ availableRuntimesMessage } ` ) ;
78+ }
79+
6580 functionNames . forEach ( ( functionName ) => {
6681 const func = functions [ functionName ] ;
6782 // Check if function handler exists
6883 try {
69- if ( ! fs . existsSync ( path . resolve ( './' , func . handler ) ) ) {
84+ // get handler file => path/to/file.handler => split ['path/to/file', 'handler']
85+ const splitHandlerPath = func . handler . split ( '.' ) ;
86+ if ( splitHandlerPath . length !== 2 ) {
87+ throw new Error ( `Handler is malformatted for ${ functionName } : handler should be path/to/file.functionInsideFile` ) ;
88+ }
89+ const handlerPath = splitHandlerPath [ 0 ] ;
90+
91+ // For each extensions linked to a language (node: .ts,.js, python: .py ...),
92+ // check that a handler file exists with one of the extensions
93+ let handlerFileExists = false ;
94+ for ( let i = 0 ; i < extensions . length ; i += 1 ) {
95+ const extension = extensions [ i ] ;
96+ const handler = `${ handlerPath } .${ extension } ` ;
97+ if ( fs . existsSync ( path . resolve ( './' , handler ) ) ) {
98+ handlerFileExists = true ;
99+ }
100+ }
101+ // If Handler file does not exist, throw an error
102+ if ( ! handlerFileExists ) {
70103 throw new Error ( 'File does not exists' ) ;
71104 }
72105 } catch ( error ) {
@@ -77,6 +110,7 @@ module.exports = {
77110 }
78111
79112 if ( this . serverless . service . custom ) {
113+ // eslint-disable-next-line prefer-destructuring
80114 containers = this . serverless . service . custom . containers ;
81115 }
82116 if ( containers && Object . keys ( containers ) . length !== 0 ) {
0 commit comments