I am trying to use the fs
readdir
to create a function that push file names into an array, recursively (including files in sub directories), using default attribute dir
as a starting point.
When I tried to console.log
the names it worked fine, but I could not figure out how to create an array with all the names in it (I'm not getting the files in the sub directories).
import { readdir } from 'fs/promises';
const arr = [];
const reader = async (dir = `./src`) => {
try {
const items = await readdir(dir, { withFileTypes: true });
items.map(item => item.isDirectory() ? reader(`${dir}/${item.name}`) : arr.push(item.name));
}
catch (err) {
console.log(err);
}
};
reader().then(() => {
console.log(arr)
})
Thanks!