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 16:26:57 +02:00
|
|
|
import { Directory, isAbsolutePath } from "./Directory";
|
|
|
|
import { FilePath, isFilePath, resolveFilePath } from "./FilePath";
|
|
|
|
|
|
|
|
/** Filepath with the additional constraint of having a .cct extension */
|
|
|
|
type WithProgramExtension = string & { __fileType: "Program" };
|
|
|
|
export type ProgramFilePath = FilePath & WithProgramExtension;
|
|
|
|
|
|
|
|
/** Check extension only. Programs are a bit different than others because of incomplete programs. */
|
|
|
|
export function hasProgramExtension(path: string): path is WithProgramExtension {
|
|
|
|
if (path.endsWith(".exe")) return true;
|
|
|
|
const extension = path.substring(path.indexOf("."));
|
2023-04-28 17:42:35 +02:00
|
|
|
return /^\.exe-[0-9]{1,2}\.[0-9]{2}%-INC$/.test(extension);
|
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 16:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sanitize a player input, resolve any relative paths, and for imports add the correct extension if missing */
|
|
|
|
export function resolveProgramFilePath(path: string, base = "" as FilePath | Directory): ProgramFilePath | null {
|
|
|
|
const result = resolveFilePath(path, base);
|
|
|
|
return result && hasProgramExtension(result) ? result : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function asProgramFilePath<T extends string>(path: T): T & ProgramFilePath {
|
|
|
|
if (isFilePath(path) && hasProgramExtension(path) && isAbsolutePath(path)) return path;
|
|
|
|
throw new Error(`${path} failed to validate as a ProgramFilePath.`);
|
|
|
|
}
|