diff --git a/commands/commands.js b/commands/commands.js
index 04cb466..60065f0 100644
--- a/commands/commands.js
+++ b/commands/commands.js
@@ -1,5 +1,18 @@
-function ls (req, res) {
- res.json({"response": "dit is totaal een reactie op een ls command :)", "command": "ls"});
+function ls (req, res, dir, fs) {
+ 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){
@@ -10,9 +23,14 @@ function pwd (req, res, dir){
res.json({"response": `/home/guest${dir}`})
}
+function help (req, res){
+ res.json({"response": "Here is a list of commands:
help: show this menu.
cd: open a directory, for example 'cd myFolder'.
ls: shows you a list of the files in the current directory.
whoami: shows you who you are logged in as.
pwd: shows you the current directory you are located in.", "command":"help"});
+}
+
module.exports = {
ls,
whoami,
- pwd
+ pwd,
+ help
}
\ No newline at end of file
diff --git a/httpdocs/public/js/terminal.js b/httpdocs/public/js/terminal.js
index 08df498..b867730 100644
--- a/httpdocs/public/js/terminal.js
+++ b/httpdocs/public/js/terminal.js
@@ -15,10 +15,51 @@ document.addEventListener("DOMContentLoaded", function () {
async function sendCommand(e){
const command = e.target.value;
- // e.target.value = "";
- // resizeInput();
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);
try {
@@ -65,11 +106,6 @@ function deactivateAllStuff() {
function handleExecutionResponse(response) {
- if (response.command === "cd"){
- currentFolder = response.directory
- }
-
-
//Show Response
let commandResponse = document.createElement("div")
commandResponse.classList.add("terminalLine");
diff --git a/server.js b/server.js
index e8bb6c9..c505f21 100644
--- a/server.js
+++ b/server.js
@@ -9,16 +9,14 @@ app.use(express.static("httpdocs/public"))
const documentRoot = `${__dirname}/httpdocs`
let fileSystem = {
- "guest": {
- "type": "directory",
- "contents": {
- "file.txt": {
- "type": "file",
- "content": "This is the text in the file
and I don't use \n to simulate a line break"
- }
- }
+ "/": {
+ "file1.txt": "this is the content of file1",
+ "projects": "dir"
+ },
+ "/projects/": {
+ "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)=> {
const raw_command = req.query.command;
+ if (raw_command == ""){
+ res.json({"response": ""});
+ return;
+ }
+
const formatted_command = String(raw_command).trim();
const currentDir = req.query.currentDir
@@ -35,7 +38,7 @@ app.get("/execute", (req, res)=> {
console.log(`Executed Command: ${formatted_command}`);
if (formatted_command.startsWith("ls")){
- commands.ls(req, res);
+ commands.ls(req, res, currentDir, fileSystem);
}
else if (formatted_command.startsWith("whoami")){
commands.whoami(req, res);
@@ -43,6 +46,10 @@ app.get("/execute", (req, res)=> {
else if (formatted_command.startsWith("pwd")){
commands.pwd(req, res, currentDir);
}
+ else if (formatted_command.startsWith("help"))
+ {
+ commands.help(req, res);
+ }
else {
res.json({"response": "invalid command"})
}