I'm trying to iterate a given directory recursively using Node and Bluebird (the promises library) but as far as it seems it doesn't work correctly.
When I uncomment the 'console.log' line below, it seems like I'm getting the correct results but I'm not sure what's going on because the final result I'm getting from the consuming code below is just the first file in the first directory.
Maybe the issue is not in the iterate function itself but in the way I'm consuming it.
I'm kinda new to promises so maybe I'm just approaching it in the wrong way.
Here is the code I wrote.
import * as Path from "path";
import * as Promise from "bluebird";
const FS: any = Promise.promisifyAll<any>((require("fs")));
export class Dir {
public static iterate(path: string) {
return new Promise((resolve, reject) => {
FS.lstatAsync(path).then(stat => {
if (stat.isDirectory()) {
FS.readdirAsync(path).each(name => {
let fullPath = Path.join(path, name);
// console.log(fullPath);
FS.lstatAsync(fullPath).then(stat => stat.isDirectory() ? Dir.iterate(fullPath) : resolve(fullPath));
});
} else {
reject(`The path '${path}' is not a directory.`)
}
});
})
}
}
Here is the way I'm using/consuming it
Dir.iterate(ProjectPaths.client)
.then(f => {
console.log(f)
return f;
})
.catch(e => console.log(e));
EDIT: Just to clarify I'm using TypeScript! forgot to mention it in my post.