Essentially I’m trying to store all of file system in a certain dir in an array of objects. Here is my code:
type Cache = {
path: string;
content: Buffer;
};
const readdir = (dir: string, currentDir: string) => {
const dirContent = fs.readdirSync(dir);
const ignore = fs.readFileSync(".gitignore", "utf8").split("n");
ignore.push(".git");
const cache: Cache[] = [];
dirContentLoop: for (let i = 0; i < dirContent.length; i++) {
const path = `${currentDir}/${dirContent[i]}`.replace("./", "");
for (let j = 0; j < ignore.length; j++) {
if (path === ignore[j]) {
continue dirContentLoop;
}
}
if (isDir(path)) {
readdir(dirContent[i], path);
continue;
}
cache.push({
path: path,
content: fs.readFileSync(path)
});
}
global.cache = cache;
};
The function readdir
takes in two params:
dir
the dir to readcurrentDir
the current directory being read in
If a path is a directory the readdir
function calls itself. However when I run the function I get an error:
node:fs:1509
const result = binding.readdir(
^
Error: ENOENT: no such file or directory, scandir 'workflows'
at Object.readdirSync (node:fs:1509:26)
at readdir (/Users/ekrich/git/universal-fs/scripts/cache.ts:10:25)
at readdir (/Users/ekrich/git/universal-fs/scripts/cache.ts:25:7)
at readdir (/Users/ekrich/git/universal-fs/scripts/cache.ts:25:7)
at cache (/Users/ekrich/git/universal-fs/scripts/cache.ts:39:3)
at <anonymous> (/Users/ekrich/git/universal-fs/scripts/cache.ts:63:1)
at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:474:24)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:109:5) {
errno: -2,
code: 'ENOENT',
syscall: 'scandir',
path: 'workflows'
}
Node.js v22.3.0
What is wrong with my logic here?