X Tutup
Skip to content

Commit 548fda9

Browse files
authored
Feature/emit host (#647)
* Added EmitHost * Fixed build failing * Use ts.sys.readFile as default instead of fs.readFile * Made emithost default ts.sys, removed it being passed in without use. * Use method syntax for emithost readFile
1 parent eb7ae08 commit 548fda9

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

build_lualib.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ if (fs.existsSync(bundlePath)) {
1616
fs.unlinkSync(bundlePath);
1717
}
1818

19-
fs.writeFileSync(bundlePath, LuaLib.loadFeatures(Object.values(tstl.LuaLibFeature)));
19+
const emitHost = {
20+
readFile: (path: string) => fs.readFileSync(path, "utf-8"),
21+
writeFile: fs.writeFileSync,
22+
};
23+
fs.writeFileSync(bundlePath, LuaLib.loadFeatures(Object.values(tstl.LuaLibFeature), emitHost));

src/Emit.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as fs from "fs";
21
import * as path from "path";
2+
import * as ts from "typescript";
33
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
4-
import { TranspiledFile } from "./Transpile";
4+
import { TranspiledFile, EmitHost } from "./Transpile";
55

66
const trimExt = (filePath: string) => filePath.slice(0, -path.extname(filePath).length);
77
const normalizeSlashes = (filePath: string) => filePath.replace(/\\/g, "/");
@@ -12,7 +12,11 @@ export interface OutputFile {
1212
}
1313

1414
let lualibContent: string;
15-
export function emitTranspiledFiles(options: CompilerOptions, transpiledFiles: TranspiledFile[]): OutputFile[] {
15+
export function emitTranspiledFiles(
16+
options: CompilerOptions,
17+
transpiledFiles: TranspiledFile[],
18+
emitHost: EmitHost = ts.sys
19+
): OutputFile[] {
1620
let { rootDir, outDir, outFile, luaLibImport } = options;
1721

1822
const configFileName = options.configFilePath as string | undefined;
@@ -57,7 +61,12 @@ export function emitTranspiledFiles(options: CompilerOptions, transpiledFiles: T
5761

5862
if (luaLibImport === LuaLibImportKind.Require || luaLibImport === LuaLibImportKind.Always) {
5963
if (lualibContent === undefined) {
60-
lualibContent = fs.readFileSync(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"), "utf8");
64+
const lualibBundle = emitHost.readFile(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"));
65+
if (lualibBundle !== undefined) {
66+
lualibContent = lualibBundle;
67+
} else {
68+
throw new Error("Could not load lualib bundle from ./dist/lualib/lualib_bundle.lua");
69+
}
6170
}
6271

6372
let outPath = path.resolve(rootDir, "lualib_bundle.lua");

src/LuaLib.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as fs from "fs";
21
import * as path from "path";
2+
import { EmitHost } from "./Transpile";
33

44
export enum LuaLibFeature {
55
ArrayConcat = "ArrayConcat",
@@ -73,7 +73,7 @@ const luaLibDependencies: { [lib in LuaLibFeature]?: LuaLibFeature[] } = {
7373
};
7474

7575
export class LuaLib {
76-
public static loadFeatures(features: Iterable<LuaLibFeature>): string {
76+
public static loadFeatures(features: Iterable<LuaLibFeature>, emitHost: EmitHost): string {
7777
let result = "";
7878

7979
const loadedFeatures = new Set<LuaLibFeature>();
@@ -86,7 +86,12 @@ export class LuaLib {
8686
dependencies.forEach(load);
8787
}
8888
const featureFile = path.resolve(__dirname, `../dist/lualib/${feature}.lua`);
89-
result += fs.readFileSync(featureFile).toString() + "\n";
89+
const luaLibFeature = emitHost.readFile(featureFile);
90+
if (luaLibFeature !== undefined) {
91+
result += luaLibFeature.toString() + "\n";
92+
} else {
93+
throw new Error(`Could not read lualib feature ../dist/lualib/${feature}.lua`);
94+
}
9095
}
9196
}
9297

src/LuaPrinter.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as tstl from "./LuaAST";
55
import { luaKeywords } from "./LuaKeywords";
66
import { LuaLib, LuaLibFeature } from "./LuaLib";
77
import * as tsHelper from "./TSHelper";
8+
import { EmitHost } from "./Transpile";
89

910
type SourceChunk = string | SourceNode;
1011

@@ -38,12 +39,15 @@ export class LuaPrinter {
3839
};
3940

4041
private options: CompilerOptions;
42+
private emitHost: EmitHost;
43+
4144
private currentIndent: string;
4245

4346
private sourceFile = "";
4447

45-
public constructor(options: CompilerOptions) {
48+
public constructor(options: CompilerOptions, emitHost: EmitHost) {
4649
this.options = options;
50+
this.emitHost = emitHost;
4751
this.currentIndent = "";
4852
}
4953

@@ -128,7 +132,7 @@ export class LuaPrinter {
128132
// Inline lualib features
129133
else if (luaLibImport === LuaLibImportKind.Inline && luaLibFeatures.size > 0) {
130134
header += "-- Lua Library inline imports\n";
131-
header += LuaLib.loadFeatures(luaLibFeatures);
135+
header += LuaLib.loadFeatures(luaLibFeatures, this.emitHost);
132136
}
133137
}
134138

src/Transpile.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ export interface TranspileOptions {
2727
customTransformers?: ts.CustomTransformers;
2828
transformer?: LuaTransformer;
2929
printer?: LuaPrinter;
30+
emitHost?: EmitHost;
31+
}
32+
33+
export interface EmitHost {
34+
readFile(path: string): string | undefined;
3035
}
3136

3237
export function transpile({
3338
program,
3439
sourceFiles: targetSourceFiles,
3540
customTransformers = {},
41+
emitHost = ts.sys,
3642
transformer = new LuaTransformer(program),
37-
printer = new LuaPrinter(program.getCompilerOptions()),
43+
printer = new LuaPrinter(program.getCompilerOptions(), emitHost),
3844
}: TranspileOptions): TranspileResult {
3945
const options = program.getCompilerOptions() as CompilerOptions;
4046

0 commit comments

Comments
 (0)
X Tutup