blob: f5131bb3b34beed5f1fda3b1fbef367375d1da20 [file] [log] [blame]
import fs from "fs";
export default async function getSubcommandsInFolder(path: string, indent: string = "") {
const files = fs
.readdirSync(path, { withFileTypes: true })
.filter((file) => !file.name.endsWith(".ts") && !file.name.endsWith(".map"));
const subcommands = [];
const subcommandGroups = [];
let errors = 0;
for (const file of files) {
if (file.name === "_meta.js") continue;
try {
if (file.isDirectory()) {
// Get the _meta.ts file
subcommandGroups.push(await import(`../../../${path}/${file.name}/_meta.js`));
} else if (file.name.endsWith(".js")) {
// If its a file
console.log(`│ ${indent}├─ Loading subcommand ${file.name}`);
subcommands.push(await import(`../../../${path}/${file.name}`));
}
} catch (e) {
console.error(`│ ${indent}│ └─ Error loading ${file.name}: ${e}`);
errors++;
}
}
return { subcommands, subcommandGroups, errors };
}