Skip to content

Commit 5e01d36

Browse files
committed
feat: revise command loader
1 parent 59116a1 commit 5e01d36

4 files changed

Lines changed: 35 additions & 19 deletions

File tree

src/commands/reload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import log from "../components/utils/log";
44
import fs from "fs";
55
import path from "path";
66
import { commands } from "../index";
7-
import Loader from "../components/utils/loader";
7+
import Loader from "../components/utils/cmd/loader";
88

99
export const info = {
1010
command: "reload",
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import log from "./log";
2-
import fs from "fs";
1+
import log from "../log";
2+
import { promises as fs } from "fs";
33
import path from "path";
4-
import { commands } from "../../index";
4+
import { commands } from "../../../index";
55

6-
const commandsPath = path.join(__dirname, "..", "..", "commands");
6+
const commandsPath = path.join(__dirname, "..", "..", "..", "commands");
77

8-
export default function (file: string, customPath?: string) {
8+
export default function loader(file: string, customPath?: string) {
99
if (/\.js$|\.ts$/.test(file)) {
1010
const filePath = path.join(customPath || commandsPath, file);
1111

@@ -32,3 +32,9 @@ export default function (file: string, customPath?: string) {
3232
}
3333
}
3434
}
35+
36+
export async function mapCommands() {
37+
const files = await fs.readdir(commandsPath);
38+
39+
Promise.all(files.map((file) => loader(file)));
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { watch } from "fs/promises";
2+
import loader from "./loader";
3+
import log from "../log";
4+
5+
export default async function (commandsPath: string) {
6+
const watcher = watch(commandsPath, { recursive: false });
7+
8+
for await (const event of watcher) {
9+
const { eventType, filename } = event;
10+
if (filename && /\.(js|ts)$/.test(filename)) {
11+
try {
12+
await loader(filename);
13+
log.info("Loader", `Reloaded command: ${filename}`);
14+
} catch (err) {
15+
log.error("Loader", `Failed to reload command: ${filename}`, err);
16+
}
17+
}
18+
}
19+
}

src/index.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import dotenv from "dotenv";
22
dotenv.config();
33

44
import { Message } from "whatsapp-web.js";
5-
import fs from "fs";
65
import path from "path";
76
import log from "./components/utils/log";
8-
import loader from "./components/utils/loader";
7+
import loader, { mapCommands } from "./components/utils/cmd/loader";
8+
import watcher from "./components/utils/cmd/watcher";
99
import "./components/process";
1010
import server from "./components/server";
1111
import { client } from "./components/client";
@@ -40,19 +40,10 @@ const commands: Record<
4040
}
4141
> = {};
4242

43-
fs.readdirSync(commandsPath).forEach((file: string) => loader(file));
43+
mapCommands();
4444

4545
// Watch for changes
46-
if (autoReload)
47-
fs.watch(commandsPath, (eventType: string, filename: string | null) => {
48-
if (filename && /\.js$|\.ts$/.test(filename)) {
49-
try {
50-
loader(filename);
51-
} catch (err) {
52-
log.error("Loader", `Failed to reload command: ${filename}`, err);
53-
}
54-
}
55-
});
46+
if (autoReload) watcher(commandsPath);
5647

5748
export {
5849
commands,

0 commit comments

Comments
 (0)