-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathutils.ts
More file actions
80 lines (68 loc) · 2.63 KB
/
utils.ts
File metadata and controls
80 lines (68 loc) · 2.63 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
import * as path from "path";
import * as resolve from "resolve";
import { SourceNode } from "source-map";
import * as ts from "typescript";
// TODO: Don't depend on CLI?
import * as cliDiagnostics from "../cli/diagnostics";
import * as lua from "../LuaAST";
import * as diagnosticFactories from "./diagnostics";
export interface EmitHost {
directoryExists(path: string): boolean;
fileExists(path: string): boolean;
getCurrentDirectory(): string;
readFile(path: string): string | undefined;
writeFile: ts.WriteFileCallback;
}
interface BaseFile {
code: string;
sourceMap?: string;
sourceFiles?: ts.SourceFile[];
}
export interface ProcessedFile extends BaseFile {
fileName: string;
luaAst?: lua.File;
sourceMapNode?: SourceNode;
}
export interface EmitFile extends BaseFile {
outputPath: string;
}
export const getConfigDirectory = (options: ts.CompilerOptions) =>
options.configFilePath ? path.dirname(options.configFilePath) : process.cwd();
const getTstlDirectory = () => path.dirname(__dirname);
export function resolvePlugin(
kind: string,
optionName: string,
basedir: string,
query: unknown,
importName = "default"
): { error?: ts.Diagnostic; result?: unknown } {
if (typeof query !== "string") {
return { error: cliDiagnostics.compilerOptionRequiresAValueOfType(optionName, "string") };
}
const isModuleNotFoundError = (error: any) => error.code === "MODULE_NOT_FOUND";
let resolved: string;
try {
resolved = resolve.sync(query, { basedir, extensions: [".js", ".ts", ".tsx"] });
} catch (err) {
if (!isModuleNotFoundError(err)) throw err;
return { error: diagnosticFactories.couldNotResolveFrom(kind, query, basedir) };
}
const hasNoRequireHook = require.extensions[".ts"] === undefined;
if (hasNoRequireHook && (resolved.endsWith(".ts") || resolved.endsWith(".tsx"))) {
try {
const tsNodePath = resolve.sync("ts-node", { basedir: getTstlDirectory() });
const tsNode: typeof import("ts-node") = require(tsNodePath);
tsNode.register({ transpileOnly: true });
} catch (err) {
if (!isModuleNotFoundError(err)) throw err;
return { error: diagnosticFactories.toLoadItShouldBeTranspiled(kind, query) };
}
}
const commonjsModule = require(resolved);
const factoryModule = commonjsModule.__esModule ? commonjsModule : { default: commonjsModule };
const result = factoryModule[importName];
if (result === undefined) {
return { error: diagnosticFactories.shouldHaveAExport(kind, query, importName) };
}
return { result };
}