- 
      
- 
        Save darkcolonist/1f40370db9db5e625e9c to your computer and use it in GitHub Desktop. 
    List all files in a directory in Node.js recursively in a synchronous fashion
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /** | |
| * include all js files in directory (node.js) | |
| * | |
| * each file included is concatenated to the file where it was included | |
| * like in php | |
| */ | |
| var fs = require("fs"); | |
| function include_dir(dir, recurse, filelist) { | |
| var fs = fs || require('fs'), | |
| files = fs.readdirSync(dir); | |
| filelist = filelist || []; | |
| recurse = recurse || false; | |
| files.forEach(function(file) { | |
| if (fs.statSync(dir + file).isDirectory()) { | |
| if(recurse){ | |
| filelist = include_dir(dir + file + '/', recurse, filelist); | |
| } | |
| } | |
| else { | |
| console.log("loading "+(dir + file)); | |
| eval(fs.readFileSync(dir + file).toString()); | |
| } | |
| }); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var walkSync = function(dir, recurse, filelist) { | |
| var fs = fs || require('fs'), | |
| files = fs.readdirSync(dir); | |
| filelist = filelist || []; | |
| recurse = recurse || false; | |
| files.forEach(function(file) { | |
| if (fs.statSync(dir + file).isDirectory()) { | |
| if(recurse){ | |
| filelist = walkSync(dir + file + '/', recurse, filelist); | |
| } | |
| } | |
| else { | |
| filelist.push(dir+file); | |
| } | |
| }); | |
| return filelist; | |
| }; | |
| console.log(walkSync("./", true)); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
if recurse set to true