1
+ import { readFileSync } from "fs" ;
2
+ import * as ts from "typescript" ;
3
+
4
+ class LanguageServiceHost implements ts . LanguageServiceHost {
5
+ files : { [ fileName : string ] : { file : ts . IScriptSnapshot ; ver : number } } = { }
6
+ log = _ => { } ;
7
+ trace = _ => { } ;
8
+ error = _ => { } ;
9
+ getCompilationSettings = ts . getDefaultCompilerOptions ;
10
+ getScriptIsOpen = _ => true ;
11
+ getCurrentDirectory = ( ) => "" ;
12
+ getDefaultLibFileName = _ => "lib" ;
13
+ getScriptVersion = fileName => this . files [ fileName ] . ver . toString ( ) ;
14
+ getScriptSnapshot = fileName => this . files [ fileName ] . file ;
15
+
16
+ getScriptFileNames ( ) : string [ ] {
17
+ var names : string [ ] = [ ] ;
18
+ for ( var name in this . files ) {
19
+ if ( this . files . hasOwnProperty ( name ) ) {
20
+ names . push ( name ) ;
21
+ }
22
+ }
23
+ return names ;
24
+ }
25
+
26
+ addFile ( fileName : string , body : string ) {
27
+ var snap = ts . ScriptSnapshot . fromString ( body ) ;
28
+ snap . getChangeRange = _ => undefined ;
29
+ var existing = this . files [ fileName ] ;
30
+ if ( existing ) {
31
+ this . files [ fileName ] . ver ++ ;
32
+ this . files [ fileName ] . file = snap
33
+ } else {
34
+ this . files [ fileName ] = { ver : 1 , file : snap } ;
35
+ }
36
+ }
37
+ }
38
+
39
+ class CompilerHost extends LanguageServiceHost implements ts . CompilerHost {
40
+ getSourceFile ( filename : string , languageVersion : ts . ScriptTarget , onError ?: ( message : string ) => void ) : ts . SourceFile {
41
+ var f = this . files [ filename ] ;
42
+ if ( ! f ) return null ;
43
+ var sourceFile = ts . createLanguageServiceSourceFile ( filename , f . file , ts . ScriptTarget . ES5 , f . ver . toString ( ) , true ) ;
44
+ return sourceFile ;
45
+ }
46
+ writeFile = ( fileName , content ) => ts . sys . writeFile ( fileName , content ) ;
47
+ getCanonicalFileName = ( fileName : string ) => fileName ;
48
+ useCaseSensitiveFileNames = ( ) => true ;
49
+ getNewLine = ( ) => "\n" ;
50
+ fileExists = ( fileName : string ) => ts . sys . fileExists ( fileName ) ;
51
+ readFile = ( fileName : string ) => ts . sys . readFile ( fileName ) ;
52
+ }
53
+
54
+ const fileNames = [ "C:\\Users\\sagi\\Documents\\Visual Studio 2015\\Projects\\NodejsConsoleApp1\\NodejsConsoleApp1\\Scripts\\Greeter.ts" ,
55
+ "C:\\Users\\sagi\\Documents\\Visual Studio 2015\\Projects\\NodejsConsoleApp1\\NodejsConsoleApp1\\Scripts\\IPoint.ts" ] ;
56
+
57
+ declare var process : any ;
58
+ declare var console : any ;
59
+ declare var os : any ;
60
+
61
+ export function compile ( fileNames : string [ ] , options : ts . CompilerOptions ) : void {
62
+ var program = ts . createProgram ( fileNames , options ) ;
63
+ var emitResult = program . emit ( ) ;
64
+
65
+ var allDiagnostics = ts . getPreEmitDiagnostics ( program ) . concat ( emitResult . diagnostics ) ;
66
+
67
+ allDiagnostics . forEach ( diagnostic => {
68
+ var { line, character } = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ) ;
69
+ var message = ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) ;
70
+ console . log ( `${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ): ${ message } ` ) ;
71
+ } ) ;
72
+
73
+ var exitCode = emitResult . emitSkipped ? 1 : 0 ;
74
+ console . log ( `Process exiting with code '${ exitCode } '.` ) ;
75
+ process . exit ( exitCode ) ;
76
+ }
77
+
78
+ compile ( fileNames , {
79
+ noEmitOnError : true ,
80
+ noImplicitAny : true ,
81
+ target : ts . ScriptTarget . ES5 ,
82
+ module : ts . ModuleKind . None ,
83
+ outFile : "test.js" ,
84
+ removeComments : true
85
+ } ) ;
0 commit comments