|
| 1 | +"use strict"; |
| 2 | +exports.__esModule = true; |
| 3 | +var ts = require("typescript"); |
| 4 | +var Transpiler_1 = require("./Transpiler"); |
| 5 | +var TSHelper_1 = require("./TSHelper"); |
| 6 | +function compile(fileNames, options) { |
| 7 | + // Verify target |
| 8 | + if (options.target != "lua") { |
| 9 | + console.error("Wrong compilation target! Add \"target\": \"lua\" to your tsconfig.json!"); |
| 10 | + process.exit(); |
| 11 | + } |
| 12 | + var program = ts.createProgram(fileNames, options); |
| 13 | + var checker = program.getTypeChecker(); |
| 14 | + // Get all diagnostics, ignore unsupported extension |
| 15 | + var diagnostics = ts.getPreEmitDiagnostics(program).filter(function (diag) { return diag.code != 6054; }); |
| 16 | + diagnostics.forEach(function (diagnostic) { |
| 17 | + if (diagnostic.file) { |
| 18 | + var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; |
| 19 | + var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); |
| 20 | + console.log(diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + "): " + message); |
| 21 | + } |
| 22 | + else { |
| 23 | + console.log("" + ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')); |
| 24 | + } |
| 25 | + }); |
| 26 | + // If there are errors dont emit |
| 27 | + if (diagnostics.filter(function (diag) { return diag.category == ts.DiagnosticCategory.Error; }).length > 0) { |
| 28 | + console.log("Stopping compilation process because of errors."); |
| 29 | + process.exit(); |
| 30 | + } |
| 31 | + program.getSourceFiles().forEach(function (sourceFile) { |
| 32 | + if (!sourceFile.isDeclarationFile) { |
| 33 | + // Print AST for debugging |
| 34 | + //printAST(sourceFile, 0); |
| 35 | + try { |
| 36 | + // Transpile AST |
| 37 | + var lua = Transpiler_1.LuaTranspiler.transpileSourceFile(sourceFile, checker); |
| 38 | + var outPath = sourceFile.fileName.substring(0, sourceFile.fileName.lastIndexOf(".")) + ".lua"; |
| 39 | + //console.log(outPath); |
| 40 | + // Write output |
| 41 | + ts.sys.writeFile(outPath, lua); |
| 42 | + } |
| 43 | + catch (exception) { |
| 44 | + if (exception.node) { |
| 45 | + var pos = ts.getLineAndCharacterOfPosition(sourceFile, exception.node.pos); |
| 46 | + // Graciously handle transpilation errors |
| 47 | + console.error("Encountered error parsing file: " + exception.message); |
| 48 | + console.error(sourceFile.fileName + " line: " + (1 + pos.line) + " column: " + pos.character); |
| 49 | + console.error(exception.stack); |
| 50 | + } |
| 51 | + else { |
| 52 | + throw exception; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + }); |
| 57 | + process.exit(); |
| 58 | +} |
| 59 | +function printAST(node, indent) { |
| 60 | + var indentStr = ""; |
| 61 | + for (var i = 0; i < indent; i++) |
| 62 | + indentStr += " "; |
| 63 | + console.log(indentStr + TSHelper_1.TSHelper.enumName(node.kind, ts.SyntaxKind)); |
| 64 | + node.forEachChild(function (child) { return printAST(child, indent + 1); }); |
| 65 | +} |
| 66 | +// Try to find tsconfig.json |
| 67 | +var filename = process.argv[2].split("\\").join("/"); |
| 68 | +var filepath = filename.substring(0, filename.lastIndexOf("/")); |
| 69 | +var configPath = ts.findConfigFile(filepath, ts.sys.fileExists); |
| 70 | +if (configPath) { |
| 71 | + configPath = configPath.split("\\").join("/"); |
| 72 | + var projectRoot = configPath.substring(0, configPath.lastIndexOf("/")); |
| 73 | + // Find all files |
| 74 | + var files = ts.sys.readDirectory(projectRoot, [".ts"]); |
| 75 | + // Read config |
| 76 | + var configFile = ts.readConfigFile(configPath, ts.sys.readFile); |
| 77 | + if (configFile.error) { |
| 78 | + console.error("Error occured:"); |
| 79 | + console.error(configFile.error); |
| 80 | + } |
| 81 | + else { |
| 82 | + compile(files, configFile.config.compilerOptions); |
| 83 | + } |
| 84 | +} |
| 85 | +else { |
| 86 | + console.error("Could not find tsconfig.json, place one in your project root!"); |
| 87 | +} |
0 commit comments