/
ts-helpers.ts
executable file
·106 lines (89 loc) · 3.4 KB
/
ts-helpers.ts
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import ts, { GetCanonicalFileName, SourceFile } from "typescript";
import path from "path";
import { VisitorContext } from "../types";
import type { REGISTER_INSTANCE } from "ts-node";
/* ****************************************************************************************************************** */
// region: TS Helpers
/* ****************************************************************************************************************** */
/**
* Determine output file path for source file
*/
export function getOutputDirForSourceFile(context: VisitorContext, sourceFile: SourceFile): string {
const {
tsInstance,
emitHost,
outputFileNamesCache,
compilerOptions,
tsInstance: { getOwnEmitOutputFilePath, getOutputExtension },
} = context;
if (outputFileNamesCache.has(sourceFile)) return outputFileNamesCache.get(sourceFile)!;
// Note: In project references, resolved path is different from path. In that case, our output path is already
// determined in resolvedPath
const outputPath =
sourceFile.path && sourceFile.resolvedPath && sourceFile.path !== sourceFile.resolvedPath
? sourceFile.resolvedPath
: getOwnEmitOutputFilePath(
sourceFile.fileName,
emitHost,
getOutputExtension(sourceFile.fileName, compilerOptions)
);
if (!outputPath)
throw new Error(
`Could not resolve output path for ${sourceFile.fileName}. Please report a GH issue at: ` +
`https://github.com/LeDDGroup/typescript-transform-paths/issues`
);
const res = path.dirname(outputPath);
outputFileNamesCache.set(sourceFile, res);
return tsInstance.normalizePath(res);
}
/**
* Determine if moduleName matches config in paths
*/
export function isModulePathsMatch(context: VisitorContext, moduleName: string): boolean {
const {
pathsPatterns,
tsInstance: { matchPatternOrExact },
} = context;
return !!(pathsPatterns && matchPatternOrExact(pathsPatterns as readonly string[], moduleName));
}
/**
* Create barebones EmitHost (for no-Program transform)
*/
export function createSyntheticEmitHost(
compilerOptions: ts.CompilerOptions,
tsInstance: typeof ts,
getCanonicalFileName: GetCanonicalFileName,
fileNames: string[]
) {
return {
getCompilerOptions: () => compilerOptions,
getCurrentDirectory: tsInstance.sys.getCurrentDirectory,
getCommonSourceDirectory: () =>
tsInstance.getCommonSourceDirectoryOfConfig(
{ options: compilerOptions, fileNames: fileNames } as ts.ParsedCommandLine,
!tsInstance.sys.useCaseSensitiveFileNames
),
getCanonicalFileName,
} as unknown as ts.EmitHost;
}
/**
* Get ts-node register info
*/
export function getTsNodeRegistrationProperties(tsInstance: typeof ts) {
let tsNodeSymbol: typeof REGISTER_INSTANCE;
try {
tsNodeSymbol = require("ts-node")?.["REGISTER_INSTANCE"];
} catch {
return undefined;
}
if (!global.process[tsNodeSymbol]) return undefined;
const { config, options } = global.process[tsNodeSymbol]!;
const { configFilePath } = config.options;
const pcl = configFilePath
? tsInstance.getParsedCommandLineOfConfigFile(configFilePath, {}, <any>tsInstance.sys)
: void 0;
const fileNames = pcl?.fileNames || config.fileNames;
const compilerOptions = Object.assign({}, config.options, options.compilerOptions, { outDir: pcl?.options.outDir });
return { compilerOptions, fileNames, tsNodeOptions: options };
}
// endregion