@@ -148,6 +148,9 @@ mainOptionKeys.forEach(function(key) {
148148 program . option ( key , option ) ;
149149 }
150150} ) ;
151+ var inplace = false ;
152+ program . option ( '--inplace' , 'Specify that the files should be overwritten.' , function ( newInplace ) {
153+ inplace = true ; } ) ;
151154program . option ( '-o --output <file>' , 'Specify output file (if not specified STDOUT will be used for output)' , function ( outputPath ) {
152155 return fs . createWriteStream ( outputPath ) . on ( 'error' , function ( e ) {
153156 fatal ( 'Cannot write ' + outputPath + '\n' + e . message ) ;
@@ -191,8 +194,9 @@ program.option('--input-dir <dir>', 'Specify an input directory');
191194program . option ( '--output-dir <dir>' , 'Specify an output directory' ) ;
192195program . option ( '--file-ext <text>' , 'Specify an extension to be read, ex: html' ) ;
193196var content ;
194- program . arguments ( '[files...]' ) . action ( function ( files ) {
195- content = files . map ( readFile ) . join ( '' ) ;
197+ var files ;
198+ program . arguments ( '[files...]' ) . action ( function ( newFiles ) {
199+ files = newFiles ;
196200} ) . parse ( process . argv ) ;
197201
198202function createOptions ( ) {
@@ -286,24 +290,31 @@ function writeMinify() {
286290var inputDir = program . inputDir ;
287291var outputDir = program . outputDir ;
288292var fileExt = program . fileExt ;
289- if ( inputDir || outputDir ) {
290- if ( ! inputDir ) {
291- fatal ( 'The option output-dir needs to be used with the option input-dir. If you are working with a single file, use -o.' ) ;
293+ if ( inplace ) {
294+ files . forEach ( function ( file ) { processFile ( file , file ) ; } ) ;
295+ } else {
296+ if ( files ) {
297+ content = files . map ( readFile ) . join ( '' ) ;
292298 }
293- else if ( ! outputDir ) {
294- fatal ( 'You need to specify where to write the output files with the option --output-dir' ) ;
299+ if ( inputDir || outputDir ) {
300+ if ( ! inputDir ) {
301+ fatal ( 'The option output-dir needs to be used with the option input-dir. If you are working with a single file, use -o.' ) ;
302+ }
303+ else if ( ! outputDir ) {
304+ fatal ( 'You need to specify where to write the output files with the option --output-dir' ) ;
305+ }
306+ processDirectory ( inputDir , outputDir , fileExt ) ;
307+ }
308+ // Minifying one or more files specified on the CMD line
309+ else if ( typeof content === 'string' ) {
310+ writeMinify ( ) ;
311+ }
312+ // Minifying input coming from STDIN
313+ else {
314+ content = '' ;
315+ process . stdin . setEncoding ( 'utf8' ) ;
316+ process . stdin . on ( 'data' , function ( data ) {
317+ content += data ;
318+ } ) . on ( 'end' , writeMinify ) ;
295319 }
296- processDirectory ( inputDir , outputDir , fileExt ) ;
297- }
298- // Minifying one or more files specified on the CMD line
299- else if ( typeof content === 'string' ) {
300- writeMinify ( ) ;
301- }
302- // Minifying input coming from STDIN
303- else {
304- content = '' ;
305- process . stdin . setEncoding ( 'utf8' ) ;
306- process . stdin . on ( 'data' , function ( data ) {
307- content += data ;
308- } ) . on ( 'end' , writeMinify ) ;
309320}
0 commit comments