2

I Know how to convert the directory structure into JSON object, from here

But I want all the files in an array, and the folders in object with the object key as the folder name. I have been trying for a long time but just cannot get it done.

Thi is what I have tried so far:

var diretoryTreeToObj = function (dir, done) {
  var results = {};
  var _contents = [];
  fs.readdir(dir, function (err, list) {
    if (err) {
      return done(err);
    }
    var pending = list.length;
    if (!pending) {
      return done(null, {name: path.basename(dir), type: 'folder', children: results});
    }
    list.forEach(function (file, index) {
      file = path.resolve(dir, file);
      fs.stat(file, function (err, stat) {
        if (stat && stat.isDirectory()) {
          diretoryTreeToObj(file, function (err, res) {
            results[path.basename(file)] = {
              name: path.basename(file),
              type: 'folder',
              path: path.dirname(file),
              _contents: [res]
            };
            if (!--pending) {
              done(null, results);
            }
          });
        } else {
          results['_contents'] = [{
            name: path.basename(file),
            path: file
          }];
          if (!--pending) {
            done(null, results);
          }
        }
      });
    });
  });
};

Thanks in advance. :)

Community
  • 1
  • 1
aditya
  • 339
  • 2
  • 12
  • Show us what you have tried so far. – georoot Mar 04 '17 at 10:05
  • updated the question, take a look – aditya Mar 04 '17 at 10:13
  • I can't try it right now but I guess the issue is that your code is synchronous since it uses *fs* module and callbacks. I would try to add a `console.log(file)` just below the call `diretoryTreeToObj(file,....` to see if you get multiple times same file name. Also can you specify what exactly doesn't work? Do you get any error? – Molda Mar 04 '17 at 10:58

1 Answers1

0

Finally I figured out the solution, here it is if anybody needs it:

var diretoryTreeToObj = function (dir, done) {
  var results = {};
  var _contents = [];
  var files = [];
  fs.readdir(dir, function (err, list) {
    if (err) {
      return done(err);
    }
    var pending = list.length;
    if (!pending) {
      return done(null, {name: path.basename(dir), type: 'folder', children: results});
    }
    list.forEach(function (file, index) {
      file = path.resolve(dir, file);
      fs.stat(file, function (err, stat) {
        results['_contents'] = files;
        if (stat && stat.isDirectory()) {
          diretoryTreeToObj(file, function (err, res) {
            results[path.basename(file)] = res;
            if (!--pending) {
              done(null, results);
            }
          });
        } else {
          files.push({
            name: path.basename(file),
            path: file
          });
          results['_contents'] = files;
          if (!--pending) {
            done(null, results);
          }
        }
      });
    });
  });
};
aditya
  • 339
  • 2
  • 12