forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
102 lines (84 loc) · 3.84 KB
/
index.ts
File metadata and controls
102 lines (84 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import { parseConfigFileWithSystem } from "./CommandLineParser";
import { CompilerOptions } from "./CompilerOptions";
import { emitTranspiledFiles, OutputFile } from "./Emit";
import { transpile, TranspiledFile, TranspileResult } from "./Transpile";
export {
createDiagnosticReporter,
parseCommandLine,
ParsedCommandLine,
updateParsedConfigFile,
} from "./CommandLineParser";
export * from "./CompilerOptions";
export * from "./Emit";
export * from "./LuaAST";
export { LuaLibFeature } from "./LuaLib";
export * from "./LuaPrinter";
export * from "./LuaTransformer";
export * from "./Transpile";
export * from "./TranspileError";
export interface TranspileFilesResult {
diagnostics: ts.Diagnostic[];
emitResult: OutputFile[];
}
export function transpileFiles(rootNames: string[], options: CompilerOptions = {}): TranspileFilesResult {
const program = ts.createProgram(rootNames, options);
const { transpiledFiles, diagnostics: transpileDiagnostics } = transpile({ program });
const emitResult = emitTranspiledFiles(program.getCompilerOptions(), transpiledFiles);
const diagnostics = ts.sortAndDeduplicateDiagnostics([
...ts.getPreEmitDiagnostics(program),
...transpileDiagnostics,
]);
return { diagnostics: [...diagnostics], emitResult };
}
export function transpileProject(configFileName: string, optionsToExtend?: CompilerOptions): TranspileFilesResult {
const parseResult = parseConfigFileWithSystem(configFileName, optionsToExtend);
if (parseResult.errors.length > 0) {
return { diagnostics: parseResult.errors, emitResult: [] };
}
return transpileFiles(parseResult.fileNames, parseResult.options);
}
const libCache: { [key: string]: ts.SourceFile } = {};
/** @internal */
export function createVirtualProgram(input: Record<string, string>, options: CompilerOptions = {}): ts.Program {
const compilerHost: ts.CompilerHost = {
fileExists: () => true,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "",
getDefaultLibFileName: ts.getDefaultLibFileName,
readFile: () => "",
getNewLine: () => "\n",
useCaseSensitiveFileNames: () => false,
writeFile: () => {},
getSourceFile: filename => {
if (filename in input) {
return ts.createSourceFile(filename, input[filename], ts.ScriptTarget.Latest, false);
}
if (filename.startsWith("lib.")) {
if (libCache[filename]) return libCache[filename];
const typeScriptDir = path.dirname(require.resolve("typescript"));
const filePath = path.join(typeScriptDir, filename);
const content = fs.readFileSync(filePath, "utf8");
libCache[filename] = ts.createSourceFile(filename, content, ts.ScriptTarget.Latest, false);
return libCache[filename];
}
},
};
return ts.createProgram(Object.keys(input), options, compilerHost);
}
export function transpileVirtualProject(files: Record<string, string>, options: CompilerOptions = {}): TranspileResult {
const program = createVirtualProgram(files, options);
const result = transpile({ program });
const diagnostics = ts.sortAndDeduplicateDiagnostics([...ts.getPreEmitDiagnostics(program), ...result.diagnostics]);
return { ...result, diagnostics: [...diagnostics] };
}
export interface TranspileStringResult {
diagnostics: ts.Diagnostic[];
file?: TranspiledFile;
}
export function transpileString(main: string, options: CompilerOptions = {}): TranspileStringResult {
const { diagnostics, transpiledFiles } = transpileVirtualProject({ "main.ts": main }, options);
return { diagnostics, file: transpiledFiles.find(({ fileName }) => fileName === "main.ts") };
}