added some more commands, chnaged filesystem layout, and added a help command :)

This commit is contained in:
valentijn
2024-01-19 11:48:07 +01:00
parent e4be64245b
commit 73d455d4bd
3 changed files with 81 additions and 20 deletions

View File

@@ -1,5 +1,18 @@
function ls (req, res) { function ls (req, res, dir, fs) {
res.json({"response": "dit is totaal een reactie op een ls command :)", "command": "ls"}); dir = dir.startsWith('/') ? dir : '/' + dir;
dir = dir.endsWith('/') ? dir : dir + '/';
console.log(dir);
const contents = fs[dir];
if (contents && typeof contents === 'object') {
let keys = Object.keys(contents)
console.log(keys);
res.json({"response": keys.join(" "), "command": "ls"});
} else {
res.json({"response": "No Files Found!", "command": "ls"});
}
} }
function whoami (req, res){ function whoami (req, res){
@@ -10,9 +23,14 @@ function pwd (req, res, dir){
res.json({"response": `/home/guest${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 = { module.exports = {
ls, ls,
whoami, whoami,
pwd pwd,
help
} }

View File

@@ -15,10 +15,51 @@ document.addEventListener("DOMContentLoaded", function () {
async function sendCommand(e){ async function sendCommand(e){
const command = e.target.value; const command = e.target.value;
// e.target.value = "";
// resizeInput();
deactivateAllStuff(); deactivateAllStuff();
if (command.startsWith("cd")){
let commandSplit = command.split(" ");
console.log(commandSplit)
try {
if (commandSplit[1].startsWith("/")){
currentFolder = commandSplit[1];
if (!commandSplit[1].endsWith("/")){
currentFolder += "/"
}
}
else if (commandSplit[1].startsWith("~")){
currentFolder = "/";
}
else if (commandSplit[1].startsWith("./")) {
currentFolder += commandSplit[1].slice(2);
if (!commandSplit[1].endsWith("/")){
currentFolder += "/"
}
} else {
currentFolder += commandSplit[1];
if (!commandSplit[1].endsWith("/")){
currentFolder += "/"
}
}
createNewTerminalLine();
addInputEventListeners();
return;
} catch {
handleExecutionResponse({"response": "Invalid Argument"})
return;
}
}
if (command.startsWith("clear")){
lines = document.querySelectorAll(".terminalLine").forEach((line)=>{
line.remove();
})
createNewTerminalLine();
addInputEventListeners();
return;
}
console.log("COMMAND SENT: "+command); console.log("COMMAND SENT: "+command);
try { try {
@@ -65,11 +106,6 @@ function deactivateAllStuff() {
function handleExecutionResponse(response) { function handleExecutionResponse(response) {
if (response.command === "cd"){
currentFolder = response.directory
}
//Show Response //Show Response
let commandResponse = document.createElement("div") let commandResponse = document.createElement("div")
commandResponse.classList.add("terminalLine"); commandResponse.classList.add("terminalLine");

View File

@@ -9,16 +9,14 @@ app.use(express.static("httpdocs/public"))
const documentRoot = `${__dirname}/httpdocs` const documentRoot = `${__dirname}/httpdocs`
let fileSystem = { let fileSystem = {
"guest": { "/": {
"type": "directory", "file1.txt": "this is the content of file1",
"contents": { "projects": "dir"
"file.txt": { },
"type": "file", "/projects/": {
"content": "This is the text in the file <br> and I don't use \n to simulate a line break" "file2.txt": "this is file 2 :0",
} "file3.txt": "and file 3 :D"
}
} }
} }
@@ -28,6 +26,11 @@ app.get('/', (req, res) => {
app.get("/execute", (req, res)=> { app.get("/execute", (req, res)=> {
const raw_command = req.query.command; const raw_command = req.query.command;
if (raw_command == ""){
res.json({"response": ""});
return;
}
const formatted_command = String(raw_command).trim(); const formatted_command = String(raw_command).trim();
const currentDir = req.query.currentDir const currentDir = req.query.currentDir
@@ -35,7 +38,7 @@ app.get("/execute", (req, res)=> {
console.log(`Executed Command: ${formatted_command}`); console.log(`Executed Command: ${formatted_command}`);
if (formatted_command.startsWith("ls")){ if (formatted_command.startsWith("ls")){
commands.ls(req, res); commands.ls(req, res, currentDir, fileSystem);
} }
else if (formatted_command.startsWith("whoami")){ else if (formatted_command.startsWith("whoami")){
commands.whoami(req, res); commands.whoami(req, res);
@@ -43,6 +46,10 @@ app.get("/execute", (req, res)=> {
else if (formatted_command.startsWith("pwd")){ else if (formatted_command.startsWith("pwd")){
commands.pwd(req, res, currentDir); commands.pwd(req, res, currentDir);
} }
else if (formatted_command.startsWith("help"))
{
commands.help(req, res);
}
else { else {
res.json({"response": "invalid command"}) res.json({"response": "invalid command"})
} }