@@ -104,6 +104,23 @@ function convertSeverity(mistakeType: string): DiagnosticSeverity {
104104 return DiagnosticSeverity . Error ;
105105}
106106
107+ function isCloudFormation ( template : string , filename : string ) : Boolean {
108+
109+ if ( / " ? A W S T e m p l a t e F o r m a t V e r s i o n " ? \s * / . exec ( template ) ) {
110+ connection . console . log ( "Determined this file is a CloudFormation Template. " + filename +
111+ ". Found the string AWSTemplateFormatVersion" ) ;
112+ return true ;
113+ }
114+ if ( / " ? R e s o u r c e s " ? \s * : / . exec ( template ) ) {
115+ if ( / " ? T y p e " ? \s * : \s * " ? ' ? ( A W S | C u s t o m ) : : / . exec ( template ) ) {
116+ connection . console . log ( "Determined this file is a CloudFormation Template. " + filename +
117+ ". Found 'Resources' and 'Type: (AWS|Custom)::'" ) ;
118+ return true ;
119+ }
120+ }
121+ return false ;
122+ }
123+
107124function validateCloudFormationFile ( document : TextDocument ) : void {
108125 let uri = document . uri ;
109126
@@ -114,16 +131,7 @@ function validateCloudFormationFile(document: TextDocument): void {
114131
115132 let file_to_lint = Files . uriToFilePath ( uri ) ;
116133
117- let is_cfn_regex = new RegExp ( '"?AWSTemplateFormatVersion"?\s*' ) ;
118- let is_cfn = false ;
119- let text = document . getText ( ) . split ( "\n" ) ;
120- for ( var index in text ) {
121- if ( is_cfn_regex . exec ( text [ index ] ) ) {
122- is_cfn = true ;
123- }
124- }
125-
126- connection . console . log ( "File '" + uri . toString ( ) + " is a CFN? " + is_cfn ) ;
134+ let is_cfn = isCloudFormation ( document . getText ( ) , uri . toString ( ) ) ;
127135
128136 if ( is_cfn ) {
129137 let args = [ '--format' , 'json' , '--template' , file_to_lint ] ;
@@ -222,6 +230,10 @@ function validateCloudFormationFile(document: TextDocument): void {
222230 connection . sendDiagnostics ( { uri : filename , diagnostics } ) ;
223231 isValidating [ uri ] = false ;
224232 } ) ;
233+ } else {
234+ connection . console . log ( "Don't believe this is a CloudFormation template. " + uri . toString ( ) +
235+ ". If it is please add AWSTemplateFormatVersion: '2010-09-09' (YAML) or " +
236+ " \"AWSTemplateFormatVersion\": \"2010-09-09\" (JSON) into the root level of the document." ) ;
225237 }
226238}
227239
0 commit comments