简体   繁体   English

如何在 JavaScript 中包含多个文件夹?

[英]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.

相关问题 如何在PhantomJS中包含多个JavaScript库? - How to include multiple JavaScript libs in PhantomJS? 如何在一个JavaScript切换中包含多个div? - how include multiple divs in one javascript toggle? 如何使用 javascript 在 COUNT ROWS 中包含多个条件 - How include multiple criteria in COUNT ROWS with javascript 如何在Node.js的views文件夹中包含与View相关的javascript(* .js)文件? - How to include view related javascript (*.js) files in the views folder in nodejs? 如何在JavaScript中使用条件语句包含多种颜色样式 - How to include multiple color styles with a conditional statement in JavaScript 如何使用 JavaScript 包含/添加多个计算? - How do I include/add multiple calculations using JavaScript? 如何在多行 innerHTML 属性的 javascript 中包含 if 语句? - How to include an if statement within the javascript of a multiple line innerHTML property? 如何在.NET中包含多个Javascript文件(就像在rails中一样) - How to Include Multiple Javascript Files in .NET (Like they do in rails) javascript_include_tag包含特定文件夹中的所有文件 - javascript_include_tag include all Files from specific folder 在CoffeeScript中包含多个JavaScript文件 - Include multiple JavaScript files in CoffeeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM