forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmit.ts
More file actions
81 lines (65 loc) · 2.78 KB
/
Emit.ts
File metadata and controls
81 lines (65 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import * as path from "path";
import * as ts from "typescript";
import { CompilerOptions, LuaLibImportKind } from "./CompilerOptions";
import { TranspiledFile, EmitHost } from "./Transpile";
const trimExt = (filePath: string) => filePath.slice(0, -path.extname(filePath).length);
const normalizeSlashes = (filePath: string) => filePath.replace(/\\/g, "/");
export interface OutputFile {
name: string;
text: string;
}
let lualibContent: string;
export function emitTranspiledFiles(
options: CompilerOptions,
transpiledFiles: TranspiledFile[],
emitHost: EmitHost = ts.sys
): OutputFile[] {
let { rootDir, outDir, outFile, luaLibImport } = options;
const configFileName = options.configFilePath as string | undefined;
// TODO: Use getCommonSourceDirectory
const baseDir = configFileName ? path.dirname(configFileName) : process.cwd();
rootDir = rootDir || baseDir;
outDir = outDir ? path.resolve(baseDir, outDir) : rootDir;
const files: OutputFile[] = [];
for (const { fileName, lua, sourceMap, declaration, declarationMap } of transpiledFiles) {
let outPath = fileName;
if (outDir !== rootDir) {
outPath = path.resolve(outDir, path.relative(rootDir, fileName));
}
// change extension or rename to outFile
if (outFile) {
outPath = path.isAbsolute(outFile) ? outFile : path.resolve(baseDir, outFile);
} else {
outPath = trimExt(outPath) + ".lua";
}
outPath = normalizeSlashes(outPath);
if (lua !== undefined) {
files.push({ name: outPath, text: lua });
}
if (sourceMap !== undefined && options.sourceMap) {
files.push({ name: outPath + ".map", text: sourceMap });
}
if (declaration !== undefined) {
files.push({ name: trimExt(outPath) + ".d.ts", text: declaration });
}
if (declarationMap !== undefined) {
files.push({ name: trimExt(outPath) + ".d.ts.map", text: declarationMap });
}
}
if (luaLibImport === LuaLibImportKind.Require || luaLibImport === LuaLibImportKind.Always) {
if (lualibContent === undefined) {
const lualibBundle = emitHost.readFile(path.resolve(__dirname, "../dist/lualib/lualib_bundle.lua"));
if (lualibBundle !== undefined) {
lualibContent = lualibBundle;
} else {
throw new Error("Could not load lualib bundle from ./dist/lualib/lualib_bundle.lua");
}
}
let outPath = path.resolve(rootDir, "lualib_bundle.lua");
if (outDir !== rootDir) {
outPath = path.join(outDir, path.relative(rootDir, outPath));
}
files.push({ name: normalizeSlashes(outPath), text: lualibContent });
}
return files;
}