diff --git a/commands/commands.js b/commands/commands.js
index 7d482da..c2ca8ab 100644
--- a/commands/commands.js
+++ b/commands/commands.js
@@ -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:
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,
- help
+ getCurrentDir,
+ customTextResponse,
+
+ ls
+}
+
+
+
+function respondToCommand(res, text, directory, command){
+ res.json({
+ "username": "guest",
+ "directory": directory,
+ "response": text,
+ "command": command
+ })
}
\ No newline at end of file
diff --git a/filesystem.json b/filesystem.json
new file mode 100644
index 0000000..b014d2a
--- /dev/null
+++ b/filesystem.json
@@ -0,0 +1,10 @@
+{
+ "/": {
+ "file1.txt": "this is the content of file1",
+ "projects": "dir"
+ },
+ "/projects/": {
+ "file2.txt": "this is file 2 :0",
+ "file3.txt": "and file 3 :D"
+ }
+}
\ No newline at end of file
diff --git a/httpdocs/public/js/terminal.js b/httpdocs/public/js/terminal.js
index 8597835..0fec9e3 100644
--- a/httpdocs/public/js/terminal.js
+++ b/httpdocs/public/js/terminal.js
@@ -1,54 +1,17 @@
const terminalContainer = document.getElementById("terminal");
const hostname = `192.168.${Math.floor(Math.random() * 100)}.${Math.floor(Math.random() * 255)}`;
-let currentFolder = "/";
-
-
document.addEventListener("DOMContentLoaded", function () {
-
document.querySelector(".pathText").innerHTML = `guest@${hostname}`;
-
addInputEventListeners();
-
});
async function sendCommand(e){
const command = e.target.value;
deactivateAllStuff();
- if (command.startsWith("cd")){
- let commandSplit = command.split(" ");
- 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();
- return;
- } catch {
- handleExecutionResponse({"response": "Invalid Argument"})
- return;
- }
- }
-
- if (command.startsWith("clear")){
+ if (command == "clear"){
lines = document.querySelectorAll(".terminalLine").forEach((line)=>{
line.remove();
})
diff --git a/server.js b/server.js
index f70f2c9..74234c6 100644
--- a/server.js
+++ b/server.js
@@ -8,17 +8,6 @@ app.use(express.static("httpdocs/public"))
const documentRoot = `${__dirname}/httpdocs`
-let fileSystem = {
- "/": {
- "file1.txt": "this is the content of file1",
- "projects": "dir"
- },
- "/projects/": {
- "file2.txt": "this is file 2 :0",
- "file3.txt": "and file 3 :D"
- }
-}
-
app.get('/', (req, res) => {
res.sendFile(documentRoot+"/index.html")
@@ -27,30 +16,24 @@ app.get('/', (req, res) => {
app.get("/execute", (req, res)=> {
const raw_command = req.query.command;
if (raw_command == ""){
- res.json({"response": ""});
+ commands.customTextResponse(res, "");
return;
}
- const formatted_command = String(raw_command).trim();
+ const formatted_command = raw_command.trim();
+ const command = formatted_command.split(" ")[0]
- const currentDir = req.query.currentDir
+ switch(command){
+ default:
+ commands.customTextResponse(res, "Command '"+command+"' not found, please try again later, or type 'help' for the list of available commands");
+ break;
+ case "ls":
+ commands.ls(res);
+ }
+})
- if (formatted_command.startsWith("ls")){
- commands.ls(req, res, currentDir, fileSystem);
- }
- else if (formatted_command.startsWith("whoami")){
- commands.whoami(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": "Unknown command, please try again later, or run 'help' for a list of commands"});
- }
+app.get("/currentDir", (req, res)=>{
+ res.send(commands.getCurrentDir());
})
app.listen(port, () => {