Skip to content

Commit e87c8ee

Browse files
committed
Move high-level API to index file
1 parent 19d47de commit e87c8ee

File tree

5 files changed

+121
-125
lines changed

5 files changed

+121
-125
lines changed

src/API.ts

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/index.ts

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
export { transpileFiles, transpileProject, transpileString, TranspileStringResult } from "./API";
1+
import * as fs from "fs";
2+
import * as path from "path";
3+
import * as ts from "typescript";
4+
import { parseConfigFileContent } from "./CommandLineParser";
5+
import { CompilerOptions } from "./CompilerOptions";
6+
import { getTranspileOutput, TranspilationResult, TranspiledFile } from "./Transpile";
7+
28
export { parseConfigFileContent } from "./CommandLineParser";
39
export { CompilerOptions, LuaLibImportKind, LuaTarget } from "./CompilerOptions";
410
export * from "./Emit";
@@ -7,3 +13,107 @@ export { LuaLibFeature } from "./LuaLib";
713
export { LuaPrinter } from "./LuaPrinter";
814
export { LuaTransformer } from "./LuaTransformer";
915
export * from "./Transpile";
16+
17+
export function transpileFiles(
18+
rootNames: string[],
19+
options: CompilerOptions = {}
20+
): TranspilationResult {
21+
const program = ts.createProgram(rootNames, options);
22+
const { diagnostics, transpiledFiles } = getTranspileOutput({ program, options });
23+
24+
const allDiagnostics = ts.sortAndDeduplicateDiagnostics([
25+
...ts.getPreEmitDiagnostics(program),
26+
...diagnostics,
27+
]);
28+
29+
return { transpiledFiles, diagnostics: [...allDiagnostics] };
30+
}
31+
32+
export function transpileProject(fileName: string, options?: CompilerOptions): TranspilationResult {
33+
const parseResult = parseConfigFileContent(
34+
fs.readFileSync(fileName, "utf8"),
35+
fileName,
36+
options
37+
);
38+
if (parseResult.isValid === false) {
39+
// TODO: Return diagnostics
40+
throw new Error(parseResult.errorMessage);
41+
}
42+
43+
return transpileFiles(parseResult.result.fileNames, parseResult.result.options);
44+
}
45+
46+
const libCache: { [key: string]: ts.SourceFile } = {};
47+
48+
/** @internal */
49+
export function createVirtualProgram(
50+
input: Record<string, string>,
51+
options?: CompilerOptions
52+
): ts.Program {
53+
const compilerHost: ts.CompilerHost = {
54+
fileExists: () => true,
55+
getCanonicalFileName: fileName => fileName,
56+
getCurrentDirectory: () => "",
57+
getDefaultLibFileName: ts.getDefaultLibFileName,
58+
readFile: () => "",
59+
getNewLine: () => "\n",
60+
useCaseSensitiveFileNames: () => false,
61+
writeFile: () => {},
62+
63+
getSourceFile: filename => {
64+
if (filename in input) {
65+
return ts.createSourceFile(
66+
filename,
67+
input[filename],
68+
ts.ScriptTarget.Latest,
69+
false
70+
);
71+
}
72+
73+
if (filename.startsWith("lib.")) {
74+
if (libCache[filename]) return libCache[filename];
75+
const typeScriptDir = path.dirname(require.resolve("typescript"));
76+
const filePath = path.join(typeScriptDir, filename);
77+
const content = fs.readFileSync(filePath, "utf8");
78+
79+
libCache[filename] = ts.createSourceFile(
80+
filename,
81+
content,
82+
ts.ScriptTarget.Latest,
83+
false
84+
);
85+
86+
return libCache[filename];
87+
}
88+
},
89+
};
90+
91+
return ts.createProgram(Object.keys(input), options, compilerHost);
92+
}
93+
94+
export interface TranspileStringResult {
95+
file: TranspiledFile;
96+
diagnostics: ts.Diagnostic[];
97+
}
98+
99+
export function transpileString(
100+
input: string | Record<string, string>,
101+
options: CompilerOptions = {}
102+
): TranspileStringResult {
103+
const programFiles = typeof input === "object" ? input : { "main.ts": input };
104+
const mainFileName =
105+
typeof input === "string"
106+
? "main.ts"
107+
: Object.keys(input).find(x => /\bmain\.[a-z]+$/.test(x));
108+
if (mainFileName === undefined) throw new Error('Input should have a file named "main"');
109+
110+
const program = createVirtualProgram(programFiles, options);
111+
const { diagnostics, transpiledFiles } = getTranspileOutput({ program, options });
112+
113+
const allDiagnostics = ts.sortAndDeduplicateDiagnostics([
114+
...ts.getPreEmitDiagnostics(program),
115+
...diagnostics,
116+
]);
117+
118+
return { file: transpiledFiles.get(mainFileName), diagnostics: [...allDiagnostics] };
119+
}

