blob: f5131bb3b34beed5f1fda3b1fbef367375d1da20 [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 = "") {
Skyler Greyda16adf2023-03-05 10:22:12 +00004 const files = fs
5 .readdirSync(path, { withFileTypes: true })
6 .filter((file) => !file.name.endsWith(".ts") && !file.name.endsWith(".map"));
PineaFan64486c42022-12-28 09:21:04 +00007 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
Skyler Greyda16adf2023-03-05 10:22:12 +000018 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 }
Skyler Greyda16adf2023-03-05 10:22:12 +000026 return { subcommands, subcommandGroups, errors };
PineaFan64486c42022-12-28 09:21:04 +000027}