1- import * as path from "path" ;
21import * as ts from "typescript" ;
3- import { CompilerOptions , LuaLibImportKind , LuaTarget } from "./CompilerOptions" ;
4- import * as diagnosticFactories from "./diagnostics" ;
2+ import { CompilerOptions , LuaLibImportKind , LuaTarget } from ".. /CompilerOptions" ;
3+ import * as cliDiagnostics from "./diagnostics" ;
54
65export interface ParsedCommandLine extends ts . ParsedCommandLine {
76 options : CompilerOptions ;
@@ -24,7 +23,7 @@ interface CommandLineOptionOfBoolean extends CommandLineOptionBase {
2423
2524type CommandLineOption = CommandLineOptionOfEnum | CommandLineOptionOfBoolean ;
2625
27- const optionDeclarations : CommandLineOption [ ] = [
26+ export const optionDeclarations : CommandLineOption [ ] = [
2827 {
2928 name : "luaLibImport" ,
3029 description : "Specifies how js standard features missing in lua are imported." ,
@@ -60,36 +59,6 @@ const optionDeclarations: CommandLineOption[] = [
6059 } ,
6160] ;
6261
63- export const version = `Version ${ require ( "../package.json" ) . version } ` ;
64-
65- const helpString = `
66- Syntax: tstl [options] [files...]
67-
68- Examples: tstl path/to/file.ts [...]
69- tstl -p path/to/tsconfig.json
70-
71- In addition to the options listed below you can also pass options
72- for the typescript compiler (For a list of options use tsc -h).
73- Some tsc options might have no effect.
74- ` . trim ( ) ;
75-
76- export function getHelpString ( ) : string {
77- let result = helpString + "\n\n" ;
78-
79- result += "Options:\n" ;
80- for ( const option of optionDeclarations ) {
81- const aliasStrings = ( option . aliases || [ ] ) . map ( a => "-" + a ) ;
82- const optionString = aliasStrings . concat ( [ "--" + option . name ] ) . join ( "|" ) ;
83-
84- const valuesHint = option . type === "enum" ? option . choices . join ( "|" ) : option . type ;
85- const spacing = " " . repeat ( Math . max ( 1 , 45 - optionString . length - valuesHint . length ) ) ;
86-
87- result += `\n ${ optionString } <${ valuesHint } >${ spacing } ${ option . description } \n` ;
88- }
89-
90- return result ;
91- }
92-
9362export function updateParsedConfigFile ( parsedConfigFile : ts . ParsedCommandLine ) : ParsedCommandLine {
9463 let hasRootLevelOptions = false ;
9564 for ( const key in parsedConfigFile . raw ) {
@@ -103,15 +72,13 @@ export function updateParsedConfigFile(parsedConfigFile: ts.ParsedCommandLine):
10372
10473 if ( parsedConfigFile . raw . tstl ) {
10574 if ( hasRootLevelOptions ) {
106- parsedConfigFile . errors . push (
107- diagnosticFactories . tstlOptionsAreMovingToTheTstlObject ( parsedConfigFile . raw . tstl )
108- ) ;
75+ parsedConfigFile . errors . push ( cliDiagnostics . tstlOptionsAreMovingToTheTstlObject ( parsedConfigFile . raw . tstl ) ) ;
10976 }
11077
11178 for ( const key in parsedConfigFile . raw . tstl ) {
11279 const option = optionDeclarations . find ( option => option . name === key ) ;
11380 if ( ! option ) {
114- parsedConfigFile . errors . push ( diagnosticFactories . unknownCompilerOption ( key ) ) ;
81+ parsedConfigFile . errors . push ( cliDiagnostics . unknownCompilerOption ( key ) ) ;
11582 continue ;
11683 }
11784
@@ -179,7 +146,7 @@ function readCommandLineArgument(option: CommandLineOption, value: any): Command
179146
180147 if ( value === undefined ) {
181148 return {
182- error : diagnosticFactories . compilerOptionExpectsAnArgument ( option . name ) ,
149+ error : cliDiagnostics . compilerOptionExpectsAnArgument ( option . name ) ,
183150 value : undefined ,
184151 increment : 0 ,
185152 } ;
@@ -201,7 +168,7 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
201168 if ( typeof value !== "boolean" ) {
202169 return {
203170 value : undefined ,
204- error : diagnosticFactories . compilerOptionRequiresAValueOfType ( option . name , "boolean" ) ,
171+ error : cliDiagnostics . compilerOptionRequiresAValueOfType ( option . name , "boolean" ) ,
205172 } ;
206173 }
207174
@@ -212,7 +179,7 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
212179 if ( typeof value !== "string" ) {
213180 return {
214181 value : undefined ,
215- error : diagnosticFactories . compilerOptionRequiresAValueOfType ( option . name , "string" ) ,
182+ error : cliDiagnostics . compilerOptionRequiresAValueOfType ( option . name , "string" ) ,
216183 } ;
217184 }
218185
@@ -221,38 +188,11 @@ function readValue(option: CommandLineOption, value: unknown): ReadValueResult {
221188 const optionChoices = option . choices . join ( ", " ) ;
222189 return {
223190 value : undefined ,
224- error : diagnosticFactories . argumentForOptionMustBe ( `--${ option . name } ` , optionChoices ) ,
191+ error : cliDiagnostics . argumentForOptionMustBe ( `--${ option . name } ` , optionChoices ) ,
225192 } ;
226193 }
227194
228195 return { value : enumValue } ;
229196 }
230197 }
231198}
232-
233- export function parseConfigFileWithSystem (
234- configFileName : string ,
235- commandLineOptions ?: CompilerOptions ,
236- system = ts . sys
237- ) : ParsedCommandLine {
238- const parsedConfigFile = ts . parseJsonSourceFileConfigFileContent (
239- ts . readJsonConfigFile ( configFileName , system . readFile ) ,
240- system ,
241- path . dirname ( configFileName ) ,
242- commandLineOptions ,
243- configFileName
244- ) ;
245-
246- return updateParsedConfigFile ( parsedConfigFile ) ;
247- }
248-
249- export function createDiagnosticReporter ( pretty : boolean , system = ts . sys ) : ts . DiagnosticReporter {
250- const reporter : ts . DiagnosticReporter = ( ts as any ) . createDiagnosticReporter ( system , pretty ) ;
251- return diagnostic => {
252- if ( diagnostic . source === "typescript-to-lua" ) {
253- diagnostic = { ...diagnostic , code : ( "TL" + diagnostic . code ) as any } ;
254- }
255-
256- reporter ( diagnostic ) ;
257- } ;
258- }
0 commit comments