mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-10-24 02:33:16 +02:00
25 lines
1.2 KiB
TypeScript
25 lines
1.2 KiB
TypeScript
|
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("."));
|
||
|
return /^\.exe-[0-9]{1-2}\.[0-9]{2}%-INC$/.test(extension);
|
||
|
}
|
||
|
|
||
|
/** 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.`);
|
||
|
}
|