forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
88 lines (76 loc) · 3.15 KB
/
Copy pathutils.ts
File metadata and controls
88 lines (76 loc) · 3.15 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
82
83
84
85
86
87
88
import * as fs from "fs";
import * as path from "path";
import * as ts from "typescript";
import { CompilerOptions } from "../CompilerOptions";
import { intersection, union } from "../utils";
const libCache = new Map<string, ts.SourceFile>();
export function createVirtualProgram(input: Record<string, string>, options: CompilerOptions = {}): ts.Program {
function notImplemented(): never {
throw new Error("Not implemented");
}
const getFileFromInput = (fileName: string) =>
input[fileName] ?? (fileName.startsWith("/") ? input[fileName.slice(1)] : undefined);
const compilerHost: ts.CompilerHost = {
useCaseSensitiveFileNames: () => false,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "/",
fileExists: fileName => fileName.startsWith("lib.") || getFileFromInput(fileName) !== undefined,
readFile: notImplemented,
writeFile: notImplemented,
getDefaultLibFileName: ts.getDefaultLibFileName,
getNewLine: () => "\n",
getSourceFile(fileName) {
const fileFromInput = getFileFromInput(fileName);
if (fileFromInput !== undefined) {
return ts.createSourceFile(fileName, fileFromInput, ts.ScriptTarget.Latest, false);
}
if (libCache.has(fileName)) return libCache.get(fileName)!;
if (fileName.startsWith("lib.")) {
const typeScriptDir = path.dirname(require.resolve("typescript"));
const filePath = path.join(typeScriptDir, fileName);
const content = fs.readFileSync(filePath, "utf8");
const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, false);
libCache.set(fileName, sourceFile);
return sourceFile;
}
},
};
return ts.createProgram(Object.keys(input), options, compilerHost);
}
export interface TranspiledFile {
sourceFiles: ts.SourceFile[];
lua?: string;
luaSourceMap?: string;
declaration?: string;
declarationMap?: string;
/** @internal */
js?: string;
/** @internal */
jsSourceMap?: string;
}
export function createEmitOutputCollector() {
const files: TranspiledFile[] = [];
const writeFile: ts.WriteFileCallback = (fileName, data, _bom, _onError, sourceFiles = []) => {
let file = files.find(f => intersection(f.sourceFiles, sourceFiles).length > 0);
if (!file) {
file = { sourceFiles: [...sourceFiles] };
files.push(file);
} else {
file.sourceFiles = union(file.sourceFiles, sourceFiles);
}
if (fileName.endsWith(".lua")) {
file.lua = data;
} else if (fileName.endsWith(".lua.map")) {
file.luaSourceMap = data;
} else if (fileName.endsWith(".js")) {
file.js = data;
} else if (fileName.endsWith(".js.map")) {
file.jsSourceMap = data;
} else if (fileName.endsWith(".d.ts")) {
file.declaration = data;
} else if (fileName.endsWith(".d.ts.map")) {
file.declarationMap = data;
}
};
return { writeFile, files };
}