Skip to content

Commit

Permalink
Return an results from emitTranspiledFiles instead of a using a callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ark120202 committed Apr 6, 2019
1 parent 8b8eaca commit 1dea9b7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion build_lualib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const options: tstl.CompilerOptions = {

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

const bundlePath = path.join(__dirname, "./dist/lualib/lualib_bundle.lua");
if (fs.existsSync(bundlePath)) {
Expand Down
24 changes: 15 additions & 9 deletions src/Emit.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
import { TranspiledFile } from "./Transpile";

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

export interface OutputFile {
name: string;
text: string;
}

let lualibContent: string;
export function emitTranspiledFiles(
options: CompilerOptions,
transpiledFiles: Map<string, TranspiledFile>,
writeFile = ts.sys.writeFile
): void {
transpiledFiles: Map<string, TranspiledFile>
): OutputFile[] {
const { rootDir, outDir, outFile, luaLibImport } = options;

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

if (lua !== undefined) {
writeFile(outPath, lua);
files.push({ name: outPath, text: lua });
}

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

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

if (declarationMap !== undefined) {
writeFile(trimExt(outPath) + ".d.ts.map", declarationMap);
files.push({ name: trimExt(outPath) + ".d.ts.map", text: declarationMap });
}
}

Expand All @@ -60,6 +64,8 @@ export function emitTranspiledFiles(
}

const outPath = path.join(outDir, "lualib_bundle.lua");
writeFile(outPath, lualibContent);
files.push({ name: outPath, text: lualibContent });
}

return files;
}
9 changes: 7 additions & 2 deletions src/tstl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
import * as fs from "fs";
import * as ts from "typescript";
import * as tstl from ".";
import * as CommandLineParser from "./CommandLineParser";
Expand Down Expand Up @@ -81,7 +82,9 @@ function executeCommandLine(argv: string[]): void {
commandLine.result.fileNames,
commandLine.result.options
);
tstl.emitTranspiledFiles(commandLine.result.options, transpiledFiles);

const emitResult = tstl.emitTranspiledFiles(commandLine.result.options, transpiledFiles);
emitResult.forEach(({ name, text }) => fs.writeFileSync(name, text));

diagnostics.forEach(reportDiagnostic);
if (diagnostics.filter(d => d.category === ts.DiagnosticCategory.Error).length === 0) {
Expand Down Expand Up @@ -125,7 +128,9 @@ function updateWatchCompilerHost(
options,
sourceFiles,
});
tstl.emitTranspiledFiles(options, transpiledFiles);

const emitResult = tstl.emitTranspiledFiles(options, transpiledFiles);
emitResult.forEach(({ name, text }) => fs.writeFileSync(name, text));

const diagnostics = ts.sortAndDeduplicateDiagnostics([
...preEmitDiagnostics,
Expand Down

0 comments on commit 1dea9b7

Please sign in to comment.