Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8070915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:32:13+00:00 2026-06-05T13:32:13+00:00

i want to traverse the directory and get the .js files and uglifying using

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:32:14+00:00Added an answer on June 5, 2026 at 1:32 pm

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

    mkdirp.sync('build/' + dirname);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have load a tree view. i want to Traverse treeview node and expand
i want to write a page that will traverse a specified directory.... and get
I have code like <table> </table> <table> </table> <table> </table> i want to traverse
I'm having the following issue - I want to traverse all the xml files
Let's say that I routinely have to work with files with an unknown, but
I am trying to traverse all directories under the current directory using a shell
I have problems with moving files in directories in Java. The problem is that
In the following code I want to traverse an NSMutableArray (self.codes) which contains NSMutableDictionary
I have the following tree: 1-[HAS]->2-[HASSUBPAGE]->3-[HASSUBPAGE]->4 Now I want to traverse this tree starting
I want to traverse all subdirectories, except the node_modules directory.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.