55 lines
2.1 KiB
TypeScript
55 lines
2.1 KiB
TypeScript
import { Socket } from 'socket.io';
|
|
|
|
function cd(args:Array<string>, curdir:any, socket:Socket, filesystem:any, curdirx:Array<string>):string {
|
|
var buf = "";
|
|
if (args.length == 0) {
|
|
buf += "Please specify a directory.\n";
|
|
|
|
}
|
|
else {
|
|
if(args[0] == "..") {
|
|
//go up one directory
|
|
//recursively scan filesystem until we find curdir and then set curdir to its parent
|
|
//check curdirx is not empty
|
|
if (curdirx[socket.id].length > 0) {
|
|
//pop off last element
|
|
var sync = "filesystem[socket.id]";
|
|
for(let i = 0; i < curdirx[socket.id].length; i++) {
|
|
sync += "['" + curdirx[socket.id][i] + "']";
|
|
}
|
|
sync += " = curdir[socket.id];";
|
|
console.log(sync);
|
|
eval(sync);
|
|
curdirx[socket.id].pop();
|
|
//curdir = filesystem[socket.id][(curdirx[socket.id][0])][(curdirx[socket.id][1])].......;
|
|
//i know i shouldnt use eval but I don't know any other way to do this
|
|
var cmdexec = "curdir[socket.id] = filesystem[socket.id]";
|
|
//syncing = write curdir to a path in the filesystem
|
|
for (let i = 0; i < curdirx[socket.id].length; i++) {
|
|
cmdexec += "[(curdirx[socket.id][" + i + "])]";
|
|
}
|
|
eval(cmdexec);
|
|
}
|
|
}
|
|
else {
|
|
if (curdir[socket.id][args[0]] != undefined) {
|
|
//check if type dict
|
|
if (typeof curdir[socket.id][args[0]] == "object") {
|
|
curdir[socket.id] = curdir[socket.id][args[0]];
|
|
buf += "Changed directory to " + args[0] + "\n";
|
|
curdirx[socket.id].push(args[0]);
|
|
}
|
|
else{
|
|
buf += "cd: " + args[0] + ": Is not a directory\n";
|
|
}
|
|
|
|
}
|
|
else {
|
|
buf += "Directory " + args[0] + " does not exist.\n";
|
|
|
|
}
|
|
}
|
|
}
|
|
return buf;
|
|
}
|
|
export { cd }; |