@@ -495,7 +495,35 @@ function jsonToGo(json, typename, flatten = true, example = false, allOmitempty
495
495
496
496
if ( typeof module != 'undefined' ) {
497
497
if ( ! module . parent ) {
498
- if ( process . argv . length > 2 && process . argv [ 2 ] === '-big' ) {
498
+ let bigstdin = false
499
+ let filename = null
500
+
501
+ process . argv . forEach ( ( val , index ) => {
502
+ if ( index < 2 )
503
+ return
504
+
505
+ if ( ! val . startsWith ( '-' ) ) {
506
+ filename = val
507
+ return
508
+ }
509
+
510
+ const argument = val . replace ( / - / g, '' )
511
+ if ( argument === "big" )
512
+ bigstdin = true
513
+ else {
514
+ console . error ( `Unexpected argument ${ val } received` )
515
+ process . exit ( 1 )
516
+ }
517
+ } )
518
+
519
+ if ( filename ) {
520
+ const fs = require ( 'fs' ) ;
521
+ const json = fs . readFileSync ( filename , 'utf8' ) ;
522
+ process . stdout . write ( jsonToGo ( json ) . go )
523
+ return
524
+ }
525
+
526
+ if ( bigstdin ) {
499
527
bufs = [ ]
500
528
process . stdin . on ( 'data' , function ( buf ) {
501
529
bufs . push ( buf )
@@ -504,16 +532,14 @@ if (typeof module != 'undefined') {
504
532
const json = Buffer . concat ( bufs ) . toString ( 'utf8' )
505
533
process . stdout . write ( jsonToGo ( json ) . go )
506
534
} )
507
- } else if ( process . argv . length === 3 ) {
508
- const fs = require ( 'fs' ) ;
509
- const json = fs . readFileSync ( process . argv [ 2 ] , 'utf8' ) ;
510
- process . stdout . write ( jsonToGo ( json ) . go )
511
- } else {
512
- process . stdin . on ( 'data' , function ( buf ) {
513
- const json = buf . toString ( 'utf8' )
514
- process . stdout . write ( jsonToGo ( json ) . go )
515
- } )
535
+ return
516
536
}
537
+
538
+ // read from stdin
539
+ process . stdin . on ( 'data' , function ( buf ) {
540
+ const json = buf . toString ( 'utf8' )
541
+ process . stdout . write ( jsonToGo ( json ) . go )
542
+ } )
517
543
} else {
518
544
module . exports = jsonToGo
519
545
}
0 commit comments