2021-09-16 01:50:44 +02:00
|
|
|
import { ITerminal } from "../ITerminal";
|
2021-09-17 08:58:02 +02:00
|
|
|
import { IRouter } from "../../ui/Router";
|
2021-09-16 01:50:44 +02:00
|
|
|
import { IPlayer } from "../../PersonObjects/IPlayer";
|
|
|
|
import { BaseServer } from "../../Server/BaseServer";
|
|
|
|
|
|
|
|
import { evaluateDirectoryPath, removeTrailingSlash } from "../DirectoryHelpers";
|
|
|
|
|
|
|
|
export function cd(
|
|
|
|
terminal: ITerminal,
|
2021-09-17 08:58:02 +02:00
|
|
|
router: IRouter,
|
2021-09-16 01:50:44 +02:00
|
|
|
player: IPlayer,
|
|
|
|
server: BaseServer,
|
2021-12-03 20:44:32 +01:00
|
|
|
args: (string | number | boolean)[],
|
2021-09-16 01:50:44 +02:00
|
|
|
): void {
|
|
|
|
if (args.length > 1) {
|
|
|
|
terminal.error("Incorrect number of arguments. Usage: cd [dir]");
|
|
|
|
} else {
|
|
|
|
let dir = args.length === 1 ? args[0] + "" : "/";
|
|
|
|
|
|
|
|
let evaledDir: string | null = "";
|
|
|
|
if (dir === "/") {
|
|
|
|
evaledDir = "/";
|
|
|
|
} else {
|
|
|
|
// Ignore trailing slashes
|
|
|
|
dir = removeTrailingSlash(dir);
|
|
|
|
|
|
|
|
evaledDir = evaluateDirectoryPath(dir, terminal.cwd());
|
|
|
|
if (evaledDir === null || evaledDir === "") {
|
|
|
|
terminal.error("Invalid path. Failed to change directories");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = player.getCurrentServer();
|
|
|
|
if (
|
|
|
|
!server.scripts.some((script) => script.filename.startsWith(evaledDir + "")) &&
|
|
|
|
!server.textFiles.some((file) => file.fn.startsWith(evaledDir + ""))
|
|
|
|
) {
|
|
|
|
terminal.error("Invalid path. Failed to change directories");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
terminal.setcwd(evaledDir);
|
|
|
|
}
|
|
|
|
}
|