bitburner-src/src/Paths/GlobbedFiles.ts
Snarling e0272ad4af
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 10:26:57 -04:00

41 lines
1.7 KiB
TypeScript

import { BaseServer } from "../Server/BaseServer";
import { root } from "./Directory";
import { ContentFileMap, allContentFiles } from "./ContentFile";
/** Search for files (Script and TextFile only) that match a given glob pattern
* @param pattern The glob pattern. Supported glob characters are * and ?
* @param server The server to search using the pattern
* @param currentDir The base directory. Optional, defaults to root. Also forced to root if the pattern starts with /
* @returns A map keyed by paths (ScriptFilePath or TextFilePath) with files as values (Script or TextFile). */
export function getGlobbedFileMap(pattern: string, server: BaseServer, currentDir = root): ContentFileMap {
const map: ContentFileMap = new Map();
// A pattern starting with / indicates wanting to match things from root directory instead of current.
if (pattern.startsWith("/")) {
currentDir = root;
pattern = pattern.substring(1);
}
// Only search within the current directory
pattern = currentDir + pattern;
// This globbing supports * and ?.
// * will be turned into regex .*
// ? will be turned into regex .
// All other special regex characters in the pattern will need to be escaped out.
const charsToEscape = new Set(["/", "\\", "^", "$", ".", "|", "+", "(", ")", "[", "{"]);
pattern = pattern
.split("")
.map((char) => {
if (char === "*") return ".*";
if (char === "?") return ".";
if (charsToEscape.has(char)) return "\\" + char;
return char;
})
.join("");
const regex = new RegExp(`^${pattern}$`);
for (const [path, file] of allContentFiles(server)) {
if (regex.test(path)) map.set(path, file);
}
return map;
}