blob: 875e0b04f50f6e8682f33f202e8b4b6f25fec1f2 [file] [log] [blame]
PineaFan64486c42022-12-28 09:21:04 +00001import fs from "fs";
2
PineaFan752af462022-12-31 21:59:38 +00003export default async function getSubcommandsInFolder(path: string, indent: string = "") {
PineaFan64486c42022-12-28 09:21:04 +00004 const files = fs.readdirSync(path, { withFileTypes: true }).filter(
5 file => !file.name.endsWith(".ts") && !file.name.endsWith(".map")
6 );
7 const subcommands = [];
8 const subcommandGroups = [];
PineaFan752af462022-12-31 21:59:38 +00009 let errors = 0;
PineaFan64486c42022-12-28 09:21:04 +000010 for (const file of files) {
11 if (file.name === "_meta.js") continue;
PineaFan752af462022-12-31 21:59:38 +000012 try {
13 if (file.isDirectory()) {
14 // Get the _meta.ts file
PineaFandf4996f2023-01-01 14:20:06 +000015 subcommandGroups.push(await import(`../../../${path}/${file.name}/_meta.js`));
PineaFan752af462022-12-31 21:59:38 +000016 } else if (file.name.endsWith(".js")) {
17 // If its a file
18 console.log(`│ ${indent}├─ Loading subcommand ${file.name}`)
PineaFandf4996f2023-01-01 14:20:06 +000019 subcommands.push(await import(`../../../${path}/${file.name}`));
PineaFan752af462022-12-31 21:59:38 +000020 }
21 } catch (e) {
22 console.error(`│ ${indent}│ └─ Error loading ${file.name}: ${e}`);
23 errors++;
PineaFan64486c42022-12-28 09:21:04 +000024 }
25 }
PineaFan752af462022-12-31 21:59:38 +000026 return {subcommands, subcommandGroups, errors};
PineaFan64486c42022-12-28 09:21:04 +000027}