src/tstl.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#!/usr/bin/env node
22
import * as ts from "typescript";
3-
import { transpileFiles } from "./API";
3+
import * as tstl from ".";
44
import * as CommandLineParser from "./CommandLineParser";
5-
import { CompilerOptions } from "./CompilerOptions";
6-
import { emitTranspiledFiles } from "./Emit";
7-
import { getTranspileOutput } from "./Transpile";
85

96
function createDiagnosticReporter(pretty: boolean): ts.DiagnosticReporter {
107
const host: ts.FormatDiagnosticsHost = {
@@ -25,7 +22,7 @@ function createDiagnosticReporter(pretty: boolean): ts.DiagnosticReporter {
2522
};
2623
}
2724

28-
function shouldBePretty(options?: CompilerOptions): boolean {
25+
function shouldBePretty(options?: tstl.CompilerOptions): boolean {
2926
return !options || options.pretty === undefined
3027
? ts.sys.writeOutputIsTTY !== undefined && ts.sys.writeOutputIsTTY()
3128
: Boolean(options.pretty);
@@ -80,11 +77,11 @@ function executeCommandLine(argv: string[]): void {
8077
ts.createWatchProgram(host);
8178
}
8279
} else {
83-
const { diagnostics, transpiledFiles } = transpileFiles(
80+
const { diagnostics, transpiledFiles } = tstl.transpileFiles(
8481
commandLine.result.fileNames,
8582
commandLine.result.options
8683
);
87-
emitTranspiledFiles(commandLine.result.options, transpiledFiles);
84+
tstl.emitTranspiledFiles(commandLine.result.options, transpiledFiles);
8885

8986
diagnostics.forEach(reportDiagnostic);
9087
if (diagnostics.filter(d => d.category === ts.DiagnosticCategory.Error).length === 0) {
@@ -101,7 +98,7 @@ function executeCommandLine(argv: string[]): void {
10198

10299
function updateWatchCompilerHost(
103100
host: ts.WatchCompilerHost<ts.SemanticDiagnosticsBuilderProgram>,
104-
options: CompilerOptions
101+
options: tstl.CompilerOptions
105102
): void {
106103
let fullRecompile = true;
107104
host.afterProgramCreate = builderProgram => {
@@ -123,12 +120,12 @@ function updateWatchCompilerHost(
123120
}
124121
}
125122

126-
const { diagnostics: emitDiagnostics, transpiledFiles } = getTranspileOutput({
123+
const { diagnostics: emitDiagnostics, transpiledFiles } = tstl.getTranspileOutput({
127124
program,
128125
options,
129126
sourceFiles,
130127
});
131-
emitTranspiledFiles(options, transpiledFiles);
128+
tstl.emitTranspiledFiles(options, transpiledFiles);
132129

133130
const diagnostics = ts.sortAndDeduplicateDiagnostics([
134131
...preEmitDiagnostics,

test/util.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as fs from "fs";
33
import * as path from "path";
44
import * as ts from "typescript";
55
import * as tstl from "../src";
6-
import { createVirtualProgram } from "../src/API";
76

87
export const nodeStub = ts.createNode(ts.SyntaxKind.Unknown);
98

@@ -167,7 +166,7 @@ export function parseTypeScript(
167166
typescript: string,
168167
target: tstl.LuaTarget = tstl.LuaTarget.Lua53,
169168
): [ts.SourceFile, ts.TypeChecker] {
170-
const program = createVirtualProgram({ "main.ts": typescript }, { luaTarget: target });
169+
const program = tstl.createVirtualProgram({ "main.ts": typescript }, { luaTarget: target });
171170
return [program.getSourceFile("main.ts"), program.getTypeChecker()];
172171
}
173172

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"declaration": true,
88
"sourceMap": true,
99
"target": "es2017",
10-
"module": "commonjs"
10+
"module": "commonjs",
11+
"stripInternal": true
1112
},
1213
"include": ["src"],
1314
"exclude": ["src/lualib"]

0 commit comments

Comments
 (0)