11import * as ts from "typescript" ;
22import { readFileSync , writeFileSync } from "fs" ;
33
4- import { LuaTranspiler } from "./Transpiler" ;
4+ import { LuaTranspiler , TranspileError } from "./Transpiler" ;
55import { TSHelper as tsEx } from "./TSHelper" ;
66
77function compile ( fileNames : string [ ] , options : ts . CompilerOptions ) : void {
8+ // Verify target
9+ if ( ( < string > < any > options . target ) != "lua" ) {
10+ console . error ( "Wrong compilation target! Add \"target\": \"lua\" to your tsconfig.json!" ) ;
11+ process . exit ( ) ;
12+ }
13+
814 let program = ts . createProgram ( fileNames , options ) ;
915 let checker = program . getTypeChecker ( ) ;
1016
17+ // Get all diagnostics, ignore unsupported extension
18+ const diagnostics = ts . getPreEmitDiagnostics ( program ) . filter ( diag => diag . code != 6054 ) ;
19+ diagnostics . forEach ( diagnostic => {
20+ if ( diagnostic . file ) {
21+ let { line, character } = diagnostic . file . getLineAndCharacterOfPosition ( diagnostic . start ! ) ;
22+ let message = ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) ;
23+ console . log ( `${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ): ${ message } ` ) ;
24+ }
25+ else {
26+ console . log ( `${ ts . flattenDiagnosticMessageText ( diagnostic . messageText , '\n' ) } ` ) ;
27+ }
28+ } ) ;
29+
30+ // If there are errors dont emit
31+ if ( diagnostics . filter ( diag => diag . category == ts . DiagnosticCategory . Error ) . length > 0 ) {
32+ console . log ( "Stopping compilation process because of errors." ) ;
33+ process . exit ( ) ;
34+ }
35+
1136 program . getSourceFiles ( ) . forEach ( sourceFile => {
1237 if ( ! sourceFile . isDeclarationFile ) {
1338 // Print AST for debugging
1439 //printAST(sourceFile, 0);
1540
16- // Transpile AST
17- let lua = LuaTranspiler . transpileSourceFile ( sourceFile , checker ) ;
18- console . log ( lua ) ;
41+ try {
42+ // Transpile AST
43+ let lua = LuaTranspiler . transpileSourceFile ( sourceFile , checker ) ;
44+ const outPath = sourceFile . fileName . substring ( 0 , sourceFile . fileName . lastIndexOf ( "." ) ) + ".lua" ;
45+ //console.log(outPath);
46+ // Write output
47+ ts . sys . writeFile ( outPath , lua ) ;
48+ } catch ( exception ) {
49+ if ( exception . node ) {
50+ const pos = ts . getLineAndCharacterOfPosition ( sourceFile , exception . node . pos ) ;
51+ // Graciously handle transpilation errors
52+ console . error ( "Encountered error parsing file: " + exception . message ) ;
53+ console . error ( sourceFile . fileName + " line: " + ( 1 + pos . line ) + " column: " + pos . character ) ;
54+ } else {
55+ throw exception ;
56+ }
57+ }
1958 }
2059 } ) ;
2160
@@ -30,7 +69,26 @@ function printAST(node: ts.Node, indent: number) {
3069 node . forEachChild ( child => printAST ( child , indent + 1 ) ) ;
3170}
3271
33- compile ( process . argv . slice ( 2 ) , {
34- noEmitOnError : true , noImplicitAny : true ,
35- target : ts . ScriptTarget . ES5 , module : ts . ModuleKind . CommonJS
36- } ) ;
72+ // Try to find tsconfig.json
73+ const filename = process . argv [ 2 ] . split ( "\\" ) . join ( "/" ) ;
74+ const filepath = filename . substring ( 0 , filename . lastIndexOf ( "/" ) ) ;
75+ let configPath = ts . findConfigFile ( filepath , ts . sys . fileExists ) ;
76+
77+ if ( configPath ) {
78+ configPath = configPath . split ( "\\" ) . join ( "/" ) ;
79+ const projectRoot = configPath . substring ( 0 , configPath . lastIndexOf ( "/" ) ) ;
80+
81+ // Find all files
82+ let files = ts . sys . readDirectory ( projectRoot , [ ".ts" ] ) ;
83+
84+ // Read config
85+ let configFile = ts . readConfigFile ( configPath , ts . sys . readFile ) ;
86+ if ( configFile . error ) {
87+ console . error ( "Error occured:" ) ;
88+ console . error ( configFile . error ) ;
89+ } else {
90+ compile ( files , configFile . config . compilerOptions ) ;
91+ }
92+ } else {
93+ console . error ( "Could not find tsconfig.json, place one in your project root!" ) ;
94+ }
0 commit comments