@@ -157,19 +157,55 @@ function readTsConfig(parsedCommandLine: ts.ParsedCommandLine): CLIParseResult
157157 }
158158
159159 const configPath = options . project ;
160- const configContents = fs . readFileSync ( configPath ) . toString ( ) ;
161- const configJson = ts . parseConfigFileTextToJson ( configPath , configContents ) ;
162- const parsedJsonConfig = ts . parseJsonConfigFileContent (
163- configJson . config ,
164- ts . sys ,
165- path . dirname ( configPath ) ,
166- options
167- ) ;
168-
169- for ( const key in parsedJsonConfig . raw ) {
160+ const parsedJsonConfig = parseTsConfigFile ( configPath , options ) ;
161+
162+ return parsedJsonConfig ;
163+ }
164+ return { isValid : true , result : parsedCommandLine } ;
165+ }
166+
167+ export function parseTsConfigFile ( filePath : string , existingOptions ?: ts . CompilerOptions ) : CLIParseResult {
168+ const configContents = fs . readFileSync ( filePath ) . toString ( ) ;
169+ return parseTsConfigString ( configContents , filePath , existingOptions ) ;
170+ }
171+
172+ export function parseTsConfigString (
173+ tsConfigString : string ,
174+ configPath : string ,
175+ existingOptions ?: ts . CompilerOptions
176+ ) : CLIParseResult {
177+ const configJson = ts . parseConfigFileTextToJson ( configPath , tsConfigString ) ;
178+ const parsedJsonConfig = ts . parseJsonConfigFileContent (
179+ configJson . config ,
180+ ts . sys ,
181+ path . dirname ( configPath ) ,
182+ existingOptions
183+ ) ;
184+
185+ for ( const key in parsedJsonConfig . raw ) {
186+ const option = optionDeclarations [ key ] ;
187+ if ( option !== undefined ) {
188+ const value = readValue ( parsedJsonConfig . raw [ key ] , option . type , key ) ;
189+ if ( option . choices ) {
190+ if ( option . choices . indexOf ( value ) < 0 ) {
191+ return {
192+ isValid : false ,
193+ errorMessage : `Unknown ${ key } value '${ value } '.\nAccepted values: ${ option . choices } ` ,
194+ } ;
195+ }
196+ }
197+ // console.warn(`[Deprectated] TSTL options are moving to the luaConfig object. Adjust your tsconfig to `
198+ // + `look like { "compilerOptions": { <typescript options> }, "tstl": { <tstl options> } }`);
199+ parsedJsonConfig . options [ key ] = value ;
200+ }
201+ }
202+
203+ // Eventually we will only look for the tstl object for tstl options
204+ if ( parsedJsonConfig . raw . tstl ) {
205+ for ( const key in parsedJsonConfig . raw . tstl ) {
170206 const option = optionDeclarations [ key ] ;
171207 if ( option !== undefined ) {
172- const value = readValue ( parsedJsonConfig . raw [ key ] , option . type , key ) ;
208+ const value = readValue ( parsedJsonConfig . raw . tstl [ key ] , option . type , key ) ;
173209 if ( option . choices ) {
174210 if ( option . choices . indexOf ( value ) < 0 ) {
175211 return {
@@ -182,9 +218,9 @@ function readTsConfig(parsedCommandLine: ts.ParsedCommandLine): CLIParseResult
182218 parsedJsonConfig . options [ key ] = value ;
183219 }
184220 }
185- return { isValid : true , result : parsedJsonConfig } ;
186221 }
187- return { isValid : true , result : parsedCommandLine } ;
222+
223+ return { isValid : true , result : parsedJsonConfig } ;
188224}
189225
190226function parseTSTLOptions ( commandLine : ts . ParsedCommandLine , args : string [ ] ) : CLIParseResult {
@@ -270,15 +306,15 @@ function getArgumentValue(
270306 return { isValid : true , result : value } ;
271307}
272308
273- function readValue ( valueString : string , valueType : string , parameterName : string ) : string | boolean {
309+ function readValue ( value : string | boolean , valueType : string , parameterName : string ) : string | boolean {
274310 if ( valueType === "boolean" ) {
275- return valueString === "true" || valueString === "t"
311+ return value === true || value === "true" || value === "t"
276312 ? true
277313 : false ;
278314 } else if ( valueType === "enum" ) {
279- return valueString . toLowerCase ( ) ;
315+ return value . toString ( ) . toLowerCase ( ) ;
280316 } else {
281- return valueString ;
317+ return value ;
282318 }
283319}
284320
0 commit comments