48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import log from "electron-log/main";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
|
|
const findFileCaseInsensitive = async (
|
|
extensionlessFilePath: string,
|
|
extensions: string[],
|
|
): Promise<string | null> => {
|
|
const directory: string = path.dirname(extensionlessFilePath);
|
|
try {
|
|
const matchingFiles = fs.readdirSync(directory).filter((file: string) => {
|
|
return (
|
|
extensions.some((ext) =>
|
|
file.toLowerCase().endsWith(ext.toLowerCase()),
|
|
) &&
|
|
path
|
|
.basename(file, path.extname(file))
|
|
.toLowerCase()
|
|
.startsWith(path.basename(extensionlessFilePath).toLowerCase())
|
|
);
|
|
});
|
|
const files: string[] = [];
|
|
matchingFiles.forEach((file) => {
|
|
const fullPath = path.join(directory, file);
|
|
files.push(fullPath);
|
|
});
|
|
|
|
// Return the first matching file if needed
|
|
if (files.length > 0) {
|
|
return files[0];
|
|
}
|
|
} catch (error) {
|
|
log.error(`Failed to read directory ${directory}:`, error);
|
|
throw error;
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
const getFilePathWithoutExtension = (filePath: string): string => {
|
|
return path.join(
|
|
path.dirname(filePath),
|
|
path.basename(filePath, path.extname(filePath)),
|
|
);
|
|
};
|
|
|
|
export { findFileCaseInsensitive };
|