-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathtsConfigLoader.ts
More file actions
153 lines (133 loc) · 3.88 KB
/
tsConfigLoader.ts
File metadata and controls
153 lines (133 loc) · 3.88 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import * as path from "path";
import * as fs from "fs";
import JSON5 from "json5";
import stripBom from "strip-bom";
/**
* Typing for the parts of tsconfig that we care about
*/
export interface TsConfig {
extends?: string;
compilerOptions?: {
baseUrl?: string;
paths?: { [key: string]: Array<string> };
strict?: boolean;
};
}
export interface TsConfigLoaderResult {
tsConfigPath: string | undefined;
baseUrl: string | undefined;
paths: { [key: string]: Array<string> } | undefined;
}
export interface TsConfigLoaderParams {
getEnv: (key: string) => string | undefined;
cwd: string;
loadSync?(
cwd: string,
filename?: string,
baseUrl?: string
): TsConfigLoaderResult;
}
export function tsConfigLoader({
cwd,
}: TsConfigLoaderParams): TsConfigLoaderResult {
let loadResult = loadSync(cwd);
return loadResult;
}
function loadSync(cwd: string): TsConfigLoaderResult {
// Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename
let configPath = resolveConfigPath(cwd);
if (!configPath) {
return {
tsConfigPath: undefined,
baseUrl: undefined,
paths: undefined,
};
}
let config = parseTsConfig(configPath);
return {
tsConfigPath: configPath,
baseUrl: config && config.compilerOptions && config.compilerOptions.baseUrl,
paths: config && config.compilerOptions && config.compilerOptions.paths,
};
}
function resolveConfigPath(cwd: string): string | undefined {
if (fs.statSync(cwd).isFile()) {
return path.resolve(cwd);
}
let configAbsolutePath = walkForTsConfig(cwd);
return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
}
function walkForTsConfig(
directory: string,
existsSync: (path: string) => boolean = fs.existsSync
): string | undefined {
let configPath = path.join(directory, "./tsconfig.json");
if (existsSync(configPath)) {
return configPath;
}
configPath = path.join(directory, "./jsconfig.json");
if (existsSync(configPath)) {
return configPath;
}
let parentDirectory = path.join(directory, "../");
// If we reached the top
if (directory === parentDirectory) {
return undefined;
}
return walkForTsConfig(parentDirectory, existsSync);
}
function parseTsConfig(
configFilePath: string,
existsSync: (path: string) => boolean = fs.existsSync,
readFileSync: (filename: string) => string = (filename: string) =>
fs.readFileSync(filename, "utf8")
): TsConfig | undefined {
if (!existsSync(configFilePath)) {
return undefined;
}
let configString = readFileSync(configFilePath);
let cleanedJson = stripBom(configString);
let config = JSON5.parse<TsConfig>(cleanedJson);
let extendedConfig = config.extends;
if (extendedConfig) {
if (
typeof extendedConfig === "string" &&
extendedConfig.indexOf(".json") === -1
) {
extendedConfig += ".json";
}
let currentDir = path.dirname(configFilePath);
let extendedConfigPath = path.join(currentDir, extendedConfig);
if (
extendedConfig.indexOf("/") !== -1 &&
extendedConfig.indexOf(".") !== -1 &&
!existsSync(extendedConfigPath)
) {
extendedConfigPath = path.join(
currentDir,
"node_modules",
extendedConfig
);
}
let base =
parseTsConfig(extendedConfigPath, existsSync, readFileSync) || {};
// baseUrl should be interpreted as relative to the base tsconfig,
// but we need to update it so it is relative to the original tsconfig being loaded
if (base.compilerOptions && base.compilerOptions.baseUrl) {
let extendsDir = path.dirname(extendedConfig);
base.compilerOptions.baseUrl = path.join(
extendsDir,
base.compilerOptions.baseUrl
);
}
return {
...base,
...config,
compilerOptions: {
...base.compilerOptions,
...config.compilerOptions,
},
};
}
return config;
}