changing the commands to ostly run server-side (NOT DONE))

a
This commit is contained in:
valentijn
2024-01-19 19:16:30 +01:00
parent 33db47102c
commit d0fb604680
4 changed files with 59 additions and 89 deletions

View File

@@ -1,34 +1,48 @@
function ls (req, res, dir, fs) {
dir = dir.startsWith('/') ? dir : '/' + dir;
dir = dir.endsWith('/') ? dir : dir + '/';
let currentState = {
"username": "guest",
"directory": "/"
}
const fileSystem = require("../filesystem.json")
function getCurrentDir(){
return currentState.directory;
}
function customTextResponse(res, txt){
respondToCommand(res, txt, currentState.directory, "");
}
const contents = fs[dir];
function ls(res) {
const command = "ls";
const contents = fileSystem[currentState.directory];
if (contents && typeof contents === 'object') {
let keys = Object.keys(contents)
res.json({"response": keys.join(" "), "command": "ls"});
respondToCommand(res, keys.join(" "), currentState.directory, command);
} else {
res.json({"response": "No Files Found!", "command": "ls"});
respondToCommand(res, "No Files Found In Current Directory", currentState.directory, command);
}
}
function whoami (req, res){
res.json({"response": "guest", "command": "whoami"});
}
function pwd (req, res, dir){
res.json({"response": `/home/guest${dir}`})
}
function help (req, res){
res.json({"response": "Here is a list of commands:<br><br>help: show this menu.<br>cd: open a directory, for example 'cd myFolder'.<br>ls: shows you a list of the files in the current directory.<br>whoami: shows you who you are logged in as.<br>pwd: shows you the current directory you are located in.", "command":"help"});
}
module.exports = {
ls,
whoami,
pwd,
help
getCurrentDir,
customTextResponse,
ls
}
function respondToCommand(res, text, directory, command){
res.json({
"username": "guest",
"directory": directory,
"response": text,
"command": command
})
}