@@ -11,42 +11,91 @@ import { LuaLibImportKind, LuaTarget, LuaTranspiler } from "./Transpiler";
1111
1212export function compile ( argv : string [ ] ) : void {
1313 const commandLine = parseCommandLine ( argv ) ;
14- compileFilesWithOptions ( commandLine . fileNames , commandLine . options ) ;
14+ /* istanbul ignore if: tested in test/compiler/watchmode.spec with subproccess */
15+ if ( commandLine . options . watch ) {
16+ watchWithOptions ( commandLine . fileNames , commandLine . options ) ;
17+ } else {
18+ compileFilesWithOptions ( commandLine . fileNames , commandLine . options ) ;
19+ }
1520}
1621
17- export function compileFilesWithOptions ( fileNames : string [ ] , options : CompilerOptions ) : void {
18- if ( ! options . luaTarget ) {
19- options . luaTarget = LuaTarget . LuaJIT ;
22+ /* istanbul ignore next: tested in test/compiler/watchmode.spec with subproccess */
23+ export function watchWithOptions ( fileNames : string [ ] , options : CompilerOptions ) : void {
24+ let host : ts . WatchCompilerHost < ts . SemanticDiagnosticsBuilderProgram > ;
25+ let config = false ;
26+ if ( options . project ) {
27+ config = true ;
28+ host = ts . createWatchCompilerHost (
29+ options . project ,
30+ options ,
31+ ts . sys ,
32+ ts . createSemanticDiagnosticsBuilderProgram
33+ ) ;
34+ } else {
35+ host = ts . createWatchCompilerHost (
36+ fileNames ,
37+ options ,
38+ ts . sys ,
39+ ts . createSemanticDiagnosticsBuilderProgram
40+ ) ;
2041 }
2142
43+ host . afterProgramCreate = program => {
44+ const status = emitFilesAndReportErrors ( program . getProgram ( ) ) ;
45+ const errorDiagnostic : ts . Diagnostic = {
46+ category : undefined ,
47+ code : 6194 ,
48+ file : undefined ,
49+ length : 0 ,
50+ messageText : "Found 0 errors. Watching for file changes." ,
51+ start : 0 ,
52+ } ;
53+ if ( status !== 0 ) {
54+ errorDiagnostic . messageText = "Found Errors. Watching for file changes." ;
55+ errorDiagnostic . code = 6193 ;
56+ }
57+ host . onWatchStatusChange (
58+ errorDiagnostic ,
59+ host . getNewLine ( ) ,
60+ program . getCompilerOptions ( )
61+ ) ;
62+ } ;
63+
64+ if ( config ) {
65+ ts . createWatchProgram (
66+ host as ts . WatchCompilerHostOfConfigFile < ts . SemanticDiagnosticsBuilderProgram > ) ;
67+ } else {
68+ ts . createWatchProgram (
69+ host as ts . WatchCompilerHostOfFilesAndCompilerOptions < ts . SemanticDiagnosticsBuilderProgram > ) ;
70+ }
71+ }
72+
73+ export function compileFilesWithOptions ( fileNames : string [ ] , options : CompilerOptions ) : void {
2274 const program = ts . createProgram ( fileNames , options ) ;
2375
76+ emitFilesAndReportErrors ( program ) ;
77+ }
78+
79+ function emitFilesAndReportErrors ( program : ts . Program ) : number {
80+ const options = program . getCompilerOptions ( ) as CompilerOptions ;
81+
2482 const checker = program . getTypeChecker ( ) ;
2583
2684 // Get all diagnostics, ignore unsupported extension
2785 const diagnostics = ts . getPreEmitDiagnostics ( program ) . filter ( diag => diag . code !== 6054 ) ;
28- diagnostics . forEach ( diagnostic => {
29- if ( diagnostic . file ) {
30- const { line, character } =
31- diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ! ) ;
32- const message = ts . flattenDiagnosticMessageText ( diagnostic . messageText , "\n" ) ;
33- console . log (
34- `${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ): ${ message } `
35- ) ;
36- } else {
37- console . log (
38- `${ ts . flattenDiagnosticMessageText ( diagnostic . messageText , "\n" ) } `
39- ) ;
40- }
41- } ) ;
86+ diagnostics . forEach ( reportDiagnostic ) ;
4287
4388 // If there are errors dont emit
4489 if ( diagnostics . filter ( diag => diag . category === ts . DiagnosticCategory . Error ) . length > 0 ) {
45- console . log ( "Stopping compilation process because of errors." ) ;
46- process . exit ( 1 ) ;
90+ if ( ! options . watch ) {
91+ process . exit ( 1 ) ;
92+ } else {
93+ return 1 ;
94+ }
4795 }
4896
4997 program . getSourceFiles ( ) . forEach ( sourceFile => {
98+
5099 if ( ! sourceFile . isDeclarationFile ) {
51100 try {
52101 const rootDir = options . rootDir ;
@@ -101,16 +150,15 @@ export function compileFilesWithOptions(fileNames: string[], options: CompilerOp
101150 path . join ( options . outDir , "lualib_bundle.lua" )
102151 ) ;
103152 }
153+
154+ return 0 ;
104155}
105156
106157export function createTranspiler ( checker : ts . TypeChecker ,
107158 options : ts . CompilerOptions ,
108159 sourceFile : ts . SourceFile ) : LuaTranspiler {
109160 let luaTargetTranspiler : LuaTranspiler ;
110161 switch ( options . luaTarget ) {
111- case LuaTarget . LuaJIT :
112- luaTargetTranspiler = new LuaTranspilerJIT ( checker , options , sourceFile ) ;
113- break ;
114162 case LuaTarget . Lua51 :
115163 luaTargetTranspiler = new LuaTranspiler51 ( checker , options , sourceFile ) ;
116164 break ;
@@ -121,9 +169,24 @@ export function createTranspiler(checker: ts.TypeChecker,
121169 luaTargetTranspiler = new LuaTranspiler53 ( checker , options , sourceFile ) ;
122170 break ;
123171 default :
124- // should not happen
125- throw Error ( "No luaTarget Specified please ensure a target is set!" ) ;
172+ luaTargetTranspiler = new LuaTranspilerJIT ( checker , options , sourceFile ) ;
173+ break ;
126174 }
127175
128176 return luaTargetTranspiler ;
129177}
178+
179+ function reportDiagnostic ( diagnostic : ts . Diagnostic ) : void {
180+ if ( diagnostic . file ) {
181+ const { line, character } =
182+ diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ! ) ;
183+ const message = ts . flattenDiagnosticMessageText ( diagnostic . messageText , "\n" ) ;
184+ console . log (
185+ `${ diagnostic . code } : ${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ): ${ message } `
186+ ) ;
187+ } else {
188+ console . log (
189+ `${ diagnostic . code } : ${ ts . flattenDiagnosticMessageText ( diagnostic . messageText , "\n" ) } `
190+ ) ;
191+ }
192+ }
0 commit comments