@@ -752,7 +752,7 @@ namespace ts {
752
752
}
753
753
}
754
754
755
- export function parseCommandLine ( commandLine : string [ ] , readFile ?: ( path : string ) => string ) : ParsedCommandLine {
755
+ export function parseCommandLine ( commandLine : string [ ] , readFile ?: ( path : string ) => string | undefined ) : ParsedCommandLine {
756
756
const options : CompilerOptions = { } ;
757
757
const fileNames : string [ ] = [ ] ;
758
758
const errors : Diagnostic [ ] = [ ] ;
@@ -878,15 +878,9 @@ namespace ts {
878
878
* Read tsconfig.json file
879
879
* @param fileName The path to the config file
880
880
*/
881
- export function readConfigFile ( fileName : string , readFile : ( path : string ) => string ) : { config ?: any ; error ?: Diagnostic } {
882
- let text = "" ;
883
- try {
884
- text = readFile ( fileName ) ;
885
- }
886
- catch ( e ) {
887
- return { config : { } , error : createCompilerDiagnostic ( Diagnostics . Cannot_read_file_0_Colon_1 , fileName , e . message ) } ;
888
- }
889
- return parseConfigFileTextToJson ( fileName , text ) ;
881
+ export function readConfigFile ( fileName : string , readFile : ( path : string ) => string | undefined ) : { config ?: any ; error ?: Diagnostic } {
882
+ const textOrDiagnostic = tryReadFile ( fileName , readFile ) ;
883
+ return typeof textOrDiagnostic === "string" ? parseConfigFileTextToJson ( fileName , textOrDiagnostic ) : { config : { } , error : textOrDiagnostic } ;
890
884
}
891
885
892
886
/**
@@ -906,15 +900,20 @@ namespace ts {
906
900
* Read tsconfig.json file
907
901
* @param fileName The path to the config file
908
902
*/
909
- export function readJsonConfigFile ( fileName : string , readFile : ( path : string ) => string ) : JsonSourceFile {
910
- let text = "" ;
903
+ export function readJsonConfigFile ( fileName : string , readFile : ( path : string ) => string | undefined ) : JsonSourceFile {
904
+ const textOrDiagnostic = tryReadFile ( fileName , readFile ) ;
905
+ return typeof textOrDiagnostic === "string" ? parseJsonText ( fileName , textOrDiagnostic ) : < JsonSourceFile > { parseDiagnostics : [ textOrDiagnostic ] } ;
906
+ }
907
+
908
+ function tryReadFile ( fileName : string , readFile : ( path : string ) | string | undefined ) : string | Diagnostic {
909
+ let text : string | undefined ;
911
910
try {
912
911
text = readFile ( fileName ) ;
913
912
}
914
913
catch ( e ) {
915
- return < JsonSourceFile > { parseDiagnostics : [ createCompilerDiagnostic ( Diagnostics . Cannot_read_file_0_Colon_1 , fileName , e . message ) ] } ;
914
+ return createCompilerDiagnostic ( Diagnostics . Cannot_read_file_0_Colon_1 , fileName , e . message ) ;
916
915
}
917
- return parseJsonText ( fileName , text ) ;
916
+ return text === undefined ? createCompilerDiagnostic ( Diagnostics . Cannot_read_file_0_Colon_1 , fileName , "File does not exist." ) : text ;
918
917
}
919
918
920
919
function commandLineOptionsToMap ( options : CommandLineOption [ ] ) {
0 commit comments