bitburner-src/src/Terminal/DirectoryServerHelpers.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

/**
* Helper functions that implement "directory" functionality in the Terminal.
* These aren't "real" directories, it's more of a pseudo-directory implementation
* that uses mainly string manipulation.
*
* This file contains function that deal with Server-related directory things.
* Functions that deal with the string manipulation can be found in
* ./DirectoryHelpers.ts
*/
2021-09-09 05:47:34 +02:00
import { isValidDirectoryPath, isInRootDirectory, getFirstParentDirectory } from "./DirectoryHelpers";
import { BaseServer } from "../Server/BaseServer";
/**
* Given a directory (by the full directory path) and a server, returns all
* subdirectories of that directory. This is only for FIRST-LEVEl/immediate subdirectories
*/
export function getSubdirectories(serv: BaseServer, dir: string): string[] {
2021-09-05 01:09:30 +02:00
const res: string[] = [];
2021-09-05 01:09:30 +02:00
if (!isValidDirectoryPath(dir)) {
return res;
}
2021-09-05 01:09:30 +02:00
let t_dir = dir;
if (!t_dir.endsWith("/")) {
t_dir += "/";
}
2021-09-05 01:09:30 +02:00
function processFile(fn: string): void {
if (t_dir === "/" && isInRootDirectory(fn)) {
const subdir = getFirstParentDirectory(fn);
if (subdir !== "/" && !res.includes(subdir)) {
res.push(subdir);
}
} else if (fn.startsWith(t_dir)) {
const remaining = fn.slice(t_dir.length);
const subdir = getFirstParentDirectory(remaining);
if (subdir !== "/" && !res.includes(subdir)) {
res.push(subdir);
}
}
2021-09-05 01:09:30 +02:00
}
2021-09-05 01:09:30 +02:00
for (const script of serv.scripts) {
processFile(script.filename);
}
2021-09-05 01:09:30 +02:00
for (const txt of serv.textFiles) {
processFile(txt.fn);
}
2021-09-05 01:09:30 +02:00
return res;
}