1

I'm working on a relatively simple node app and using fs to help write files and folders. One of checks I need to do is see if a folder already exists, and it does, stop as I don't want to overwrite others.

I wrote a small function to check if the folder exists, but for every folder I give it, it always says it does exists, even when it does not.

checkFolder: function(folder) {
        try {
            fs.existsSync(folder);
            return true;
        } catch (e) {
            console.log('Folder does not exists')
            return false;
        }
    }

And the folder path I'm passing is set via a varabiable and the fdfdfd is the fake folder which does not exists, but the function is reporting it does.

let dialoguePath = path.join(process.cwd(), process.env.aemRepo, process.env.dialogueFolder, answers.componentName)
// /home/sga/media/dev/Work/omitted/Adobe/AEM/Projects/omitted/ui.apps/src/main/content/jcr_root/apps/q/components/fdfdfd
libs.checkFolder(dialoguePath)

I'm sure it's something stupid simple, but about to step out for a few hours so thought another set of eyes would spot my mistake.

Thanks!

TimLavelle
  • 81
  • 7

1 Answers1

2

existSync returns a boolean therefore:

checkFolder: function(folder) {
   return fs.existsSync(folder);
}
cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • Thanks @cyberwombat, reading a previous comment and seeing this, I was indeed doing stupid stuff and over complicating it. – TimLavelle Jul 23 '23 at 02:35