|
1 | 1 | import * as path from "path"; |
2 | 2 | import * as ts from "typescript"; |
3 | | -import { CompilerOptions } from "../CompilerOptions"; |
| 3 | +import { CompilerOptions, TypeScriptToLuaOptions } from "../CompilerOptions"; |
4 | 4 | import { normalizeSlashes } from "../utils"; |
5 | 5 | import * as cliDiagnostics from "./diagnostics"; |
6 | 6 | import { ParsedCommandLine, updateParsedConfigFile } from "./parse"; |
@@ -41,17 +41,73 @@ export function parseConfigFileWithSystem( |
41 | 41 | commandLineOptions?: CompilerOptions, |
42 | 42 | system = ts.sys |
43 | 43 | ): ParsedCommandLine { |
| 44 | + const configRootDir = path.dirname(configFileName); |
44 | 45 | const parsedConfigFile = ts.parseJsonSourceFileConfigFileContent( |
45 | 46 | ts.readJsonConfigFile(configFileName, system.readFile), |
46 | 47 | system, |
47 | | - path.dirname(configFileName), |
| 48 | + configRootDir, |
48 | 49 | commandLineOptions, |
49 | 50 | configFileName |
50 | 51 | ); |
51 | 52 |
|
| 53 | + const cycleCache = new Set<string>(); |
| 54 | + const extendedTstlOptions = getExtendedTstlOptions(configFileName, configRootDir, cycleCache, system); |
| 55 | + |
| 56 | + parsedConfigFile.raw.tstl = Object.assign(extendedTstlOptions, parsedConfigFile.raw.tstl ?? {}); |
| 57 | + |
52 | 58 | return updateParsedConfigFile(parsedConfigFile); |
53 | 59 | } |
54 | 60 |
|
| 61 | +function getExtendedTstlOptions( |
| 62 | + configFilePath: string, |
| 63 | + configRootDir: string, |
| 64 | + cycleCache: Set<string>, |
| 65 | + system: ts.System |
| 66 | +): TypeScriptToLuaOptions { |
| 67 | + const absolutePath = path.isAbsolute(configFilePath) ? configFilePath : path.resolve(configRootDir, configFilePath); |
| 68 | + const newConfigRoot = path.dirname(absolutePath); |
| 69 | + |
| 70 | + if (cycleCache.has(absolutePath)) { |
| 71 | + return {}; |
| 72 | + } |
| 73 | + |
| 74 | + cycleCache.add(absolutePath); |
| 75 | + const fileContent = system.readFile(absolutePath); |
| 76 | + const options = {}; |
| 77 | + |
| 78 | + if (fileContent) { |
| 79 | + const { config: parsedConfig } = ts.parseConfigFileTextToJson(configFilePath, fileContent) as { |
| 80 | + config?: { |
| 81 | + extends?: string | string[]; |
| 82 | + tstl?: TypeScriptToLuaOptions; |
| 83 | + }; |
| 84 | + }; |
| 85 | + |
| 86 | + if (!parsedConfig) { |
| 87 | + return {}; |
| 88 | + } |
| 89 | + |
| 90 | + if (parsedConfig.extends) { |
| 91 | + if (Array.isArray(parsedConfig.extends)) { |
| 92 | + for (const extendedConfigFile of parsedConfig.extends) { |
| 93 | + Object.assign( |
| 94 | + options, |
| 95 | + getExtendedTstlOptions(extendedConfigFile, newConfigRoot, cycleCache, system) |
| 96 | + ); |
| 97 | + } |
| 98 | + } else { |
| 99 | + Object.assign(options, getExtendedTstlOptions(parsedConfig.extends, newConfigRoot, cycleCache, system)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (parsedConfig.tstl) { |
| 104 | + Object.assign(options, parsedConfig.tstl); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return options; |
| 109 | +} |
| 110 | + |
55 | 111 | export function createConfigFileUpdater( |
56 | 112 | optionsToExtend: CompilerOptions |
57 | 113 | ): (options: ts.CompilerOptions) => ts.Diagnostic[] { |
|
0 commit comments