@@ -67,19 +67,24 @@ namespace ts {
67
67
}
68
68
69
69
export function createCompilerHost ( options : CompilerOptions , setParentNodes ?: boolean ) : CompilerHost {
70
+ return createCompilerHostWorker ( options , setParentNodes ) ;
71
+ }
72
+ /*@internal */
73
+ // TODO(shkamat): update this after reworking ts build API
74
+ export function createCompilerHostWorker ( options : CompilerOptions , setParentNodes ?: boolean , system = sys ) : CompilerHost {
70
75
const existingDirectories = createMap < boolean > ( ) ;
71
76
72
77
function getCanonicalFileName ( fileName : string ) : string {
73
78
// if underlying system can distinguish between two files whose names differs only in cases then file name already in canonical form.
74
79
// otherwise use toLowerCase as a canonical form.
75
- return sys . useCaseSensitiveFileNames ? fileName : fileName . toLowerCase ( ) ;
80
+ return system . useCaseSensitiveFileNames ? fileName : fileName . toLowerCase ( ) ;
76
81
}
77
82
78
83
function getSourceFile ( fileName : string , languageVersion : ScriptTarget , onError ?: ( message : string ) => void ) : SourceFile | undefined {
79
84
let text : string | undefined ;
80
85
try {
81
86
performance . mark ( "beforeIORead" ) ;
82
- text = sys . readFile ( fileName , options . charset ) ;
87
+ text = system . readFile ( fileName , options . charset ) ;
83
88
performance . mark ( "afterIORead" ) ;
84
89
performance . measure ( "I/O Read" , "beforeIORead" , "afterIORead" ) ;
85
90
}
@@ -97,7 +102,7 @@ namespace ts {
97
102
if ( existingDirectories . has ( directoryPath ) ) {
98
103
return true ;
99
104
}
100
- if ( sys . directoryExists ( directoryPath ) ) {
105
+ if ( system . directoryExists ( directoryPath ) ) {
101
106
existingDirectories . set ( directoryPath , true ) ;
102
107
return true ;
103
108
}
@@ -108,7 +113,7 @@ namespace ts {
108
113
if ( directoryPath . length > getRootLength ( directoryPath ) && ! directoryExists ( directoryPath ) ) {
109
114
const parentDirectory = getDirectoryPath ( directoryPath ) ;
110
115
ensureDirectoriesExist ( parentDirectory ) ;
111
- sys . createDirectory ( directoryPath ) ;
116
+ system . createDirectory ( directoryPath ) ;
112
117
}
113
118
}
114
119
@@ -119,8 +124,8 @@ namespace ts {
119
124
outputFingerprints = createMap < OutputFingerprint > ( ) ;
120
125
}
121
126
122
- const hash = sys . createHash ! ( data ) ; // TODO: GH#18217
123
- const mtimeBefore = sys . getModifiedTime ! ( fileName ) ; // TODO: GH#18217
127
+ const hash = system . createHash ! ( data ) ; // TODO: GH#18217
128
+ const mtimeBefore = system . getModifiedTime ! ( fileName ) ; // TODO: GH#18217
124
129
125
130
if ( mtimeBefore ) {
126
131
const fingerprint = outputFingerprints . get ( fileName ) ;
@@ -133,9 +138,9 @@ namespace ts {
133
138
}
134
139
}
135
140
136
- sys . writeFile ( fileName , data , writeByteOrderMark ) ;
141
+ system . writeFile ( fileName , data , writeByteOrderMark ) ;
137
142
138
- const mtimeAfter = sys . getModifiedTime ! ( fileName ) || missingFileModifiedTime ; // TODO: GH#18217
143
+ const mtimeAfter = system . getModifiedTime ! ( fileName ) || missingFileModifiedTime ; // TODO: GH#18217
139
144
140
145
outputFingerprints . set ( fileName , {
141
146
hash,
@@ -149,11 +154,11 @@ namespace ts {
149
154
performance . mark ( "beforeIOWrite" ) ;
150
155
ensureDirectoriesExist ( getDirectoryPath ( normalizePath ( fileName ) ) ) ;
151
156
152
- if ( isWatchSet ( options ) && sys . createHash && sys . getModifiedTime ) {
157
+ if ( isWatchSet ( options ) && system . createHash && system . getModifiedTime ) {
153
158
writeFileIfUpdated ( fileName , data , writeByteOrderMark ) ;
154
159
}
155
160
else {
156
- sys . writeFile ( fileName , data , writeByteOrderMark ) ;
161
+ system . writeFile ( fileName , data , writeByteOrderMark ) ;
157
162
}
158
163
159
164
performance . mark ( "afterIOWrite" ) ;
@@ -167,32 +172,29 @@ namespace ts {
167
172
}
168
173
169
174
function getDefaultLibLocation ( ) : string {
170
- return getDirectoryPath ( normalizePath ( sys . getExecutingFilePath ( ) ) ) ;
175
+ return getDirectoryPath ( normalizePath ( system . getExecutingFilePath ( ) ) ) ;
171
176
}
172
177
173
- const newLine = getNewLineCharacter ( options ) ;
174
- const realpath = sys . realpath && ( ( path : string ) => sys . realpath ! ( path ) ) ;
178
+ const newLine = getNewLineCharacter ( options , ( ) => system . newLine ) ;
179
+ const realpath = system . realpath && ( ( path : string ) => system . realpath ! ( path ) ) ;
175
180
176
181
return {
177
182
getSourceFile,
178
183
getDefaultLibLocation,
179
184
getDefaultLibFileName : options => combinePaths ( getDefaultLibLocation ( ) , getDefaultLibFileName ( options ) ) ,
180
185
writeFile,
181
- getCurrentDirectory : memoize ( ( ) => sys . getCurrentDirectory ( ) ) ,
182
- useCaseSensitiveFileNames : ( ) => sys . useCaseSensitiveFileNames ,
186
+ getCurrentDirectory : memoize ( ( ) => system . getCurrentDirectory ( ) ) ,
187
+ useCaseSensitiveFileNames : ( ) => system . useCaseSensitiveFileNames ,
183
188
getCanonicalFileName,
184
189
getNewLine : ( ) => newLine ,
185
- fileExists : fileName => sys . fileExists ( fileName ) ,
186
- readFile : fileName => sys . readFile ( fileName ) ,
187
- trace : ( s : string ) => sys . write ( s + newLine ) ,
188
- directoryExists : directoryName => sys . directoryExists ( directoryName ) ,
189
- getEnvironmentVariable : name => sys . getEnvironmentVariable ? sys . getEnvironmentVariable ( name ) : "" ,
190
- getDirectories : ( path : string ) => sys . getDirectories ( path ) ,
190
+ fileExists : fileName => system . fileExists ( fileName ) ,
191
+ readFile : fileName => system . readFile ( fileName ) ,
192
+ trace : ( s : string ) => system . write ( s + newLine ) ,
193
+ directoryExists : directoryName => system . directoryExists ( directoryName ) ,
194
+ getEnvironmentVariable : name => system . getEnvironmentVariable ? system . getEnvironmentVariable ( name ) : "" ,
195
+ getDirectories : ( path : string ) => system . getDirectories ( path ) ,
191
196
realpath,
192
- readDirectory : ( path , extensions , include , exclude , depth ) => sys . readDirectory ( path , extensions , include , exclude , depth ) ,
193
- getModifiedTime : sys . getModifiedTime && ( path => sys . getModifiedTime ! ( path ) ) ,
194
- setModifiedTime : sys . setModifiedTime && ( ( path , date ) => sys . setModifiedTime ! ( path , date ) ) ,
195
- deleteFile : sys . deleteFile && ( path => sys . deleteFile ! ( path ) )
197
+ readDirectory : ( path , extensions , include , exclude , depth ) => system . readDirectory ( path , extensions , include , exclude , depth )
196
198
} ;
197
199
}
198
200
@@ -2319,27 +2321,20 @@ namespace ts {
2319
2321
}
2320
2322
2321
2323
function computeCommonSourceDirectory ( sourceFiles : SourceFile [ ] ) : string {
2322
- const fileNames : string [ ] = [ ] ;
2323
- for ( const file of sourceFiles ) {
2324
- if ( ! file . isDeclarationFile ) {
2325
- fileNames . push ( file . fileName ) ;
2326
- }
2327
- }
2324
+ const fileNames = mapDefined ( sourceFiles , file => file . isDeclarationFile ? undefined : file . fileName ) ;
2328
2325
return computeCommonSourceDirectoryOfFilenames ( fileNames , currentDirectory , getCanonicalFileName ) ;
2329
2326
}
2330
2327
2331
- function checkSourceFilesBelongToPath ( sourceFiles : SourceFile [ ] , rootDirectory : string ) : boolean {
2328
+ function checkSourceFilesBelongToPath ( sourceFiles : ReadonlyArray < SourceFile > , rootDirectory : string ) : boolean {
2332
2329
let allFilesBelongToPath = true ;
2333
- if ( sourceFiles ) {
2334
- const absoluteRootDirectoryPath = host . getCanonicalFileName ( getNormalizedAbsolutePath ( rootDirectory , currentDirectory ) ) ;
2335
-
2336
- for ( const sourceFile of sourceFiles ) {
2337
- if ( ! sourceFile . isDeclarationFile ) {
2338
- const absoluteSourceFilePath = host . getCanonicalFileName ( getNormalizedAbsolutePath ( sourceFile . fileName , currentDirectory ) ) ;
2339
- if ( absoluteSourceFilePath . indexOf ( absoluteRootDirectoryPath ) !== 0 ) {
2340
- programDiagnostics . add ( createCompilerDiagnostic ( Diagnostics . File_0_is_not_under_rootDir_1_rootDir_is_expected_to_contain_all_source_files , sourceFile . fileName , rootDirectory ) ) ;
2341
- allFilesBelongToPath = false ;
2342
- }
2330
+ const absoluteRootDirectoryPath = host . getCanonicalFileName ( getNormalizedAbsolutePath ( rootDirectory , currentDirectory ) ) ;
2331
+
2332
+ for ( const sourceFile of sourceFiles ) {
2333
+ if ( ! sourceFile . isDeclarationFile ) {
2334
+ const absoluteSourceFilePath = host . getCanonicalFileName ( getNormalizedAbsolutePath ( sourceFile . fileName , currentDirectory ) ) ;
2335
+ if ( absoluteSourceFilePath . indexOf ( absoluteRootDirectoryPath ) !== 0 ) {
2336
+ programDiagnostics . add ( createCompilerDiagnostic ( Diagnostics . File_0_is_not_under_rootDir_1_rootDir_is_expected_to_contain_all_source_files , sourceFile . fileName , rootDirectory ) ) ;
2337
+ allFilesBelongToPath = false ;
2343
2338
}
2344
2339
}
2345
2340
}
0 commit comments