Add support for relative paths in mv

Fixes mv command deleting file on overwrite
This commit is contained in:
theit8514 2021-12-01 12:27:39 -05:00
parent 4e022c68e9
commit 8ef227595a

@ -5,6 +5,7 @@ import { BaseServer } from "../../Server/BaseServer";
import { isScriptFilename } from "../../Script/isScriptFilename"; import { isScriptFilename } from "../../Script/isScriptFilename";
import { TextFile } from "../../TextFile"; import { TextFile } from "../../TextFile";
import { Script } from "../../Script/Script"; import { Script } from "../../Script/Script";
import { getDestinationFilepath, areFilesEqual } from "../DirectoryHelpers";
export function mv( export function mv(
terminal: ITerminal, terminal: ITerminal,
@ -20,7 +21,7 @@ export function mv(
try { try {
const source = args[0] + ""; const source = args[0] + "";
const dest = args[1] + ""; const t_dest = args[1] + "";
if (!isScriptFilename(source) && !source.endsWith(".txt")) { if (!isScriptFilename(source) && !source.endsWith(".txt")) {
terminal.error(`'mv' can only be used on scripts and text files (.txt)`); terminal.error(`'mv' can only be used on scripts and text files (.txt)`);
@ -34,9 +35,19 @@ export function mv(
} }
const sourcePath = terminal.getFilepath(source); const sourcePath = terminal.getFilepath(source);
const destPath = terminal.getFilepath(dest); // Get the destination based on the source file and the current directory
const dest = getDestinationFilepath(t_dest, source, terminal.cwd());
if (dest === null) {
terminal.error("error parsing dst file");
return;
}
const destFile = terminal.getFile(player, dest); const destFile = terminal.getFile(player, dest);
const destPath = terminal.getFilepath(dest);
if (areFilesEqual(sourcePath, destPath)) {
terminal.error(`Source and destination files are the same file`);
return;
}
// 'mv' command only works on scripts and txt files. // 'mv' command only works on scripts and txt files.
// Also, you can't convert between different file types // Also, you can't convert between different file types
@ -55,7 +66,7 @@ export function mv(
if (destFile != null) { if (destFile != null) {
// Already exists, will be overwritten, so we'll delete it // Already exists, will be overwritten, so we'll delete it
const status = server.removeFile(destPath); const status = server.removeFile(dest);
if (!status.res) { if (!status.res) {
terminal.error(`Something went wrong...please contact game dev (probably a bug)`); terminal.error(`Something went wrong...please contact game dev (probably a bug)`);
return; return;