5

Attempting to use the code from the following answer: https://stackoverflow.com/a/24594123/3951987

Returns true for a file inside a directory for me.

To provide more context, here's some basic code to describe my implementation:

getDirs = (a_path) => {
    return fs.readdirSync(a_path)
        .filter(file => fs.statSync(path.join(a_path, file)).isDirectory());
}

someCode = () => {
    let some_path = path.resolve(process.cwd(), "example");
    console.log(getDirs(some_path));
}

In my project, I have:

example/
- test/
- manifest.json
index.js

Somehow the getDirs function described above still returns manifest.json as a directory, even with the given filter applied.

Anyone else had this? Any ideas?

Additional Information

I've modified the getDirs function to the following:

getDirs = (a_path) => {
    console.log("getDirs a_path = "+a_path);
    let dirs = fs.readdirSync(a_path);

    for(let x = 0; x<dirs.length; x++){
        let a_dir = path.resolve(a_path, dirs[x]);
        console.log(dirs[x]);
        console.log(fs.statSync(a_path, a_dir).isDirectory());
    }
    return dirs;
};

The output for this is:

getDirs a_path = <pathtoproject>/example
test
true
manifest.json
true
Community
  • 1
  • 1
jarodsmk
  • 1,876
  • 2
  • 21
  • 40

1 Answers1

7

Change:

console.log(fs.statSync(a_path, a_dir).isDirectory());

to:

console.log(fs.statSync(a_dir).isDirectory());

Otherwise your stating the a_path, not a_dir.

fs.statSync takes one argument - see the docs:

rsp
  • 107,747
  • 29
  • 201
  • 177
  • Ugh, such a small oversight giving me a headache... Well spotted sir :) – jarodsmk Mar 02 '17 at 11:10
  • Looking back, since I dont need to compile my absolute paths, the example i used had fs.statSync(path.join(... where, I pass the path directly – jarodsmk Mar 02 '17 at 11:16
  • @N15M0_jk Yes, and your first example works well with: `.filter(file => fs.statSync(path.join(a_path, file)).isDirectory());` – rsp Mar 02 '17 at 11:57