33import * as ts from "typescript" ;
44import * as fs from "fs" ;
55import * as path from "path" ;
6- import * as yargs from 'yargs'
7-
8- // ES6 syntax broken
9- import dedent = require ( "dedent" )
106
117import { LuaTranspiler , TranspileError } from "./Transpiler" ;
128import { TSHelper as tsEx } from "./TSHelper" ;
13-
14- interface CompilerOptions extends ts . CompilerOptions {
15- addHeader ? : boolean ;
16- luaTarget ? : string ;
17- }
9+ import { CompilerOptions , parseCommandLine } from "./CommandLineParser" ;
1810
1911function compile ( fileNames : string [ ] , options : CompilerOptions ) : void {
2012 let program = ts . createProgram ( fileNames , options ) ;
@@ -39,14 +31,6 @@ function compile(fileNames: string[], options: CompilerOptions): void {
3931 process . exit ( 1 ) ;
4032 }
4133
42- if ( ! options . rootDir ) {
43- options . rootDir = process . cwd ( ) ;
44- }
45-
46- if ( ! options . outDir ) {
47- options . outDir = options . rootDir ;
48- }
49-
5034 program . getSourceFiles ( ) . forEach ( sourceFile => {
5135 if ( ! sourceFile . isDeclarationFile ) {
5236 try {
@@ -83,146 +67,13 @@ function compile(fileNames: string[], options: CompilerOptions): void {
8367 } ) ;
8468
8569 // Copy lualib to target dir
86- // This isnt run in sync because copyFileSync wont report errors.
87- fs . copyFile ( path . resolve ( __dirname , "../dist/lualib/typescript.lua" ) , path . join ( options . outDir , "typescript_lualib.lua" ) , ( err : NodeJS . ErrnoException ) => {
88- if ( err ) {
89- console . log ( "ERROR: copying lualib to output." ) ;
90- process . exit ( 1 ) ;
91- }
92- else {
93- process . exit ( 0 ) ;
94- }
95- } ) ;
96- }
97-
98- function printAST ( node : ts . Node , indent : number ) {
99- let indentStr = "" ;
100- for ( let i = 0 ; i < indent ; i ++ ) indentStr += " " ;
101-
102- console . log ( indentStr + tsEx . enumName ( node . kind , ts . SyntaxKind ) ) ;
103- node . forEachChild ( child => printAST ( child , indent + 1 ) ) ;
104- }
105-
106- // Polyfill for report diagnostics
107- function logError ( commandLine : ts . ParsedCommandLine , tstlOptionKeys : ReadonlyArray < string > ) {
108- const tsInvalidCompilerOptionErrorCode = 5023 ;
109- let ignoredErrorCount = 0 ;
110-
111- if ( commandLine . errors . length !== 0 ) {
112- commandLine . errors . forEach ( ( err ) => {
113- // Ignore errors caused by tstl specific compiler options
114- if ( err . code == tsInvalidCompilerOptionErrorCode ) {
115- for ( const key of tstlOptionKeys ) {
116- if ( err . messageText . toString ( ) . indexOf ( key ) != - 1 ) {
117- ignoredErrorCount += 1 ;
118- }
119- }
120- }
121- else {
122- console . log ( err . messageText ) ;
123- }
124- } ) ;
125- if ( commandLine . errors . length > ignoredErrorCount ) {
126- process . exit ( 1 ) ;
127- }
128- }
70+ fs . copyFileSync ( path . resolve ( __dirname , "../dist/lualib/typescript.lua" ) , path . join ( options . outDir , "typescript_lualib.lua" ) ) ;
12971}
13072
131- function executeCommandLine ( args : ReadonlyArray < string > ) {
132- const tstlOptions : { [ key : string ] : yargs . Options } = {
133- 'lt' : {
134- alias : 'luaTarget' ,
135- choices : [ 'JIT' , '5.1' , '5.2' , '5.3' ] ,
136- default : 'JIT' ,
137- describe : 'Specify Lua target version.' ,
138- type : 'string'
139- } ,
140- 'ah' : {
141- alias : 'addHeader' ,
142- describe : 'Specify if a header will be added to compiled files.' ,
143- default : true ,
144- type : 'boolean'
145- }
146- } ;
147-
148- const tstlOptionKeys = [ ] ;
149- for ( let key in tstlOptions ) {
150- let optionName = key ;
151- if ( tstlOptions [ key ] . alias ) {
152- optionName = tstlOptions [ key ] . alias as string ;
153- }
154-
155- tstlOptionKeys . push ( optionName ) ;
156- }
157-
158- const argv = yargs
159- . usage ( dedent ( `tstl [options] [files...]
160-
161- In addition to the options listed below you can also pass options for the typescript compiler (For a list of options use tsc -h).
162-
163- NOTES:
164- - The tsc options might have no effect.
165- - Options in tsconfig.json are prioritized.` ) )
166- . example ( 'tstl path/to/file.ts [...]' , 'Compile files' )
167- . example ( 'tstl -p path/to/tsconfig.json' , 'Compile project' )
168- . options ( tstlOptions )
169- . argv ;
170-
171- let commandLine = ts . parseCommandLine ( args ) ;
172-
173- logError ( commandLine , tstlOptionKeys ) ;
174-
175- // Add tstl CLI options
176- for ( const key of tstlOptionKeys ) {
177- commandLine . options [ key ] = argv [ key ] ;
178- }
179-
180- let configPath ;
181- if ( commandLine . options . project ) {
182- configPath = path . isAbsolute ( commandLine . options . project ) ? commandLine . options . project : path . join ( process . cwd ( ) , commandLine . options . project ) ;
183- if ( fs . statSync ( configPath ) . isDirectory ( ) ) {
184- configPath = path . join ( configPath , 'tsconfig.json' ) ;
185- } else if ( fs . statSync ( configPath ) . isFile ( ) && path . extname ( configPath ) === ".ts" ) {
186- // Search for tsconfig upwards in directory hierarchy starting from the file path
187- let dir = path . dirname ( configPath ) . split ( path . sep ) ;
188- let found = false ;
189- for ( let i = dir . length ; i > 0 ; i -- ) {
190- const searchPath = dir . slice ( 0 , i ) . join ( "/" ) + path . sep + "tsconfig.json" ;
191-
192- // If tsconfig.json was found, stop searching
193- if ( ts . sys . fileExists ( searchPath ) ) {
194- configPath = searchPath ;
195- found = true ;
196- break ;
197- }
198- }
199-
200- if ( ! found ) {
201- console . error ( "Tried to build project but could not find tsconfig.json!" ) ;
202- process . exit ( 1 ) ;
203- }
204- }
205- commandLine . options . project = configPath ;
206- let configContents = fs . readFileSync ( configPath ) . toString ( ) ;
207- const configJson = ts . parseConfigFileTextToJson ( configPath , configContents ) ;
208- commandLine = ts . parseJsonConfigFileContent ( configJson . config , ts . sys , path . dirname ( configPath ) , commandLine . options ) ;
209-
210- // Add compiler options that are ignored by TS parsers
211- // Options supplied in tsconfig are prioritized to allow for CLI defaults
212- for ( const compilerOption in commandLine . raw . compilerOptions ) {
213- if ( tstlOptionKeys . indexOf ( compilerOption ) != - 1 ) {
214- commandLine . options [ compilerOption ] = commandLine . raw . compilerOptions [ compilerOption ] ;
215- }
216- }
217- }
218-
219- if ( configPath && ! commandLine . options . rootDir ) {
220- commandLine . options . rootDir = path . dirname ( configPath ) ;
221- }
222-
223- logError ( commandLine , tstlOptionKeys ) ;
224-
225- compile ( commandLine . fileNames , commandLine . options ) ;
73+ export function execCommandLine ( argv ?: string [ ] ) {
74+ argv = argv ? argv : process . argv . slice ( 2 ) ;
75+ let commandLine = parseCommandLine ( argv ) ;
76+ compile ( commandLine . fileNames , commandLine . options )
22677}
22778
228- executeCommandLine ( process . argv . slice ( 2 ) ) ;
79+ execCommandLine ( ) ;
0 commit comments