From 8ef227595ad6cd4e491cb1da29282432c336040d Mon Sep 17 00:00:00 2001 From: theit8514 Date: Wed, 1 Dec 2021 12:27:39 -0500 Subject: [PATCH] Add support for relative paths in mv Fixes mv command deleting file on overwrite --- src/Terminal/commands/mv.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Terminal/commands/mv.ts b/src/Terminal/commands/mv.ts index 0ed723987..449f92cc8 100644 --- a/src/Terminal/commands/mv.ts +++ b/src/Terminal/commands/mv.ts @@ -5,6 +5,7 @@ import { BaseServer } from "../../Server/BaseServer"; import { isScriptFilename } from "../../Script/isScriptFilename"; import { TextFile } from "../../TextFile"; import { Script } from "../../Script/Script"; +import { getDestinationFilepath, areFilesEqual } from "../DirectoryHelpers"; export function mv( terminal: ITerminal, @@ -20,7 +21,7 @@ export function mv( try { const source = args[0] + ""; - const dest = args[1] + ""; + const t_dest = args[1] + ""; if (!isScriptFilename(source) && !source.endsWith(".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 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 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. // Also, you can't convert between different file types @@ -55,7 +66,7 @@ export function mv( if (destFile != null) { // Already exists, will be overwritten, so we'll delete it - const status = server.removeFile(destPath); + const status = server.removeFile(dest); if (!status.res) { terminal.error(`Something went wrong...please contact game dev (probably a bug)`); return;