159 lines
5.1 KiB
JavaScript
159 lines
5.1 KiB
JavaScript
let currentState = {
|
|
"username": "guest",
|
|
"directory": "/"
|
|
}
|
|
|
|
const fileSystem = require("../filesystem.json")
|
|
|
|
function getCurrentDir(){
|
|
return currentState.directory;
|
|
}
|
|
|
|
function customTextResponse(res, txt){
|
|
respondToCommand(res, txt, currentState.directory, "");
|
|
}
|
|
|
|
|
|
function ls(res) {
|
|
const command = "ls";
|
|
const contents = fileSystem[currentState.directory];
|
|
|
|
if (contents && typeof contents === 'object') {
|
|
let keys = Object.keys(contents)
|
|
respondToCommand(res, keys.join(" "), currentState.directory, command);
|
|
} else {
|
|
respondToCommand(res, "No Files Found In Current Directory", currentState.directory, command);
|
|
}
|
|
}
|
|
|
|
function whoami(res){
|
|
const command = "whoami"
|
|
respondToCommand(res, currentState.username, currentState.directory, command);
|
|
}
|
|
|
|
function cd (res, raw_command) {
|
|
const command = "cd";
|
|
let arguement = raw_command.split(" ")[1]
|
|
|
|
//check if folder to cd to is in current dir
|
|
const contents = fileSystem[currentState.directory];
|
|
|
|
if (contents && typeof contents==="object"){
|
|
let keys = Object.keys(contents);
|
|
for (let i = 0; i < keys.length; i++){
|
|
let key = keys[i]
|
|
|
|
if (key == arguement && fileSystem[currentState.directory][key] == "dir"){
|
|
currentState.directory = currentState.directory + arguement;
|
|
|
|
if (!currentState.directory.endsWith("/")) currentState.directory += "/";
|
|
respondToCommand(res, "", currentState.directory, command);
|
|
return;
|
|
}
|
|
else if (key == arguement){
|
|
respondToCommand(res, "You cannot open a file as a directory", currentState.directory, command);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// check a few things:
|
|
if (arguement == "/" || arguement == "~" || arguement == "~/"){
|
|
currentState.directory = "/";
|
|
respondToCommand(res, "", currentState.directory, command);
|
|
return;
|
|
}
|
|
|
|
if (arguement == ".." || arguement == "../"){
|
|
//move 1 folder back
|
|
const lastSlashIndex = currentState.directory.lastIndexOf('/');
|
|
const pathWithoutTrailingSlash = lastSlashIndex === currentState.directory.length - 1 ? currentState.directory.substring(0, currentState.directory.length - 1) : currentState.directory;
|
|
const secondLastSlashIndex = pathWithoutTrailingSlash.lastIndexOf('/');
|
|
currentState.directory = pathWithoutTrailingSlash.substring(0, secondLastSlashIndex + 1);
|
|
|
|
respondToCommand(res, "", currentState.directory, command);
|
|
return;
|
|
}
|
|
|
|
respondToCommand(res, "Unable to open directory", currentState.directory, command);
|
|
}
|
|
else
|
|
{
|
|
respondToCommand(res, "Internal Server Error, try again later.", currentState.directory, command)
|
|
}
|
|
}
|
|
|
|
function pwd (res){
|
|
const command = "pwd";
|
|
respondToCommand(res, currentState.directory, currentState.directory, command);
|
|
}
|
|
|
|
function cat (res, raw_command){
|
|
const command = "cat";
|
|
let arguement = raw_command.split(" ")[1]
|
|
|
|
//check if folder to cd to is in current dir
|
|
const contents = fileSystem[currentState.directory];
|
|
|
|
if (contents && typeof contents==="object"){
|
|
let keys = Object.keys(contents);
|
|
for (let i = 0; i < keys.length; i++){
|
|
let key = keys[i]
|
|
|
|
if (key == arguement && fileSystem[currentState.directory][key] != "dir"){
|
|
respondToCommand(res, fileSystem[currentState.directory][key], currentState.directory, command);
|
|
return;
|
|
}
|
|
else if (key == arguement){
|
|
respondToCommand(res, "You cannot open a directory as a file", currentState.directory, command);
|
|
return;
|
|
}
|
|
}
|
|
respondToCommand(res, "Unable to open file", currentState.directory, command)
|
|
}
|
|
else
|
|
{
|
|
respondToCommand(res, "Internal Server Error, try again later.", currentState.directory, command)
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function help(res){
|
|
const command = "help"
|
|
const text = `
|
|
Welcome to the fancy help menu!!<br>
|
|
Here is a list of the available commands:<br><br>
|
|
|
|
"help" - shows you this menu<br>
|
|
"ls" - shows you the files and directories in your current directory<br>
|
|
"cd" - move into a folder you specify, you can use the name of the folder, or you can also use things like ../ to move back one folder<br>
|
|
"whoami" - shows you as what user you are logged in as<br>
|
|
"pwd" - shows you the current directory you are currently working in<br>
|
|
"cat" - shows you the content of files, fox example cat file.txt shows you the content of file.txt if it is in the current directory<br>
|
|
`
|
|
respondToCommand(res, text, currentState.directory, command);
|
|
}
|
|
|
|
module.exports = {
|
|
getCurrentDir,
|
|
customTextResponse,
|
|
|
|
ls,
|
|
whoami,
|
|
cd,
|
|
pwd,
|
|
cat,
|
|
help,
|
|
}
|
|
|
|
|
|
|
|
function respondToCommand(res, text, directory, command){
|
|
res.json({
|
|
"username": "guest",
|
|
"directory": directory,
|
|
"response": text,
|
|
"command": command
|
|
})
|
|
} |