blob: d8a12984335e9a86b18750106fc476849746de0a [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
15 subcommandGroups.push((await import(`../../../${path}/${file.name}/_meta.js`)).command);
16 } else if (file.name.endsWith(".js")) {
17 // If its a file
18 console.log(`│ ${indent}├─ Loading subcommand ${file.name}`)
19 subcommands.push((await import(`../../../${path}/${file.name}`)).command);
20 }
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}