-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathtsconfig.ts
More file actions
148 lines (126 loc) · 5.01 KB
/
tsconfig.ts
File metadata and controls
148 lines (126 loc) · 5.01 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
import * as path from "path";
import * as ts from "typescript";
import { CompilerOptions, TypeScriptToLuaOptions } from "../CompilerOptions";
import { normalizeSlashes } from "../utils";
import * as cliDiagnostics from "./diagnostics";
import { ParsedCommandLine, updateParsedConfigFile } from "./parse";
export function locateConfigFile(commandLine: ParsedCommandLine): ts.Diagnostic | string | undefined {
const { project } = commandLine.options;
if (!project) {
if (commandLine.fileNames.length > 0) {
return undefined;
}
const searchPath = normalizeSlashes(process.cwd());
return ts.findConfigFile(searchPath, ts.sys.fileExists);
}
if (commandLine.fileNames.length !== 0) {
return cliDiagnostics.optionProjectCannotBeMixedWithSourceFilesOnACommandLine();
}
// TODO: Unlike tsc, this resolves `.` to absolute path
const fileOrDirectory = normalizeSlashes(path.resolve(process.cwd(), project));
if (ts.sys.directoryExists(fileOrDirectory)) {
const configFileName = path.posix.join(fileOrDirectory, "tsconfig.json");
if (ts.sys.fileExists(configFileName)) {
return configFileName;
} else {
return cliDiagnostics.cannotFindATsconfigJsonAtTheSpecifiedDirectory(project);
}
} else if (ts.sys.fileExists(fileOrDirectory)) {
return fileOrDirectory;
} else {
return cliDiagnostics.theSpecifiedPathDoesNotExist(project);
}
}
export function parseConfigFileWithSystem(
configFileName: string,
commandLineOptions?: CompilerOptions,
system = ts.sys
): ParsedCommandLine {
const configRootDir = path.dirname(configFileName);
const parsedConfigFile = ts.parseJsonSourceFileConfigFileContent(
ts.readJsonConfigFile(configFileName, system.readFile),
system,
configRootDir,
commandLineOptions,
configFileName
);
const cycleCache = new Set<string>();
const extendedTstlOptions = getExtendedTstlOptions(configFileName, configRootDir, cycleCache, system);
parsedConfigFile.raw.tstl = Object.assign(extendedTstlOptions, parsedConfigFile.raw.tstl ?? {});
return updateParsedConfigFile(parsedConfigFile);
}
function resolveNpmModuleConfig(
moduleName: string,
configRootDir: string,
host: ts.ModuleResolutionHost
): string | undefined {
const resolved = ts.nodeNextJsonConfigResolver(moduleName, path.join(configRootDir, "tsconfig.json"), host);
if (resolved.resolvedModule) {
return resolved.resolvedModule.resolvedFileName;
}
}
function getExtendedTstlOptions(
configFilePath: string,
configRootDir: string,
cycleCache: Set<string>,
system: ts.System
): TypeScriptToLuaOptions {
const absolutePath = ts.pathIsAbsolute(configFilePath)
? configFilePath
: ts.pathIsRelative(configFilePath)
? path.resolve(configRootDir, configFilePath)
: resolveNpmModuleConfig(configFilePath, configRootDir, system); // if a path is neither relative nor absolute, it is probably a npm module
if (!absolutePath) {
return {};
}
const newConfigRoot = path.dirname(absolutePath);
if (cycleCache.has(absolutePath)) {
return {};
}
cycleCache.add(absolutePath);
const fileContent = system.readFile(absolutePath);
const options = {};
if (fileContent) {
const { config: parsedConfig } = ts.parseConfigFileTextToJson(configFilePath, fileContent) as {
config?: {
extends?: string | string[];
tstl?: TypeScriptToLuaOptions;
};
};
if (!parsedConfig) {
return {};
}
if (parsedConfig.extends) {
if (Array.isArray(parsedConfig.extends)) {
for (const extendedConfigFile of parsedConfig.extends) {
Object.assign(
options,
getExtendedTstlOptions(extendedConfigFile, newConfigRoot, cycleCache, system)
);
}
} else {
Object.assign(options, getExtendedTstlOptions(parsedConfig.extends, newConfigRoot, cycleCache, system));
}
}
if (parsedConfig.tstl) {
Object.assign(options, parsedConfig.tstl);
}
}
return options;
}
export function createConfigFileUpdater(
optionsToExtend: CompilerOptions
): (options: ts.CompilerOptions) => ts.Diagnostic[] {
const configFileMap = new WeakMap<ts.TsConfigSourceFile, ts.ParsedCommandLine>();
return options => {
const { configFile, configFilePath } = options;
if (!configFile || !configFilePath) return [];
if (!configFileMap.has(configFile)) {
const parsedConfigFile = parseConfigFileWithSystem(configFilePath, optionsToExtend, ts.sys);
configFileMap.set(configFile, parsedConfigFile);
}
const parsedConfigFile = configFileMap.get(configFile)!;
Object.assign(options, parsedConfigFile.options);
return parsedConfigFile.errors;
};
}