Files
bitburner-src/src/Terminal/commands/cd.ts
T
Snarling 40b89baca1 MISC: Various small fixes (#574)
* ns.ls filter can include leading slash in filename
* scp from terminal accepts multiple filenames
* terminal displays root / instead of ~ as base
* cd with no args returns to root
2023-06-06 08:46:07 -04:00

15 lines
807 B
TypeScript

import { Terminal } from "../../Terminal";
import { BaseServer } from "../../Server/BaseServer";
import { directoryExistsOnServer, resolveDirectory } from "../../Paths/Directory";
export function cd(args: (string | number | boolean)[], server: BaseServer): void {
if (args.length > 1) return Terminal.error("Incorrect number of arguments. Usage: cd [dir]");
// If no arg was provided, just use "/".
const userInput = String(args[0] ?? "/");
const targetDir = resolveDirectory(userInput, Terminal.currDir);
// Explicitly checking null due to root being ""
if (targetDir === null) return Terminal.error(`Could not resolve directory ${userInput}`);
if (!directoryExistsOnServer(targetDir, server)) return Terminal.error(`Directory ${targetDir} does not exist.`);
Terminal.setcwd(targetDir);
}