Skip to content

Commit 1dea9b7

Browse files
committed
Return an results from emitTranspiledFiles instead of a using a callback
1 parent 8b8eaca commit 1dea9b7

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

build_lualib.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const options: tstl.CompilerOptions = {
1616

1717
// TODO: Check diagnostics
1818
const { transpiledFiles } = tstl.transpileFiles(glob.sync("./src/lualib/**/*.ts"), options);
19-
tstl.emitTranspiledFiles(options, transpiledFiles);
19+
const emitResult = tstl.emitTranspiledFiles(options, transpiledFiles);
20+
emitResult.forEach(({ name, text }) => fs.writeFileSync(name, text));
2021

2122
const bundlePath = path.join(__dirname, "./dist/lualib/lualib_bundle.lua");
2223
if (fs.existsSync(bundlePath)) {

src/Emit.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import * as fs from "fs";
22
import * as path from "path";
3-
import * as ts from "typescript";
43
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
54
import { TranspiledFile } from "./Transpile";
65

76
const trimExt = (filePath: string) =>
87
path.join(path.dirname(filePath), path.basename(filePath, path.extname(filePath)));
98

9+
export interface OutputFile {
10+
name: string;
11+
text: string;
12+
}
13+
1014
let lualibContent: string;
1115
export function emitTranspiledFiles(
1216
options: CompilerOptions,
13-
transpiledFiles: Map<string, TranspiledFile>,
14-
writeFile = ts.sys.writeFile
15-
): void {
17+
transpiledFiles: Map<string, TranspiledFile>
18+
): OutputFile[] {
1619
const { rootDir, outDir, outFile, luaLibImport } = options;
1720

21+
const files: OutputFile[] = [];
1822
for (const [fileName, { lua, sourceMap, declaration, declarationMap }] of transpiledFiles) {
1923
let outPath = fileName;
2024
if (outDir !== rootDir) {
@@ -35,19 +39,19 @@ export function emitTranspiledFiles(
3539
}
3640

3741
if (lua !== undefined) {
38-
writeFile(outPath, lua);
42+
files.push({ name: outPath, text: lua });
3943
}
4044

4145
if (sourceMap !== undefined && options.sourceMap) {
42-
writeFile(outPath + ".map", sourceMap);
46+
files.push({ name: outPath + ".map", text: sourceMap });
4347
}
4448

4549
if (declaration !== undefined) {
46-
writeFile(trimExt(outPath) + ".d.ts", declaration);
50+
files.push({ name: trimExt(outPath) + ".d.ts", text: declaration });
4751
}
4852

4953
if (declarationMap !== undefined) {
50-
writeFile(trimExt(outPath) + ".d.ts.map", declarationMap);
54+
files.push({ name: trimExt(outPath) + ".d.ts.map", text: declarationMap });
5155
}
5256
}
5357

@@ -60,6 +64,8 @@ export function emitTranspiledFiles(
6064
}
6165

6266
const outPath = path.join(outDir, "lualib_bundle.lua");
63-
writeFile(outPath, lualibContent);
67+
files.push({ name: outPath, text: lualibContent });
6468
}
69+
70+
return files;
6571
}

src/tstl.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
import * as fs from "fs";
23
import * as ts from "typescript";
34
import * as tstl from ".";
45
import * as CommandLineParser from "./CommandLineParser";
@@ -81,7 +82,9 @@ function executeCommandLine(argv: string[]): void {
8182
commandLine.result.fileNames,
8283
commandLine.result.options
8384
);
84-
tstl.emitTranspiledFiles(commandLine.result.options, transpiledFiles);
85+
86+
const emitResult = tstl.emitTranspiledFiles(commandLine.result.options, transpiledFiles);
87+
emitResult.forEach(({ name, text }) => fs.writeFileSync(name, text));
8588

8689
diagnostics.forEach(reportDiagnostic);
8790
if (diagnostics.filter(d => d.category === ts.DiagnosticCategory.Error).length === 0) {
@@ -125,7 +128,9 @@ function updateWatchCompilerHost(
125128
options,
126129
sourceFiles,
127130
});
128-
tstl.emitTranspiledFiles(options, transpiledFiles);
131+
132+
const emitResult = tstl.emitTranspiledFiles(options, transpiledFiles);
133+
emitResult.forEach(({ name, text }) => fs.writeFileSync(name, text));
129134

130135
const diagnostics = ts.sortAndDeduplicateDiagnostics([
131136
...preEmitDiagnostics,

0 commit comments

Comments
 (0)