Merge pull request #1932 from theit8514/cp-1922

Update cp and mv terminal commands #1922
This commit is contained in:
hydroflame 2021-12-16 12:14:15 -05:00 committed by GitHub
commit 0a63de4539
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 9 deletions

@ -169,6 +169,45 @@ export function getAllParentDirectories(path: string): string {
return t_path.slice(0, lastSlash + 1); return t_path.slice(0, lastSlash + 1);
} }
/**
* Given a destination that only contains a directory part, returns the
* path to the source filename inside the new destination directory.
* Otherwise, returns the path to the destination file.
* @param destination The destination path or file name
* @param source The source path
* @param cwd The current working directory
* @returns A file path which may be absolute or relative
*/
export function getDestinationFilepath(destination: string, source: string, cwd: string) {
const dstDir = evaluateDirectoryPath(destination, cwd);
// If evaluating the directory for this destination fails, we have a filename or full path.
if (dstDir === null) {
return destination;
} else {
// Append the filename to the directory provided.
let t_path = removeTrailingSlash(dstDir);
const fileName = getFileName(source);
return t_path + "/" + fileName;
}
}
/**
* Given a filepath, returns the file name (e.g. without directory parts)
* For example:
* /home/var/test.js -> test.js
* ./var/test.js -> test.js
* test.js -> test.js
*/
export function getFileName(path: string): string {
const t_path = path;
const lastSlash = t_path.lastIndexOf("/");
if (lastSlash === -1) {
return t_path;
}
return t_path.slice(lastSlash + 1);
}
/** /**
* Checks if a file path refers to a file in the root directory. * Checks if a file path refers to a file in the root directory.
*/ */

@ -3,6 +3,7 @@ import { IRouter } from "../../ui/Router";
import { IPlayer } from "../../PersonObjects/IPlayer"; import { IPlayer } from "../../PersonObjects/IPlayer";
import { BaseServer } from "../../Server/BaseServer"; import { BaseServer } from "../../Server/BaseServer";
import { isScriptFilename } from "../../Script/isScriptFilename"; import { isScriptFilename } from "../../Script/isScriptFilename";
import { getDestinationFilepath, areFilesEqual } from "../DirectoryHelpers";
export function cp( export function cp(
terminal: ITerminal, terminal: ITerminal,
@ -16,9 +17,23 @@ export function cp(
terminal.error("Incorrect usage of cp command. Usage: cp [src] [dst]"); terminal.error("Incorrect usage of cp command. Usage: cp [src] [dst]");
return; return;
} }
const src = args[0] + ""; // Convert a relative path source file to the absolute path.
const dst = args[1] + ""; const src = terminal.getFilepath(args[0] + "");
if (src === dst) { if (src === null) {
terminal.error("src cannot be a directory");
return;
}
// Get the destination based on the source file and the current directory
const t_dst = getDestinationFilepath(args[1] + "", src, terminal.cwd());
if (t_dst === null) {
terminal.error("error parsing dst file");
return;
}
// Convert a relative path destination file to the absolute path.
const dst = terminal.getFilepath(t_dst);
if (areFilesEqual(src, dst)) {
terminal.error("src and dst cannot be the same"); terminal.error("src and dst cannot be the same");
return; return;
} }
@ -50,7 +65,7 @@ export function cp(
const tRes = server.writeToTextFile(dst, txtFile.text); const tRes = server.writeToTextFile(dst, txtFile.text);
if (!tRes.success) { if (!tRes.success) {
terminal.error("scp failed"); terminal.error("cp failed");
return; return;
} }
if (tRes.overwritten) { if (tRes.overwritten) {
@ -71,13 +86,13 @@ export function cp(
} }
} }
if (sourceScript == null) { if (sourceScript == null) {
terminal.error("cp() failed. No such script exists"); terminal.error("cp failed. No such script exists");
return; return;
} }
const sRes = server.writeToScriptFile(dst, sourceScript.code); const sRes = server.writeToScriptFile(dst, sourceScript.code);
if (!sRes.success) { if (!sRes.success) {
terminal.error(`scp failed`); terminal.error(`cp failed`);
return; return;
} }
if (sRes.overwritten) { if (sRes.overwritten) {

@ -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;