Search code examples
javascriptnode.jsdirectory

Array of directory tree node.js


I am trying to generate a directory tree into an array using node js. Output would be:

[ __Dirname [array of sub directories] ]
["FolderA"[ ["SubFolderA",[]] ]], ["FolderB",[]]

function readDir(dir){
   fs.readdir(dir, function(err, files){
      for(var i=0;i<files.length;i++){
        (function(j){
          fs.stat(files[j], function(err, stats){
            if(stats.isDirectory()){
              readDir(files[j]);
            }
          });
        }
      }
   });
}

If you know an easier way to do this please inform me. All I need is a list of directories and all their sub directories recursively.


Solution

  • The following code uses node-walker to generate the directory tree object.

    var path = require('path');
    var util = require('util');
    var walker = require('walker');
    
    /*
     * To walk a directory and generate the tree object
     * 
     * @param dest {string} the directory to start with.
     * @param cb {function} the callback function, cb(err, dirObj)
     * 
     */    
    function readDir(dest, cb) {
      var dirObj = {};
      var child, parts, obj;
    
      walker(dest)
        .on('dir', function(dir, stat) {
          if (dir === dest) return;
    
          child = dir.slice(dest.length, dir.length);
          if (child.indexOf(path.sep) === 0) {
            child = child.slice(1, child.length);
          };
    
          parts = child.split(path.sep);
    
          obj = dirObj;
    
          for(var i=0;i<parts.length;i++) {
            if (parts[i] !== '') {
              if (obj[parts[i]] === undefined) {
                obj[parts[i]] = {};
              };
    
              obj = obj[parts[i]];
            }
          };
        })
        .on('error', function(err, entry, stat) {
          cb(err, null);
        })
        .on('end', function() {
          cb(null, dirObj);
        })
    };
    
    readDir(__dirname, function(err, dirObj) {
      if (err) {
        console.log(err);
      } else {
        // Handle the returned directory object
        console.log(util.inspect(dirObj, {showHidden: true, depth: null}));
      }
    });