-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.js
More file actions
36 lines (30 loc) · 1.13 KB
/
commands.js
File metadata and controls
36 lines (30 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const commandMap = {}; // { help: handlerFn, ... }
const commandDescriptions = {}; // { help: "description", ... }
const loadCommand = (commandName) => {
return import(`./commands/${commandName}.js`)
.then((commandModule) => {
commandMap[commandName] = commandModule.default;
commandDescriptions[commandName] = commandModule.description || 'No description available.';
})
.catch((err) => {
console.error(`Failed to load command ${commandName}:`, err);
});
};
const commandNames = ['help', 'about', 'clear', 'ls', 'cd', 'cat', 'info', 'blog'];
Promise.all(commandNames.map(loadCommand))
.then(() => {
console.log("All commands loaded successfully.");
})
.catch((err) => {
console.error("Error loading commands:", err);
});
export function runCommand(cmdLine, username, hostname, write, env) {
const [cmd, ...args] = cmdLine.split(' ');
const command = commandMap[cmd];
if (command) {
command(write, args, env);
} else if (cmd !== '') {
write(`${cmd}: command not found`);
}
}
export { commandDescriptions };