@@ -4,8 +4,12 @@ module TypeScript.WebTsc {
44
55 declare var RealActiveXObject : { new ( s : string ) : any } ;
66
7- function getWScriptSystem ( ) : System {
7+ function getWScriptSystem ( ) {
88 var fso = new RealActiveXObject ( "Scripting.FileSystemObject" ) ;
9+
10+ var fileStream = new ActiveXObject ( "ADODB.Stream" ) ;
11+ fileStream . Type = 2 /*text*/ ;
12+
913 var args : string [ ] = [ ] ;
1014 for ( var i = 0 ; i < WScript . Arguments . length ; i ++ ) {
1115 args [ i ] = WScript . Arguments . Item ( i ) ;
@@ -19,17 +23,35 @@ module TypeScript.WebTsc {
1923 writeErr ( s : string ) : void {
2024 WScript . StdErr . Write ( s ) ;
2125 } ,
22- readFile ( fileName : string ) : string {
26+ readFile ( fileName : string , encoding ?: string ) : string {
27+ if ( ! fso . FileExists ( fileName ) ) {
28+ return undefined ;
29+ }
30+ fileStream . Open ( ) ;
2331 try {
24- var f = fso . OpenTextFile ( fileName , 1 ) ;
25- var s : string = f . ReadAll ( ) ;
26- // TODO: Properly handle byte order marks
27- if ( s . length >= 3 && s . charCodeAt ( 0 ) === 0xEF && s . charCodeAt ( 1 ) === 0xBB && s . charCodeAt ( 2 ) === 0xBF ) s = s . slice ( 3 ) ;
28- f . Close ( ) ;
32+ if ( encoding ) {
33+ fileStream . Charset = encoding ;
34+ fileStream . LoadFromFile ( fileName ) ;
35+ }
36+ else {
37+ // Load file and read the first two bytes into a string with no interpretation
38+ fileStream . Charset = "x-ansi" ;
39+ fileStream . LoadFromFile ( fileName ) ;
40+ var bom = fileStream . ReadText ( 2 ) || "" ;
41+ // Position must be at 0 before encoding can be changed
42+ fileStream . Position = 0 ;
43+ // [0xFF,0xFE] and [0xFE,0xFF] mean utf-16 (little or big endian), otherwise default to utf-8
44+ fileStream . Charset = bom . length >= 2 && ( bom . charCodeAt ( 0 ) === 0xFF && bom . charCodeAt ( 1 ) === 0xFE || bom . charCodeAt ( 0 ) === 0xFE && bom . charCodeAt ( 1 ) === 0xFF ) ? "unicode" : "utf-8" ;
45+ }
46+ // ReadText method always strips byte order mark from resulting string
47+ return fileStream . ReadText ( ) ;
2948 }
3049 catch ( e ) {
50+ throw e ;
51+ }
52+ finally {
53+ fileStream . Close ( ) ;
3154 }
32- return s ;
3355 } ,
3456 writeFile ( fileName : string , data : string ) : boolean {
3557 var f = fso . CreateTextFile ( fileName , true ) ;
0 commit comments