@@ -280,63 +280,129 @@ export default class PDFCLI {
280280 this . statusMsgs = [ ] ;
281281 }
282282
283- initialize ( ) {
284- let retVal = true ;
283+ initialize ( ) : { success : boolean ; error ?: string } {
285284 try {
285+ // Handle version and help flags
286286 if ( ONLY_SHOW_VERSION ) {
287287 console . log ( pkInfo . version ) ;
288- retVal = false ;
289- } else if ( ONLY_SHOW_HELP ) {
290- yargs . showHelp ( ) ;
291- retVal = false ;
292- } else if ( ! HAS_INPUT_DIR_OR_FILE ) {
288+ return { success : false } ;
289+ }
290+
291+ if ( ONLY_SHOW_HELP ) {
293292 yargs . showHelp ( ) ;
294- console . error ( "-f is required to specify input directory or file." ) ;
295- retVal = false ;
293+ return { success : false } ;
294+ }
295+
296+ // Validate mandatory -f parameter
297+ if ( ! HAS_INPUT_DIR_OR_FILE ) {
298+ return {
299+ success : false ,
300+ error : "-f|--file parameter is required to specify input directory or file."
301+ } ;
302+ }
303+
304+ // Validate that -f has a value
305+ if ( typeof INPUT_DIR_OR_FILE !== 'string' || INPUT_DIR_OR_FILE . trim ( ) === '' ) {
306+ return {
307+ success : false ,
308+ error : "-f|--file parameter must have a valid path value."
309+ } ;
310+ }
311+
312+ // Validate that -f is not specified multiple times
313+ if ( Array . isArray ( INPUT_DIR_OR_FILE ) ) {
314+ return {
315+ success : false ,
316+ error : `-f|--file parameter can only be specified once. Received multiple values: ${ INPUT_DIR_OR_FILE . join ( ", " ) } `
317+ } ;
296318 }
319+
320+ // Validate input path exists
321+ if ( ! fs . existsSync ( INPUT_DIR_OR_FILE ) ) {
322+ return {
323+ success : false ,
324+ error : `Input path does not exist: ${ INPUT_DIR_OR_FILE } `
325+ } ;
326+ }
327+
328+ // Validate output directory if specified
329+ // if (argv.o && !fs.existsSync(argv.o)) {
330+ // return {
331+ // success: false,
332+ // error: `Output directory does not exist: ${argv.o}`
333+ // };
334+ // }
335+
336+ return { success : true } ;
297337 } catch ( e : unknown ) {
298338 const error = e instanceof Error ? e : new Error ( String ( e ) ) ;
299- console . error ( `Exception: ${ error . message } ` ) ;
300- retVal = false ;
339+ return {
340+ success : false ,
341+ error : `Exception during initialization: ${ error . message } `
342+ } ;
301343 }
302- return retVal ;
303344 }
304345
305346 async start ( ) {
306- if ( ! this . initialize ( ) || ! INPUT_DIR_OR_FILE ) {
307- console . error ( "Invalid input parameters." ) ;
308- return ;
347+ // Initialize and validate parameters
348+ const initResult = this . initialize ( ) ;
349+ if ( ! initResult . success ) {
350+ if ( initResult . error ) {
351+ // Show help for parameter errors
352+ yargs . showHelp ( ) ;
353+ console . error ( `\nError: ${ initResult . error } ` ) ;
354+ process . exit ( 1 ) ;
355+ }
356+ // Exit cleanly for -v or -h flags (no error)
357+ process . exit ( 0 ) ;
309358 }
310359
311360 console . log ( _PRO_TIMER ) ;
312361 console . time ( _PRO_TIMER ) ;
313362
363+ let hasError = false ;
364+ let errorMessage : string | undefined ;
365+
314366 try {
315- const inputStatus = fs . statSync ( INPUT_DIR_OR_FILE ) ;
367+ const inputStatus = fs . statSync ( INPUT_DIR_OR_FILE as string ) ;
316368 if ( inputStatus . isFile ( ) ) {
317369 this . inputCount = 1 ;
318370 await this . processOneFile (
319- path . dirname ( INPUT_DIR_OR_FILE ) ,
320- path . basename ( INPUT_DIR_OR_FILE )
371+ path . dirname ( INPUT_DIR_OR_FILE as string ) ,
372+ path . basename ( INPUT_DIR_OR_FILE as string )
321373 ) ;
322374 } else if ( inputStatus . isDirectory ( ) ) {
323- await this . processOneDirectory ( path . normalize ( INPUT_DIR_OR_FILE ) ) ;
375+ await this . processOneDirectory ( path . normalize ( INPUT_DIR_OR_FILE as string ) ) ;
324376 }
325377 } catch ( e ) {
326- console . error ( "Exception: " , e ) ;
378+ hasError = true ;
379+ const error = e instanceof Error ? e : new Error ( String ( e ) ) ;
380+ errorMessage = `Exception during processing: ${ error . message } ` ;
381+ this . addStatusMsg ( true , errorMessage ) ;
382+ this . failedCount ++ ;
327383 } finally {
328- this . complete ( ) ;
384+ this . complete ( hasError , errorMessage ) ;
329385 }
330386 }
331387
332- complete ( ) {
333- if ( this . statusMsgs . length > 0 ) console . log ( this . statusMsgs ) ;
334- console . log (
335- `${ this . inputCount } input files\t${ this . successCount } success\t${ this . failedCount } fail\t${ this . warningCount } warning`
388+ complete ( hasError : boolean = false , errorMessage ?: string ) {
389+ const stdioFunc = ( hasError || this . failedCount > 0 ) ? console . error : console . log ;
390+
391+ if ( errorMessage ) {
392+ stdioFunc ( `\nError: ${ errorMessage } ` ) ;
393+ }
394+ if ( this . statusMsgs . length > 0 ) {
395+ stdioFunc ( this . statusMsgs ) ;
396+ }
397+ stdioFunc (
398+ `\n${ this . inputCount } input files\t${ this . successCount } success\t${ this . failedCount } fail\t${ this . warningCount } warning`
336399 ) ;
400+
337401 process . nextTick ( ( ) => {
338402 console . timeEnd ( _PRO_TIMER ) ;
339- // process.exit((this.inputCount === this.successCount) ? 0 : 1);
403+ if ( hasError || this . failedCount > 0 ) {
404+ process . exit ( 1 ) ;
405+ }
340406 } ) ;
341407 }
342408
0 commit comments