Skip to content

Commit

Permalink
Resolves debug mod loading
Browse files Browse the repository at this point in the history
The `getFiles` and `getDirectories` methods now include a `includeInputDir` parameter which optionally includes/excludes the `directory` path in the returned paths.
  • Loading branch information
refringe committed Jan 9, 2025
1 parent 85be13c commit 19f8efd
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion project/src/loaders/PreSptModLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export class PreSptModLoader implements IModLoader {
protected async addModAsync(mod: string, pkg: IPackageJsonData): Promise<void> {
const modPath = this.getModPath(mod);

const typeScriptFiles = this.fileSystemSync.getFiles(`${modPath}src`, true, ["ts"]);
const typeScriptFiles = this.fileSystemSync.getFiles(`${modPath}src`, true, ["ts"], true);

if (typeScriptFiles.length > 0) {
if (ProgramStatics.COMPILED) {
Expand Down
2 changes: 1 addition & 1 deletion project/src/servers/ConfigServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class ConfigServer {

// Get all filepaths
const filepath = ProgramStatics.COMPILED ? "SPT_Data/Server/configs/" : "./assets/configs/";
const files = this.fileSystemSync.getFiles(filepath, true, this.acceptableFileExtensions);
const files = this.fileSystemSync.getFiles(filepath, true, this.acceptableFileExtensions, true);

// Add file content to result
for (const file of files) {
Expand Down
4 changes: 2 additions & 2 deletions project/src/services/BackupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class BackupService {
// Fetch all profiles in the profile directory.
let currentProfiles: string[] = [];
try {
currentProfiles = await this.fileSystem.getFiles(this.profileDir, false, ["json"]);
currentProfiles = await this.fileSystem.getFiles(this.profileDir, false, ["json"], true);
} catch (error) {
this.logger.debug("Skipping profile backup: Unable to read profiles directory");
return;
Expand Down Expand Up @@ -147,7 +147,7 @@ export class BackupService {
* @returns A promise that resolves to an array of sorted backup file paths.
*/
private async getBackupPaths(dir: string): Promise<string[]> {
const backups = await this.fileSystem.getFiles(dir, false, ["json"]);
const backups = await this.fileSystem.getFiles(dir, false, ["json"], true);
return backups.sort(this.compareBackupDates.bind(this));
}

Expand Down
4 changes: 2 additions & 2 deletions project/src/utils/DatabaseImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class DatabaseImporter implements OnLoad {
await this.hydrateDatabase(this.filepath);

const imageFilePath = `${this.filepath}images/`;
const directories = await this.fileSystem.getDirectories(imageFilePath, true);
const directories = await this.fileSystem.getDirectories(imageFilePath);
await this.loadImagesAsync(imageFilePath, directories, [
"/files/achievement/",
"/files/CONTENT/banners/",
Expand Down Expand Up @@ -145,7 +145,7 @@ export class DatabaseImporter implements OnLoad {
public async loadImagesAsync(filepath: string, directories: string[], routes: string[]): Promise<void> {
for (const directoryIndex in directories) {
// Get all files in directory
const filesInDirectory = await this.fileSystem.getFiles(`${filepath}${directories[directoryIndex]}`, true);
const filesInDirectory = await this.fileSystem.getFiles(`${filepath}${directories[directoryIndex]}`);
for (const file of filesInDirectory) {
// Register each file in image router
const filename = FileSystem.stripExtension(file);
Expand Down
31 changes: 25 additions & 6 deletions project/src/utils/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,21 @@ export class FileSystem {
* @param directory The directory to get files from.
* @param searchRecursive Whether to search recursively.
* @param fileTypes An optional array of file extensions to filter by (without the dot).
* @param includeInputDir If true, the returned paths will include the directory parameter path. If false, the paths
* will begin from within the directory parameter path. Default false.
* @returns A promise that resolves with an array of file paths.
*/
public async getFiles(directory: string, searchRecursive = false, fileTypes?: string[]): Promise<string[]> {
public async getFiles(
directory: string,
searchRecursive = false,
fileTypes?: string[],
includeInputDir = false,
): Promise<string[]> {
if (!(await fsExtra.pathExists(directory))) {
return [];
}
const dirents = await fsExtra.readdir(directory, { withFileTypes: true, recursive: searchRecursive });
const directoryNormalized = path.normalize(directory).replace(/\\/g, "/");
const dirents = await fsExtra.readdir(directoryNormalized, { withFileTypes: true, recursive: searchRecursive });
return (
dirents
// Filter out anything that isn't a file.
Expand All @@ -320,6 +328,8 @@ export class FileSystem {
})
// Join and normalize the input directory and dirent.name to use forward slashes.
.map((dirent) => path.join(dirent.parentPath, dirent.name).replace(/\\/g, "/"))
// Optionally remove the input directory from the path.
.map((dir) => (includeInputDir ? dir : dir.replace(directoryNormalized, "")))
);
}

Expand All @@ -329,20 +339,29 @@ export class FileSystem {
* Will always return paths with forward slashes.
*
* @param directory The directory to get directories from.
* @param searchRecursive Whether to search recursively.
* @param searchRecursive Whether to search recursively. Default false.
* @param includeInputDir If true, the returned paths will include the directory parameter path. If false, the paths
* will begin from within the directory parameter path. Default false.
* @returns A promise that resolves with an array of directory paths.
*/
public async getDirectories(directory: string, searchRecursive = false): Promise<string[]> {
public async getDirectories(
directory: string,
searchRecursive = false,
includeInputDir = false,
): Promise<string[]> {
if (!(await fsExtra.pathExists(directory))) {
return [];
}
const dirents = await fsExtra.readdir(directory, { withFileTypes: true, recursive: searchRecursive });
const directoryNormalized = path.normalize(directory).replace(/\\/g, "/");
const dirents = await fsExtra.readdir(directoryNormalized, { withFileTypes: true, recursive: searchRecursive });
return (
dirents
// Filter out anything that isn't a directory.
.filter((dirent) => dirent.isDirectory())
// Join and normalize the input directory and dirent.name to use forward slashes.
.map((dirent) => path.join(directory, dirent.name).replace(/\\/g, "/"))
.map((dirent) => path.join(dirent.parentPath, dirent.name).replace(/\\/g, "/"))
// Optionally remove the input directory from the path.
.map((dir) => (includeInputDir ? dir : dir.replace(directoryNormalized, "")))
);
}
}
25 changes: 20 additions & 5 deletions project/src/utils/FileSystemSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,20 @@ export class FileSystemSync {
* @param directory The directory to get files from.
* @param searchRecursive Whether to search recursively.
* @param fileTypes An optional array of file extensions to filter by (without the dot).
* @param includeInputDir If true, the returned paths will include the directory parameter path. If false, the paths
* will begin from within the directory parameter path. Default false.
* @returns An array of file paths.
*/
public getFiles(directory: string, searchRecursive = false, fileTypes?: string[]): string[] {
public getFiles(
directory: string,
searchRecursive = false,
fileTypes?: string[],
includeInputDir = false,
): string[] {
if (!fsExtra.pathExistsSync(directory)) {
return [];
}
const directoryNormalized = path.normalize(directory).replace(/\\/g, "/");
const dirents = fsExtra.readdirSync(directory, { withFileTypes: true, recursive: searchRecursive });
return (
dirents
Expand All @@ -314,6 +322,8 @@ export class FileSystemSync {
})
// Join and normalize the input directory and dirent.name to use forward slashes.
.map((dirent) => path.join(dirent.parentPath, dirent.name).replace(/\\/g, "/"))
// Optionally remove the input directory from the path.
.map((dir) => (includeInputDir ? dir : dir.replace(directoryNormalized, "")))
);
}

Expand All @@ -323,20 +333,25 @@ export class FileSystemSync {
* Will always return paths with forward slashes.
*
* @param directory The directory to get directories from.
* @param searchRecursive Whether to search recursively.
* @param searchRecursive Whether to search recursively. Default false.
* @param includeInputDir If true, the returned paths will include the directory parameter path. If false, the paths
* will begin from within the directory parameter path. Default false.
* @returns An array of directory paths.
*/
public getDirectories(directory: string, searchRecursive = false): string[] {
public getDirectories(directory: string, searchRecursive = false, includeInputDir = false): string[] {
if (!fsExtra.pathExistsSync(directory)) {
return [];
}
const dirents = fsExtra.readdirSync(directory, { withFileTypes: true, recursive: searchRecursive });
const directoryNormalized = path.normalize(directory).replace(/\\/g, "/");
const dirents = fsExtra.readdirSync(directoryNormalized, { withFileTypes: true, recursive: searchRecursive });
return (
dirents
// Filter out anything that isn't a directory.
.filter((dirent) => dirent.isDirectory())
// Join and normalize the input directory and dirent.name to use forward slashes.
.map((dirent) => path.join(directory, dirent.name).replace(/\\/g, "/"))
.map((dirent) => path.join(dirent.parentPath, dirent.name).replace(/\\/g, "/"))
// Optionally remove the input directory from the path.
.map((dir) => (includeInputDir ? dir : dir.replace(directoryNormalized, "")))
);
}
}
3 changes: 2 additions & 1 deletion project/src/utils/ImporterUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export class ImporterUtil {
): Promise<T> {
const result = {} as T;

const allFiles = await this.fileSystem.getFiles(filepath, true, ["json"]);
const allFiles = await this.fileSystem.getFiles(filepath, true, ["json"], true);

const progressWriter = new ProgressWriter(allFiles.length); // Progress bar initialization
const fileProcessingPromises = allFiles.map(async (file) => {
try {
Expand Down

0 comments on commit 19f8efd

Please sign in to comment.