0

I just have a quick question there:

I am using Node.JS to write a commandline tool that validates JSON Files with JSON Schemas. So, now I have a problem that when wanting to get all the schemas, that I always get "undefined" for using a async function but otherwise only sync functions.

For this commandline tool async is NOT needed.

Could someone help me out and give me a hand on how to make it work just fine?

var getJSONSchemaFiles = function (dir) {
results2 = [];


    var recursive = require('recursive-readdir');

    recursive(dir, function (err, files) {
        // Files is an array of filename
      //  console.log(files);

        files.forEach(function (entry) {

            if (entry.indexOf(".schema.json") > -1) {

                results2.push(entry);

            }
        });
        console.log(results2);

    });

        return results2;

};

I am using the npm "recursive-readdir" but I think that I do not even need a npm for this kind of thing?

Stefan C.
  • 101
  • 1
  • 6
  • You can't return a value from an asynchronous function. See this question: http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function – soulprovidr Dec 01 '14 at 14:50
  • Thats why I asked how to re-write my function to do the same but be sync? – Stefan C. Dec 01 '14 at 14:53
  • The module you're using is asynchronous by design. You might be able to find one that makes use of node's synchronous file I/O methods (like [`readdirSync`](http://nodejs.org/api/fs.html#fs_fs_readdirsync_path)). The logic isn't super crazy, so I'm confident you could put a script together yourself that does what you want. – soulprovidr Dec 01 '14 at 15:04
  • It seems recursive-readdir contains only async version. You have to change your function to work asynchronously: var getJSONSchemaFiles = function (dir, callback)... or use some other module. – Viktor Kireev Dec 01 '14 at 15:04

2 Answers2

1

Ok, this enumerates all files under the given path synchronously:

var fs = require('fs');

function recursiveReaddir(path) {
    var stat = fs.lstatSync(path);
    if(stat.isFile())
        return [path];
    if(!stat.isDirectory())
        return [];
    return [].concat.apply([], fs.readdirSync(path).map(function(fname) {
        return recursiveReaddir(path + '/' + fname);
    }));
}
georg
  • 211,518
  • 52
  • 313
  • 390
0

Use glob module https://github.com/isaacs/node-glob. There is async and Sync methods like: glob.sync(pattern, [options]); and glob(pattern, [options], cb);
Example from their docs:

var glob = require("glob")

// options is optional
glob("**/*.js", options, function (er, files) {
  // files is an array of filenames.
  // If the `nonull` option is set, and nothing
  // was found, then files is ["**/*.js"]
  // er is an error object or null.
})
Sergey Yarotskiy
  • 517
  • 4
  • 14