@@ -5,11 +5,14 @@ import zlib from 'zlib';
55import fs from 'fs' ;
66import path from 'path' ;
77
8+ let log = console . log
9+
810function parseCommandLineArgs ( ) {
911 const optionDefinitions = [
1012 { name : 'decompress' , alias : 'd' , type : Boolean , description : 'Decompress files (default: compress).' } ,
1113 { name : 'keep' , alias : 'k' , type : Boolean , description : 'Keep input files after processing (default: delete).' } ,
1214 { name : 'help' , alias : 'h' , type : Boolean , description : 'Print this usage guide.' } ,
15+ { name : 'quiet' , alias : 'q' , type : Boolean , description : 'Quite output, only print errors.' } ,
1316 { name : 'globs' , type : String , multiple : true , defaultOption : true , description : 'Glob patterns of files to process.' } ,
1417 ] ;
1518 const options = commandLineArgs ( optionDefinitions ) ;
@@ -32,10 +35,14 @@ function parseCommandLineArgs() {
3235 process . exit ( 0 ) ;
3336 }
3437
38+ if ( options . quiet ) {
39+ log = ( ) => { } ;
40+ }
41+
3542 if ( options . globs === undefined ) {
3643 if ( options . decompress ) {
3744 const defaultGlob = '**/*.z' ;
38- console . log ( `No input glob pattern given, using default: ${ defaultGlob } ` ) ;
45+ log ( `No input glob pattern given, using default: ${ defaultGlob } ` ) ;
3946 options . globs = [ defaultGlob ] ;
4047 } else {
4148 // For compression, require the user to specify explicit input file patterns.
@@ -61,9 +68,9 @@ function compress(inputData) {
6168 const originalSize = inputData . length ;
6269 const compressedSize = compressedData . length ;
6370 const compressionRatio = calculateCompressionRatio ( originalSize , compressedSize ) ;
64- console . log ( ` Original size: ${ String ( originalSize ) . padStart ( 8 ) } bytes` ) ;
65- console . log ( ` Compressed size: ${ String ( compressedSize ) . padStart ( 8 ) } bytes` ) ;
66- console . log ( ` Compression ratio: ${ compressionRatio . toFixed ( 2 ) . padStart ( 8 ) } %` ) ;
71+ log ( ` Original size: ${ String ( originalSize ) . padStart ( 8 ) } bytes` ) ;
72+ log ( ` Compressed size: ${ String ( compressedSize ) . padStart ( 8 ) } bytes` ) ;
73+ log ( ` Compression ratio: ${ compressionRatio . toFixed ( 2 ) . padStart ( 8 ) } %` ) ;
6774
6875 return compressedData ;
6976}
@@ -74,18 +81,19 @@ function decompress(inputData) {
7481 const compressedSize = inputData . length ;
7582 const decompressedSize = decompressedData . length ;
7683 const expansionRatio = calculateExpansionRatio ( compressedSize , decompressedSize ) ;
77- console . log ( ` Compressed size: ${ String ( compressedSize ) . padStart ( 8 ) } bytes` ) ;
78- console . log ( ` Decompressed size: ${ String ( decompressedSize ) . padStart ( 8 ) } bytes` ) ;
79- console . log ( ` Expansion ratio: ${ expansionRatio . toFixed ( 2 ) . padStart ( 8 ) } %` ) ;
84+ log ( ` Compressed size: ${ String ( compressedSize ) . padStart ( 8 ) } bytes` ) ;
85+ log ( ` Decompressed size: ${ String ( decompressedSize ) . padStart ( 8 ) } bytes` ) ;
86+ log ( ` Expansion ratio: ${ expansionRatio . toFixed ( 2 ) . padStart ( 8 ) } %` ) ;
8087
8188 return decompressedData ;
8289}
8390
8491function globsToFiles ( globs ) {
8592 let files = [ ] ;
86- console . assert ( globs . length > 0 ) ;
93+ console . assert ( globs . length > 0 ) ;
94+ const globtions = { nodir : true , ignore : '**/node_modules/**' } ;
8795 for ( const glob of globs ) {
88- const matches = globSync ( glob , { nodir : true } ) ;
96+ const matches = globSync ( glob , globtions ) ;
8997 files = files . concat ( matches ) ;
9098 }
9199 files = Array . from ( new Set ( files ) ) . sort ( ) ;
@@ -94,7 +102,7 @@ function globsToFiles(globs) {
94102
95103function processFiles ( files , isDecompress , keep ) {
96104 const verb = isDecompress ? 'decompress' : 'compress' ;
97- console . log ( `Found ${ files . length } files to ${ verb } ` + ( files . length ? ':' : '.' ) ) ;
105+ log ( `Found ${ files . length } files to ${ verb } ` + ( files . length ? ':' : '.' ) ) ;
98106
99107 // For printing overall statistics at the end.
100108 let totalInputSize = 0 ;
@@ -111,7 +119,7 @@ function processFiles(files, isDecompress, keep) {
111119 } else {
112120 outputFilename = inputFilename . slice ( 0 , - 2 ) ;
113121 }
114- console . log ( ` Decompressing to: ${ outputFilename } ` ) ;
122+ log ( ` Decompressing to: ${ outputFilename } ` ) ;
115123 } else {
116124 if ( path . extname ( inputFilename ) === '.z' ) {
117125 console . warn ( ` Warning: Input file already has a .z extension.` ) ;
@@ -130,7 +138,7 @@ function processFiles(files, isDecompress, keep) {
130138
131139 if ( ! keep ) {
132140 fs . unlinkSync ( inputFilename ) ;
133- console . log ( ` Deleted input file.` ) ;
141+ log ( ` Deleted input file.` ) ;
134142 }
135143 } catch ( err ) {
136144 console . error ( `Error ${ verb } ing ${ inputFilename } :` , err ) ;
@@ -140,14 +148,14 @@ function processFiles(files, isDecompress, keep) {
140148 if ( files . length > 1 ) {
141149 if ( isDecompress ) {
142150 const totalExpansionRatio = calculateExpansionRatio ( totalInputSize , totalOutputSize ) ;
143- console . log ( `Total compressed sizes: ${ String ( totalInputSize ) . padStart ( 9 ) } bytes` ) ;
144- console . log ( `Total decompressed sizes: ${ String ( totalOutputSize ) . padStart ( 9 ) } bytes` ) ;
145- console . log ( `Average expansion ratio: ${ totalExpansionRatio . toFixed ( 2 ) . padStart ( 9 ) } %` ) ;
151+ log ( `Total compressed sizes: ${ String ( totalInputSize ) . padStart ( 9 ) } bytes` ) ;
152+ log ( `Total decompressed sizes: ${ String ( totalOutputSize ) . padStart ( 9 ) } bytes` ) ;
153+ log ( `Average expansion ratio: ${ totalExpansionRatio . toFixed ( 2 ) . padStart ( 9 ) } %` ) ;
146154 } else {
147155 const totalCompressionRatio = calculateCompressionRatio ( totalInputSize , totalOutputSize ) ;
148- console . log ( `Total original sizes: ${ String ( totalInputSize ) . padStart ( 9 ) } bytes` ) ;
149- console . log ( `Total compressed sizes: ${ String ( totalOutputSize ) . padStart ( 9 ) } bytes` ) ;
150- console . log ( `Average compression ratio: ${ totalCompressionRatio . toFixed ( 2 ) . padStart ( 9 ) } %` ) ;
156+ log ( `Total original sizes: ${ String ( totalInputSize ) . padStart ( 9 ) } bytes` ) ;
157+ log ( `Total compressed sizes: ${ String ( totalOutputSize ) . padStart ( 9 ) } bytes` ) ;
158+ log ( `Average compression ratio: ${ totalCompressionRatio . toFixed ( 2 ) . padStart ( 9 ) } %` ) ;
151159 }
152160 }
153161}
0 commit comments