[英]How to include multiple folder in JavaScript?
I'm trying to include multiple folders in JavaScript, specific: Discord JS, here's my code:我正在尝试在 JavaScript 中包含多个文件夹,具体为:Discord JS,这是我的代码:
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.我尝试使用
./aPublic &&./bAdmin
和('./aPublic') && ('./bAdmin')
但它只读取“bAdmin”文件夹而不是它们两个,我想包括两个代码中有 7 个文件夹。
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()
.让我们将您的目录列表放在一个数组中,如下所示,使用
.map()
读取每个目录,将每个文件的路径与其对应的目录连接起来,并使用.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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.