@@ -96,35 +96,51 @@ namespace ts {
96
96
if ( existingDirectories . has ( directoryPath ) ) {
97
97
return true ;
98
98
}
99
- if ( system . directoryExists ( directoryPath ) ) {
99
+ if ( ( compilerHost . directoryExists || system . directoryExists ) ( directoryPath ) ) {
100
100
existingDirectories . set ( directoryPath , true ) ;
101
101
return true ;
102
102
}
103
103
return false ;
104
104
}
105
105
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 ) ;
115
127
}
116
128
}
117
129
}
118
130
119
131
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
+ }
120
137
121
- function writeFileIfUpdated ( fileName : string , data : string , writeByteOrderMark : boolean ) : void {
122
138
if ( ! outputFingerprints ) {
123
139
outputFingerprints = createMap < OutputFingerprint > ( ) ;
124
140
}
125
141
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 ) ;
128
144
129
145
if ( mtimeBefore ) {
130
146
const fingerprint = outputFingerprints . get ( fileName ) ;
@@ -139,7 +155,7 @@ namespace ts {
139
155
140
156
system . writeFile ( fileName , data , writeByteOrderMark ) ;
141
157
142
- const mtimeAfter = system . getModifiedTime ! ( fileName ) || missingFileModifiedTime ; // TODO: GH#18217
158
+ const mtimeAfter = system . getModifiedTime ( fileName ) || missingFileModifiedTime ;
143
159
144
160
outputFingerprints . set ( fileName , {
145
161
hash,
@@ -148,28 +164,6 @@ namespace ts {
148
164
} ) ;
149
165
}
150
166
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
-
173
167
function getDefaultLibLocation ( ) : string {
174
168
return getDirectoryPath ( normalizePath ( system . getExecutingFilePath ( ) ) ) ;
175
169
}
0 commit comments