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