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 = "") { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 4 | const files = fs |
| 5 | .readdirSync(path, { withFileTypes: true }) |
| 6 | .filter((file) => !file.name.endsWith(".ts") && !file.name.endsWith(".map")); |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 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 |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 15 | subcommandGroups.push(await import(`../../../${path}/${file.name}/_meta.js`)); |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 16 | } else if (file.name.endsWith(".js")) { |
| 17 | // If its a file |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 18 | console.log(`│ ${indent}├─ Loading subcommand ${file.name}`); |
PineaFan | df4996f | 2023-01-01 14:20:06 +0000 | [diff] [blame] | 19 | subcommands.push(await import(`../../../${path}/${file.name}`)); |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 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 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 26 | return { subcommands, subcommandGroups, errors }; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 27 | } |