Christoph Blüm
Christoph Blüm

Reputation: 975

Add filenames from Directory to Object

I am trying to add filenames from a directory with sub-directories to an object.

My folders are structured as the following image shows:

enter image description here

Every time I run my code, only filenames (here commands) in the Top Folder are added to my object.

async function getCommands() {
   let commands = {};

   //get all enteties from folder
   let commandFiles = await fs.promises.readdir("./commands/", (err, elements) => {
       if (err) return console.log(err);
   });

   commandFiles.forEach(file => { //loop through all elements in folder "commands"
      const element_in_folder = fs.statSync(`./commands/${file}`)
      if (element_in_folder.isDirectory() == true) { //check if element in folder is a subfolder
         const sub_directory = `./commands/${file}/`;
         addCommandsFromSubFolder(commands, sub_directory);
      } else {
         //add command to object
         commands[Object.keys(commands).length] = {
            "command": file
         }
      }
   });
   return commands; //return full object with all commands
}

function addCommandsFromSubFolder(commands, sub_directory) {
   //loop through all files in subfolders
   fs.readdir(sub_directory, (err, files) => {
      if (err) return console.log(err);
      let commandsInSubFolder = files.values();
      //add files from subfolder to the object
      for (const command of commandsInSubFolder ) {
         commands[Object.keys(commands).length] = {
            "command": command
         }
      }
   });
}

If I log commands in addCommandsFromSubFolder, I can still see, that the files from all subfolders are still added, but node won't wait for them to be added.

Upvotes: 0

Views: 94

Answers (1)

Mahdi Zarrintareh
Mahdi Zarrintareh

Reputation: 176

you should await on function call for subfolder

async function getCommands() {
   let commands = {};

   //get all enteties from folder
   let commandFiles = await fs.promises.readdir("./commands/", (err, elements) => {
       if (err) return console.log(err);
   });

   for (const file of commandFiles) {
      const element_in_folder = fs.statSync(`./commands/${file}`)
      if (element_in_folder.isDirectory() == true) { //check if element in folder is a subfolder
         const sub_directory = `./commands/${file}/`;
         await addCommandsFromSubFolder(commands, sub_directory);
      } else {
         //add command to object
         commands[Object.keys(commands).length] = {
            "command": file
         }
      }
   }

   return commands; //return full object with all commands
}

async function addCommandsFromSubFolder(commands, sub_directory) {
    try {
        files = await fs.promises.readdir(sub_directory);
        for (const command of commandsInSubFolder ) {
            commands[Object.keys(commands).length] = {
                "command": command
            }
        }
    } catch (e) {
        console.log('e', e);
    }
}

only you should attention to require fs like this (only node > 10):

const { promises: fs } = require("fs");

for node < 10 you should use promise to await

Upvotes: 2

Related Questions