@@ -96,35 +96,51 @@ namespace ts {
9696 if ( existingDirectories . has ( directoryPath ) ) {
9797 return true ;
9898 }
99- if ( system . directoryExists ( directoryPath ) ) {
99+ if ( ( compilerHost . directoryExists || system . directoryExists ) ( directoryPath ) ) {
100100 existingDirectories . set ( directoryPath , true ) ;
101101 return true ;
102102 }
103103 return false ;
104104 }
105105
106- function ensureDirectoriesExist ( directoryPath : string ) {
107- if ( directoryPath . length > getRootLength ( directoryPath ) && ! directoryExists ( directoryPath ) ) {
108- const parentDirectory = getDirectoryPath ( directoryPath ) ;
109- ensureDirectoriesExist ( parentDirectory ) ;
110- if ( compilerHost . createDirectory ) {
111- compilerHost . createDirectory ( directoryPath ) ;
112- }
113- else {
114- system . createDirectory ( directoryPath ) ;
106+ function writeFile ( fileName : string , data : string , writeByteOrderMark : boolean , onError ?: ( message : string ) => void ) {
107+ try {
108+ performance . mark ( "beforeIOWrite" ) ;
109+
110+ // NOTE: If patchWriteFileEnsuringDirectory has been called,
111+ // the system.writeFile will do its own directory creation and
112+ // the ensureDirectoriesExist call will always be redundant.
113+ writeFileEnsuringDirectories (
114+ fileName ,
115+ data ,
116+ writeByteOrderMark ,
117+ ( path , data , writeByteOrderMark ) => writeFileWorker ( path , data , writeByteOrderMark ) ,
118+ path => ( compilerHost . createDirectory || system . createDirectory ) ( path ) ,
119+ path => directoryExists ( path ) ) ;
120+
121+ performance . mark ( "afterIOWrite" ) ;
122+ performance . measure ( "I/O Write" , "beforeIOWrite" , "afterIOWrite" ) ;
123+ }
124+ catch ( e ) {
125+ if ( onError ) {
126+ onError ( e . message ) ;
115127 }
116128 }
117129 }
118130
119131 let outputFingerprints : Map < OutputFingerprint > ;
132+ function writeFileWorker ( fileName : string , data : string , writeByteOrderMark : boolean ) {
133+ if ( ! isWatchSet ( options ) || ! system . createHash || ! system . getModifiedTime ) {
134+ system . writeFile ( fileName , data , writeByteOrderMark ) ;
135+ return ;
136+ }
120137
121- function writeFileIfUpdated ( fileName : string , data : string , writeByteOrderMark : boolean ) : void {
122138 if ( ! outputFingerprints ) {
123139 outputFingerprints = createMap < OutputFingerprint > ( ) ;
124140 }
125141
126- const hash = system . createHash ! ( data ) ; // TODO: GH#18217
127- const mtimeBefore = system . getModifiedTime ! ( fileName ) ; // TODO: GH#18217
142+ const hash = system . createHash ( data ) ;
143+ const mtimeBefore = system . getModifiedTime ( fileName ) ;
128144
129145 if ( mtimeBefore ) {
130146 const fingerprint = outputFingerprints . get ( fileName ) ;
@@ -139,7 +155,7 @@ namespace ts {
139155
140156 system . writeFile ( fileName , data , writeByteOrderMark ) ;
141157
142- const mtimeAfter = system . getModifiedTime ! ( fileName ) || missingFileModifiedTime ; // TODO: GH#18217
158+ const mtimeAfter = system . getModifiedTime ( fileName ) || missingFileModifiedTime ;
143159
144160 outputFingerprints . set ( fileName , {
145161 hash,
@@ -148,28 +164,6 @@ namespace ts {
148164 } ) ;
149165 }
150166
151- function writeFile ( fileName : string , data : string , writeByteOrderMark : boolean , onError ?: ( message : string ) => void ) {
152- try {
153- performance . mark ( "beforeIOWrite" ) ;
154- ensureDirectoriesExist ( getDirectoryPath ( normalizePath ( fileName ) ) ) ;
155-
156- if ( isWatchSet ( options ) && system . createHash && system . getModifiedTime ) {
157- writeFileIfUpdated ( fileName , data , writeByteOrderMark ) ;
158- }
159- else {
160- system . writeFile ( fileName , data , writeByteOrderMark ) ;
161- }
162-
163- performance . mark ( "afterIOWrite" ) ;
164- performance . measure ( "I/O Write" , "beforeIOWrite" , "afterIOWrite" ) ;
165- }
166- catch ( e ) {
167- if ( onError ) {
168- onError ( e . message ) ;
169- }
170- }
171- }
172-
173167 function getDefaultLibLocation ( ) : string {
174168 return getDirectoryPath ( normalizePath ( system . getExecutingFilePath ( ) ) ) ;
175169 }
0 commit comments