Reputation: 7
I'm trying to include multiple folders in JavaScript, specific: Discord JS, here's my code:
const commandFiles = fs.readdirSync('./aPublic').filter(file => file.endsWith('.js')); // I wanted to include it here
for (const file of commandFiles) {
const command = require(`./aPublic/${file}`); // I wanted to include it here too!
client.commands.set(command.name, command);
}
I've tried to use ./aPublic && ./bAdmin
and also ('./aPublic') && ('./bAdmin')
but it only read the "bAdmin" folder rather than both of them, I wanted to include like 7 folders in both code.
Upvotes: 0
Views: 452
Reputation: 855
Let's put your list of directories in an array like below, use .map()
to read each of them, join the path of each file with its corresponding directory and flatten the result using .flat()
.
const arrDir = ['./aPublic', './bPublic'];
const commandFiles = arrDir
.map(dir => {
const listFiles = fs.readdirSync(dir);
return listFiles.map(file => path.join(__dirname, dir, file));
})
.flat()
.filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(file);
client.commands.set(command.name, command);
}
Upvotes: 1