added some more commands, chnaged filesystem layout, and added a help command :)
This commit is contained in:
@@ -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:<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
|
||||
pwd,
|
||||
help
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
27
server.js
27
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 <br> 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"})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user