0

So I have a script that makes an object from the current directory and everything below it.

Here's a console.log of the return statement.

› node recursiveObjectifyDir.js
{ files:
   { 'LICENSE-MIT': { stat: [Object] },
     Gruntfile: { stat: [Object] } },
  dirs: { nested2: [Circular] } }

What does this [Circular] thing mean and where does it come from?

Here the actual script for reference:

var fs = require('fs');
var path = require('path');

function buildFolder(folder) {
    if (!folder) {
        folder = __dirname;
    }
    nestedFiles = fs.readdirSync(folder);
    currentFile = folder + '/' + nestedFiles.pop();
    main = {
        files: {},
        dirs: {}
    };
    while (currentFile !== folder + '/undefined') {
        var fileName = path.basename(currentFile, path.extname(currentFile));
        var fileStat = fs.statSync(currentFile);
        var fullPath = folder + '/' + fileName;
        if (fileStat.isDirectory()) {
            if (fullPath.match(/git/)){
            } else if (fullPath.match(/node_modules/)){
            } else{
              var folderObj = buildFolder(fullPath);
              Object.defineProperty(main.dirs,
                  fileName, {
                      configurable: true,
                      enumerable: true,
                      value: folderObj
                  }
              );
            }
        } else {
            if (fullPath.match(/DS_Store/)){}
            Object.defineProperty(main.files,
                fileName, {
                    configurable: true,
                    enumerable: true,
                    value: {
                        stat: fileStat
                    },
                }
            );
        }
       currentFile = folder + '/' + nestedFiles.pop();
    }
    return main;
}

buildFolderReturn = buildFolder();
console.log(buildFolderReturn);
ovatsug25
  • 7,786
  • 7
  • 34
  • 48

1 Answers1

1

With node.js (javascript language) you have to be careful about scope. Within your function, which is called recursively, those variables at the start you are treating as if they are in the function scope however they are accessible to your recursive calls of the same function. Just put var in front of those as you have done in the loop.

var nestedFiles = fs.readdirSync(folder);
var currentFile = folder + '/' + nestedFiles.pop();
var main = {
    files: {},
    dirs: {}
};

I think that might be the problem here. Put var on everything unless you expect it to be available in scope already. Unless you are already in global scope but I suggest putting var on any variable which is not already in scope.

Action Dan
  • 443
  • 4
  • 10
  • The scope issue you mention is relevant in Javascript, not just Node.js. – Jared Farrish Jun 28 '14 at 12:56
  • yay... http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-in-ecmascript-262-3rd-edition-javascript – ovatsug25 Jun 28 '14 at 20:20
  • f you're in the global scope then there's no difference. If you're in a function then "var" will create a local variable, "no var" will look up the scope chain until it finds the variable or hits the global scope (at which point it will create it): – ovatsug25 Jun 28 '14 at 20:21
  • Jared I tweaked the answer to include the reference to javascript. Thank you. – Action Dan Jun 29 '14 at 23:50