PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 1 | import fs from "fs"; |
| 2 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame^] | 3 | export default async function getSubcommandsInFolder(path: string, indent: string = "") { |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 4 | 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 = []; |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame^] | 9 | let errors = 0; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 10 | for (const file of files) { |
| 11 | if (file.name === "_meta.js") continue; |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame^] | 12 | 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++; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 24 | } |
| 25 | } |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame^] | 26 | return {subcommands, subcommandGroups, errors}; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 27 | } |