|
1 | 1 | import * as ts from "typescript"; |
2 | 2 | import { BuildMode, CompilerOptions, LuaLibImportKind, LuaTarget } from "../CompilerOptions"; |
3 | 3 | import * as cliDiagnostics from "./diagnostics"; |
| 4 | +import * as fs from "fs"; |
| 5 | +import * as path from "path"; |
4 | 6 |
|
5 | 7 | export interface ParsedCommandLine extends ts.ParsedCommandLine { |
6 | 8 | options: CompilerOptions; |
@@ -111,6 +113,31 @@ export const optionDeclarations: CommandLineOption[] = [ |
111 | 113 | }, |
112 | 114 | ]; |
113 | 115 |
|
| 116 | +function substituteFirmwareLocation(config: ts.ParsedCommandLine) { |
| 117 | + if (config.options.outDir?.includes("${firmwareLocation}")) { |
| 118 | + let substitution = ""; |
| 119 | + const baseDir = process.cwd(); |
| 120 | + const paths = fs.readdirSync(baseDir, { recursive: true }); |
| 121 | + const filtered = paths.filter(dir => dir.toString().endsWith("sharedscripts_default")); |
| 122 | + if (filtered.length === 1) { |
| 123 | + substitution = path.dirname(filtered[0].toString()); |
| 124 | + } |
| 125 | + if (substitution && substitution !== "") { |
| 126 | + config.options.outDir = path.normalize(config.options.outDir?.replace("${firmwareLocation}", substitution)); |
| 127 | + } else { |
| 128 | + config.errors.push({ |
| 129 | + file: undefined, |
| 130 | + start: undefined, |
| 131 | + length: undefined, |
| 132 | + category: ts.DiagnosticCategory.Error, |
| 133 | + code: 42, |
| 134 | + source: "typescript-to-lua", |
| 135 | + messageText: "Cannot find firmware location (sharedscripts_default)" |
| 136 | + }); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
114 | 141 | export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine): ParsedCommandLine { |
115 | 142 | let hasRootLevelOptions = false; |
116 | 143 | for (const [name, rawValue] of Object.entries(parsedConfigFile.raw)) { |
@@ -140,6 +167,8 @@ export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine): |
140 | 167 | } |
141 | 168 | } |
142 | 169 |
|
| 170 | + substituteFirmwareLocation(parsedConfigFile); |
| 171 | + |
143 | 172 | return parsedConfigFile; |
144 | 173 | } |
145 | 174 |
|
@@ -181,6 +210,8 @@ function updateParsedCommandLine(parsedCommandLine: ts.ParsedCommandLine, args: |
181 | 210 | } |
182 | 211 | } |
183 | 212 |
|
| 213 | + substituteFirmwareLocation(parsedCommandLine); |
| 214 | + |
184 | 215 | return parsedCommandLine; |
185 | 216 | } |
186 | 217 |
|
|
0 commit comments