Add case insensitivity checks for Mac and Linux platforms

This commit is contained in:
Patrick Fic
2025-04-02 12:57:08 -07:00
parent 20f2963330
commit dbaa88a19b
14 changed files with 469 additions and 124 deletions

View File

@@ -1,17 +1,22 @@
import { platform } from "@electron-toolkit/utils";
import { DBFFile } from "dbffile";
import log from "electron-log/main";
import _ from "lodash";
import deepLowerCaseKeys from "../../util/deepLowercaseKeys";
import errorTypeCheck from "../../util/errorTypeCheck";
import store from "../store/store";
import { DecodedAd1, OwnerRecordInterface } from "./decode-ad1.interface";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodeAD1 = async (
extensionlessFilePath: string,
): Promise<DecodedAd1> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}A.AD1`);
} catch (error) {
} catch {
// log.debug("Error opening AD1 File.", errorTypeCheck(error));
dbf = await DBFFile.open(`${extensionlessFilePath}.AD1`);
// log.debug("Trying to find AD1 file using regular CIECA Id.");
@@ -19,7 +24,28 @@ const DecodeAD1 = async (
if (!dbf) {
log.error(`Could not find any AD1 files at ${extensionlessFilePath}`);
throw new Error(`Could not find any AD1 files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any AD1 files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = ["a.ad1", ".ad1"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any AD1 files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any AD1 files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening AD1 File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords(1);
@@ -205,4 +231,5 @@ const DecodeAD1 = async (
return { ...rawAd1Data, owner: { data: ownerRecord } };
};
export default DecodeAD1;

View File

@@ -3,22 +3,45 @@ import log from "electron-log/main";
import _ from "lodash";
import deepLowerCaseKeys from "../../util/deepLowercaseKeys";
import { DecodedAD2 } from "./decode-ad2.interface";
import { platform } from "@electron-toolkit/utils";
import errorTypeCheck from "../../util/errorTypeCheck";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodeAD2 = async (
extensionlessFilePath: string,
): Promise<DecodedAD2> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}B.AD2`);
} catch (error) {
// log.error("Error opening AD2 File.", errorTypeCheck(error));
} catch {
dbf = await DBFFile.open(`${extensionlessFilePath}.AD2`);
// log.log("Trying to find AD2 file using regular CIECA Id.");
}
if (!dbf) {
log.error(`Could not find any AD2 files at ${extensionlessFilePath}`);
throw new Error(`Could not find any AD2 files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any AD2 files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = ["b.ad2", ".ad2"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any AD2 files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any AD2 files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening AD2 File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords(1);

View File

@@ -4,11 +4,15 @@ import _ from "lodash";
import deepLowerCaseKeys from "../../util/deepLowercaseKeys";
import errorTypeCheck from "../../util/errorTypeCheck";
import { DecodedEnv } from "./decode-env.interface";
import { platform } from "@electron-toolkit/utils";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodeEnv = async (
extensionlessFilePath: string,
): Promise<DecodedEnv> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}.ENV`);
} catch (error) {
@@ -17,9 +21,29 @@ const DecodeEnv = async (
if (!dbf) {
log.error(`Could not find any ENV files at ${extensionlessFilePath}`);
throw new Error(`Could not find any ENV files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any ENV files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = [".env"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any ENV files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any ENV files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening ENV File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords(1);
//AD2 will always have only 1 row.

View File

@@ -5,11 +5,14 @@ import deepLowerCaseKeys from "../../util/deepLowercaseKeys";
import errorTypeCheck from "../../util/errorTypeCheck";
import store from "../store/store";
import { DecodedLin, DecodedLinLine } from "./decode-lin.interface";
import { platform } from "@electron-toolkit/utils";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodeLin = async (
extensionlessFilePath: string,
): Promise<DecodedLin> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}.LIN`);
} catch (error) {
@@ -19,7 +22,28 @@ const DecodeLin = async (
if (!dbf) {
log.error(`Could not find any LIN files at ${extensionlessFilePath}`);
throw new Error(`Could not find any LIN files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any LIN files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = ["lin"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any LIN files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any LIN files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening LIN File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords();

View File

@@ -4,11 +4,14 @@ import _ from "lodash";
import deepLowerCaseKeys from "../../util/deepLowercaseKeys";
import errorTypeCheck from "../../util/errorTypeCheck";
import { DecodedPfh } from "./decode-pfh.interface";
import { platform } from "os";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodePfh = async (
extensionlessFilePath: string,
): Promise<DecodedPfh> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}.PFH`);
} catch (error) {
@@ -19,9 +22,29 @@ const DecodePfh = async (
if (!dbf) {
log.error(`Could not find any PFH files at ${extensionlessFilePath}`);
throw new Error(`Could not find any PFH files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFH files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = [".pfh"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any PFH files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFH files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening PFH File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords(1);
//AD2 will always have only 1 row.

View File

@@ -8,11 +8,14 @@ import {
JobLaborRateFields,
DecodedPflLine,
} from "./decode-pfl.interface";
import { platform } from "@electron-toolkit/utils";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodePfl = async (
extensionlessFilePath: string,
): Promise<DecodedPfl> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}.PFL`);
} catch (error) {
@@ -22,7 +25,28 @@ const DecodePfl = async (
if (!dbf) {
log.error(`Could not find any PFL files at ${extensionlessFilePath}`);
throw new Error(`Could not find any PFL files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFL files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = [".pfl"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any PFL files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFL files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening PFL File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords();

View File

@@ -9,11 +9,14 @@ import {
DecodedPfmLine,
JobMaterialRateFields,
} from "./decode-pfm.interface";
import { platform } from "@electron-toolkit/utils";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodePfm = async (
extensionlessFilePath: string,
): Promise<DecodedPfm> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}.PFM`);
} catch (error) {
@@ -23,9 +26,29 @@ const DecodePfm = async (
if (!dbf) {
log.error(`Could not find any PFM files at ${extensionlessFilePath}`);
throw new Error(`Could not find any PFM files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFM files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = [".pfm"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any PFM files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFM files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening PFM File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords();
//AD2 will always have only 1 row.

View File

@@ -5,11 +5,14 @@ import deepLowerCaseKeys from "../../util/deepLowercaseKeys";
import errorTypeCheck from "../../util/errorTypeCheck";
import YNBoolConverter from "../../util/ynBoolConverter";
import { DecodedPfo, DecodedPfoLine } from "./decode-pfo.interface";
import { platform } from "@electron-toolkit/utils";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodePfo = async (
extensionlessFilePath: string,
): Promise<DecodedPfo> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}.PFO`);
} catch (error) {
@@ -20,7 +23,28 @@ const DecodePfo = async (
if (!dbf) {
log.error(`Could not find any PFO files at ${extensionlessFilePath}`);
throw new Error(`Could not find any PFO files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFO files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = [".pfo"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any PFO files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFO files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening PFO File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords(1);

View File

@@ -9,11 +9,14 @@ import {
DecodedPfpLine,
DecodedPfpLinesByType,
} from "./decode-pfp.interface";
import { platform } from "@electron-toolkit/utils";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodePfp = async (
extensionlessFilePath: string,
): Promise<DecodedPfp> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}.PFP`);
} catch (error) {
@@ -23,9 +26,29 @@ const DecodePfp = async (
if (!dbf) {
log.error(`Could not find any PFP files at ${extensionlessFilePath}`);
throw new Error(`Could not find any PFP files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFP files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = [".pfp"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any PFP files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFP files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening PFP File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords();
//AD2 will always have only 1 row.

View File

@@ -1,14 +1,17 @@
import { platform } from "@electron-toolkit/utils";
import { DBFFile } from "dbffile";
import log from "electron-log/main";
import _ from "lodash";
import deepLowerCaseKeys from "../../util/deepLowercaseKeys";
import errorTypeCheck from "../../util/errorTypeCheck";
import { DecodedPft, DecodedPftLine } from "./decode-pft.interface";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodePft = async (
extensionlessFilePath: string,
): Promise<DecodedPft> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}.PFT`);
} catch (error) {
@@ -17,7 +20,28 @@ const DecodePft = async (
if (!dbf) {
log.error(`Could not find any PFT files at ${extensionlessFilePath}`);
throw new Error(`Could not find any PFT files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFT files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = ["pft"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any PFT files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any PFT files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening PFT File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords(1);

View File

@@ -4,11 +4,14 @@ import _ from "lodash";
import deepLowerCaseKeys from "../../util/deepLowercaseKeys";
import errorTypeCheck from "../../util/errorTypeCheck";
import { DecodedStl, DecodedStlLine } from "./decode-stl.interface";
import { platform } from "@electron-toolkit/utils";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodeStl = async (
extensionlessFilePath: string,
): Promise<DecodedStl> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}.STL`);
} catch (error) {
@@ -17,9 +20,29 @@ const DecodeStl = async (
if (!dbf) {
log.error(`Could not find any STL files at ${extensionlessFilePath}`);
throw new Error(`Could not find any STL files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any STL files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = ["stl"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any STL files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any STL files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening STL File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords();
//AD2 will always have only 1 row.

View File

@@ -4,11 +4,14 @@ import _ from "lodash";
import deepLowerCaseKeys from "../../util/deepLowercaseKeys";
import errorTypeCheck from "../../util/errorTypeCheck";
import { DecodedTtl, DecodedTtlLine } from "./decode-ttl.interface";
import { platform } from "@electron-toolkit/utils";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodeTtl = async (
extensionlessFilePath: string,
): Promise<DecodedTtl> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}.TTL`);
} catch (error) {
@@ -17,7 +20,28 @@ const DecodeTtl = async (
if (!dbf) {
log.error(`Could not find any TTL files at ${extensionlessFilePath}`);
throw new Error(`Could not find any TTL files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any TTL files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = ["ttl"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any TTL files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any TTL files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening TTL File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords(1);

View File

@@ -6,11 +6,14 @@ import { DecodedVeh, VehicleRecordInterface } from "./decode-veh.interface";
import errorTypeCheck from "../../util/errorTypeCheck";
import store from "../store/store";
import typeCaster from "../../util/typeCaster";
import { platform } from "@electron-toolkit/utils";
import { findFileCaseInsensitive } from "./decoder-utils";
const DecodeVeh = async (
extensionlessFilePath: string,
): Promise<DecodedVeh> => {
let dbf: DBFFile | null = null;
if (platform.isWindows) {
try {
dbf = await DBFFile.open(`${extensionlessFilePath}V.VEH`);
} catch (error) {
@@ -21,9 +24,29 @@ const DecodeVeh = async (
if (!dbf) {
log.error(`Could not find any VEH files at ${extensionlessFilePath}`);
throw new Error(`Could not find any VEH files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any VEH files at ${extensionlessFilePath}`,
);
}
} else {
const possibleExtensions: string[] = ["v.veh", ".veh"];
const filePath = await findFileCaseInsensitive(
extensionlessFilePath,
possibleExtensions,
);
try {
if (!filePath) {
log.error(`Could not find any VEH files at ${extensionlessFilePath}`);
throw new Error(
`Could not find any VEH files at ${extensionlessFilePath}`,
);
}
dbf = await DBFFile.open(filePath);
} catch (error) {
log.error("Error opening VEH File.", errorTypeCheck(error));
throw error;
}
}
const rawDBFRecord = await dbf.readRecords(1);
//AD2 will always have only 1 row.

View File

@@ -0,0 +1,36 @@
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) =>
extensions.some((ext) =>
file.toLowerCase().endsWith(ext.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;
};
export { findFileCaseInsensitive };