33import * as fs from 'fs' ;
44import * as path from 'path' ;
55import { processDirectory , processFile } from './processor' ;
6- import { LuatsConfig , loadConfig , defaultConfig } from './config' ;
6+ import { LuatsConfig , loadConfig } from './config' ;
7+ import { analyze } from '../index' ;
78
89// CLI command types
910type Command = 'convert' | 'validate' | 'help' ;
@@ -15,6 +16,7 @@ interface CliOptions {
1516 config ? : string ;
1617 watch ? : boolean ;
1718 verbose ? : boolean ;
19+ silent ? : boolean ;
1820}
1921
2022/**
@@ -80,6 +82,8 @@ function parseOptions(args: string[]): CliOptions {
8082 options . watch = true ;
8183 } else if ( arg === '--verbose' || arg === '-v' ) {
8284 options . verbose = true ;
85+ } else if ( arg === '--silent' || arg === '-s' ) {
86+ options . silent = true ;
8387 }
8488 }
8589
@@ -135,11 +139,15 @@ async function convertCommand(options: CliOptions, config: LuatsConfig): Promise
135139 if ( stats . isFile ( ) ) {
136140 // Process a single file
137141 await processFile ( inputPath , outputPath , config ) ;
138- console . log ( `Converted file: ${ inputPath } -> ${ outputPath } ` ) ;
142+ if ( ! options . silent ) {
143+ console . log ( `Converted file: ${ inputPath } -> ${ outputPath } ` ) ;
144+ }
139145 } else if ( stats . isDirectory ( ) ) {
140146 // Process a directory
141147 await processDirectory ( inputPath , outputPath , config ) ;
142- console . log ( `Converted directory: ${ inputPath } -> ${ outputPath } ` ) ;
148+ if ( ! options . silent ) {
149+ console . log ( `Converted directory: ${ inputPath } -> ${ outputPath } ` ) ;
150+ }
143151 } else {
144152 throw new Error ( `Invalid input: ${ inputPath } ` ) ;
145153 }
@@ -156,24 +164,41 @@ async function validateCommand(options: CliOptions, config: LuatsConfig): Promis
156164 const inputPath = path . resolve ( options . input ) ;
157165
158166 if ( ! fs . existsSync ( inputPath ) ) {
159- throw new Error ( `Input file does not exist : ${ inputPath } ` ) ;
167+ throw new Error ( `File not found : ${ inputPath } ` ) ;
160168 }
161169
162- if ( fs . statSync ( inputPath ) . isDirectory ( ) ) {
163- throw new Error ( 'Validation can only be performed on a single file' ) ;
170+ if ( ! fs . statSync ( inputPath ) . isFile ( ) ) {
171+ throw new Error ( `Not a file: ${ inputPath } ` ) ;
164172 }
165173
166- // Read the file
174+ if ( ! options . silent ) {
175+ console . log ( `Validating ${ inputPath } ` ) ;
176+ }
177+
178+ // Read input file
167179 const luaCode = fs . readFileSync ( inputPath , 'utf-8' ) ;
168180
169181 // Determine if it's Luau based on extension
170- const isLuau = inputPath . endsWith ( '.luau' ) ;
182+ const isLuau = path . extname ( inputPath ) === '.luau' ;
171183
172- console . log ( `Validating file: ${ inputPath } ` ) ;
184+ // Analyze the code
185+ const result = analyze ( luaCode , isLuau ) ;
173186
174- // Here you would add the actual validation logic
175- // For now, we'll just return success
176- console . log ( 'Validation successful!' ) ;
187+ if ( result . errors . length === 0 ) {
188+ if ( ! options . silent ) {
189+ console . log ( `✓ ${ inputPath } is valid` ) ;
190+ }
191+ } else {
192+ console . error ( `✗ ${ inputPath } has ${ result . errors . length } error(s)` ) ;
193+
194+ if ( options . verbose || ! options . silent ) {
195+ result . errors . forEach ( ( error : Error , index : number ) => {
196+ console . error ( `Error ${ index + 1 } : ${ error . message } ` ) ;
197+ } ) ;
198+ }
199+
200+ throw new Error ( 'Validation failed' ) ;
201+ }
177202}
178203
179204/**
@@ -187,17 +212,19 @@ Usage:
187212 luats <command> [options]
188213
189214Commands:
190- convert Convert Lua/Luau files to TypeScript
191- validate Validate Lua/Luau files
215+ convert Convert a Lua/Luau file or directory to TypeScript
216+ validate Validate a Lua/Luau file
192217
193218Options:
194219 --input, -i Input file or directory
195220 --output, -o Output file or directory
196221 --config, -c Config file path
197222 --watch, -w Watch for file changes
198223 --verbose, -v Verbose output
224+ --silent, -s Suppress output messages
199225
200226Examples:
227+ luats convert --input ./src/types.lua --output ./dist/types.d.ts
201228 luats convert --input ./src --output ./dist
202229 luats validate --input ./src/main.lua
203230 ` ) ;
@@ -210,33 +237,6 @@ if (require.main === module) {
210237 process . exit ( 1 ) ;
211238 } ) ;
212239}
213-
214- if ( ! fs . existsSync ( inputPath ) ) {
215- throw new Error ( `File not found: ${ inputPath } ` ) ;
216- }
217-
218- if ( ! fs . statSync ( inputPath ) . isFile ( ) ) {
219- throw new Error ( `Not a file: ${ inputPath } ` ) ;
220- }
221-
222- if ( ! options . silent ) {
223- console . log ( `Validating ${ inputPath } ` ) ;
224- }
225-
226- // Read input file
227- const luaCode = fs . readFileSync ( inputPath , 'utf-8' ) ;
228-
229- // Determine if it's Luau based on extension
230- const isLuau = path . extname ( inputPath ) === '.luau' ;
231-
232- // Analyze the code
233- const result = analyze ( luaCode , isLuau ) ;
234-
235- if ( result . errors . length === 0 ) {
236- if ( ! options . silent ) {
237- console . log ( `✓ ${ inputPath } is valid` ) ;
238- }
239- } else {
240240 console . error ( `✗ ${ inputPath } has ${ result . errors . length } error(s)` ) ;
241241
242242 if ( options . verbose || ! options . silent ) {
0 commit comments