Saya mencoba menambahkan nama file dari direktori dengan sub-direktori ke objek.

Folder saya terstruktur seperti yang ditunjukkan gambar berikut:

enter image description here

Setiap kali saya menjalankan kode saya, hanya nama file (perintah di sini) di Folder Atas yang ditambahkan ke objek saya.

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
         }
      }
   });
}

Jika saya log commands di addCommandsFromSubFolder, saya masih dapat melihat, bahwa file dari semua subfolder masih ditambahkan, tetapi node tidak akan menunggu untuk ditambahkan.

0
Christoph Blüm 25 Mei 2021, 16:31

1 menjawab

Jawaban Terbaik

Anda harus menunggu panggilan fungsi untuk 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);
    }
}

Hanya Anda yang harus memperhatikan memerlukan fs seperti ini (hanya simpul> 10):

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

Untuk simpul <10 Anda harus menggunakan janji untuk menunggu

1
Mahdi Zarrintareh 25 Mei 2021, 13:50