0

i want to traverse the directory and get the .js files and uglifying using uglifyjs and node.js but i have some problems with my code. below is my code :

var mkdirp = require( 'mkdirp' ),
    walk = require( 'walk' ),
    fs = require( 'fs' ),
    jsp = require( 'uglify-js' ).parser,
    pro = require( 'uglify-js' ).uglify,
    files   = [],
    htmlfilestouglify = [];
// Walker options
var walker  = walk.walk( 'shreedhar/www' , { followLinks: false } );

walker.on('file', function( root, stat, next ) {
    // Add this file to the list of files
    files.push(root + '/' + stat.name);
    next();
});

walker.on( 'end', function() {
    for( var i=0; i<files.length; i++){
        // console.log(files[i]);
        var ext = files[i].split( '.' ).pop();
        if( ext == 'js' ){ 
            console.log( files[i] );
            var orig_code = fs.readFileSync( files[i] ).toString(); //read the content of the file

            // create directory
            var fnarr = files[i].split('/'),
                fname = fnarr.pop( files[i].length-1 ),

                dirlen = fnarr.length,
                dirname = fnarr.slice( 0, dirlen ).join('/');

            mkdirp('build/'+dirname );

            // create file
            fs.open('build/'+dirname+'/'+fname, 'w');

            // uglify the content of the file
            var ast = jsp.parse(orig_code); // parse code and get the initial AST
            ast = pro.ast_mangle(ast); // get a new AST with mangled names
            ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
            var final_code = pro.gen_code(ast); 


            // write uglified code into file

            fs.writeFileSync('build/'+dirname+'/'+fname, final_code); 

        }
        else if( ext == 'html'){
            htmlfilestouglify.push(files[i]);
        }
    }
});

problem is : if i comment the writeFileSync and run the above code it will create the directory and once again after un commenting the writeFileSync and run, it will write the minified code into files, i couldnt figure out the problem with my code.. can anyone please help me out.

Shreedhar
  • 5,502
  • 3
  • 22
  • 27

1 Answers1

3

Because mkdirp is asynchronous. Call the synchronous version and it should work:

mkdirp.sync('build/' + dirname);
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • sir, is there anything like uglifyjs to uglify the html markup? – Shreedhar Jun 13 '12 at 11:50
  • @shreedhar you can't _really_ uglify HTML markup, tags at least can't change (even if you send "uglified" tags you have to restore them with JS and then they'll be available). You may generate ugly IDs and class names but it won't really make your HTML unreadable. – Adriano Repetti Jun 13 '12 at 12:01
  • atleast can we remove whitespaces? – Shreedhar Jun 13 '12 at 12:07
  • @shreedhar yes, you can minify HTML (if you **really** need it), check this post here on SO: http://stackoverflow.com/questions/728260/html-minification – Adriano Repetti Jun 13 '12 at 12:18
  • sir, im thinking to add this code snippet into my blog. its working fine for my problem statement, is ther any ways to improve this snippet before put into my blog? – Shreedhar Jul 09 '12 at 04:45