diff --git a/src/main/decoder/decode-ad1.ts b/src/main/decoder/decode-ad1.ts index 30ebb2e..9116a73 100644 --- a/src/main/decoder/decode-ad1.ts +++ b/src/main/decoder/decode-ad1.ts @@ -1,25 +1,51 @@ +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 => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}A.AD1`); - } catch (error) { - // 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."); - } - if (!dbf) { - log.error(`Could not find any AD1 files at ${extensionlessFilePath}`); - throw new Error(`Could not find any AD1 files at ${extensionlessFilePath}`); + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}A.AD1`); + } 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."); + } + + if (!dbf) { + log.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; diff --git a/src/main/decoder/decode-ad2.ts b/src/main/decoder/decode-ad2.ts index ddfb702..adfb2b3 100644 --- a/src/main/decoder/decode-ad2.ts +++ b/src/main/decoder/decode-ad2.ts @@ -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 => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}B.AD2`); - } catch (error) { - // log.error("Error opening AD2 File.", errorTypeCheck(error)); - dbf = await DBFFile.open(`${extensionlessFilePath}.AD2`); - // log.log("Trying to find AD2 file using regular CIECA Id."); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}B.AD2`); + } catch { + dbf = await DBFFile.open(`${extensionlessFilePath}.AD2`); + } - if (!dbf) { - log.error(`Could not find any AD2 files at ${extensionlessFilePath}`); - throw new Error(`Could not find any AD2 files at ${extensionlessFilePath}`); + if (!dbf) { + log.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); diff --git a/src/main/decoder/decode-env.ts b/src/main/decoder/decode-env.ts index 529fcf7..de26726 100644 --- a/src/main/decoder/decode-env.ts +++ b/src/main/decoder/decode-env.ts @@ -4,22 +4,46 @@ 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 => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}.ENV`); - } catch (error) { - log.error("Error opening ENV File.", errorTypeCheck(error)); - } - if (!dbf) { - log.error(`Could not find any ENV files at ${extensionlessFilePath}`); - throw new Error(`Could not find any ENV files at ${extensionlessFilePath}`); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}.ENV`); + } catch (error) { + log.error("Error opening ENV File.", errorTypeCheck(error)); + } + if (!dbf) { + log.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. diff --git a/src/main/decoder/decode-lin.ts b/src/main/decoder/decode-lin.ts index 8c5e724..a5b2c1b 100644 --- a/src/main/decoder/decode-lin.ts +++ b/src/main/decoder/decode-lin.ts @@ -5,21 +5,45 @@ 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 => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}.LIN`); - } catch (error) { - //LIN File only has 1 location. - log.error("Error opening LIN File.", errorTypeCheck(error)); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}.LIN`); + } catch (error) { + //LIN File only has 1 location. + log.error("Error opening LIN File.", errorTypeCheck(error)); + } - if (!dbf) { - log.error(`Could not find any LIN files at ${extensionlessFilePath}`); - throw new Error(`Could not find any LIN files at ${extensionlessFilePath}`); + if (!dbf) { + log.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(); diff --git a/src/main/decoder/decode-pfh.ts b/src/main/decoder/decode-pfh.ts index 4ca911b..0a9c724 100644 --- a/src/main/decoder/decode-pfh.ts +++ b/src/main/decoder/decode-pfh.ts @@ -4,24 +4,47 @@ 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 => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}.PFH`); - } catch (error) { - log.error("Error opening PFH File.", errorTypeCheck(error)); - dbf = await DBFFile.open(`${extensionlessFilePath}.PFH`); - log.log("Trying to find PFH file using regular CIECA Id."); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}.PFH`); + } catch (error) { + log.error("Error opening PFH File.", errorTypeCheck(error)); + dbf = await DBFFile.open(`${extensionlessFilePath}.PFH`); + log.log("Trying to find PFH file using regular CIECA Id."); + } - if (!dbf) { - log.error(`Could not find any PFH files at ${extensionlessFilePath}`); - throw new Error(`Could not find any PFH files at ${extensionlessFilePath}`); + if (!dbf) { + log.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. diff --git a/src/main/decoder/decode-pfl.ts b/src/main/decoder/decode-pfl.ts index a0caa67..0cd6843 100644 --- a/src/main/decoder/decode-pfl.ts +++ b/src/main/decoder/decode-pfl.ts @@ -8,21 +8,45 @@ import { JobLaborRateFields, DecodedPflLine, } from "./decode-pfl.interface"; +import { platform } from "@electron-toolkit/utils"; +import { findFileCaseInsensitive } from "./decoder-utils"; const DecodePfl = async ( extensionlessFilePath: string, ): Promise => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}.PFL`); - } catch (error) { - //PFL File only has 1 location. - log.error("Error opening PFL File.", errorTypeCheck(error)); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}.PFL`); + } catch (error) { + //PFL File only has 1 location. + log.error("Error opening PFL File.", errorTypeCheck(error)); + } - if (!dbf) { - log.error(`Could not find any PFL files at ${extensionlessFilePath}`); - throw new Error(`Could not find any PFL files at ${extensionlessFilePath}`); + if (!dbf) { + log.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(); diff --git a/src/main/decoder/decode-pfm.ts b/src/main/decoder/decode-pfm.ts index 5b99398..cc840db 100644 --- a/src/main/decoder/decode-pfm.ts +++ b/src/main/decoder/decode-pfm.ts @@ -9,23 +9,46 @@ import { DecodedPfmLine, JobMaterialRateFields, } from "./decode-pfm.interface"; +import { platform } from "@electron-toolkit/utils"; +import { findFileCaseInsensitive } from "./decoder-utils"; const DecodePfm = async ( extensionlessFilePath: string, ): Promise => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}.PFM`); - } catch (error) { - //PFM File only has 1 location. - log.error("Error opening PFM File.", errorTypeCheck(error)); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}.PFM`); + } catch (error) { + //PFM File only has 1 location. + log.error("Error opening PFM File.", errorTypeCheck(error)); + } - if (!dbf) { - log.error(`Could not find any PFM files at ${extensionlessFilePath}`); - throw new Error(`Could not find any PFM files at ${extensionlessFilePath}`); + if (!dbf) { + log.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. diff --git a/src/main/decoder/decode-pfo.ts b/src/main/decoder/decode-pfo.ts index 6a6e536..c63241d 100644 --- a/src/main/decoder/decode-pfo.ts +++ b/src/main/decoder/decode-pfo.ts @@ -5,22 +5,46 @@ 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 => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}.PFO`); - } catch (error) { - log.error("Error opening PFO File.", errorTypeCheck(error)); - dbf = await DBFFile.open(`${extensionlessFilePath}.PFO`); - log.log("Trying to find PFO file using regular CIECA Id."); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}.PFO`); + } catch (error) { + log.error("Error opening PFO File.", errorTypeCheck(error)); + dbf = await DBFFile.open(`${extensionlessFilePath}.PFO`); + log.log("Trying to find PFO file using regular CIECA Id."); + } - if (!dbf) { - log.error(`Could not find any PFO files at ${extensionlessFilePath}`); - throw new Error(`Could not find any PFO files at ${extensionlessFilePath}`); + if (!dbf) { + log.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); diff --git a/src/main/decoder/decode-pfp.ts b/src/main/decoder/decode-pfp.ts index b19e822..c6d1f2a 100644 --- a/src/main/decoder/decode-pfp.ts +++ b/src/main/decoder/decode-pfp.ts @@ -9,23 +9,46 @@ import { DecodedPfpLine, DecodedPfpLinesByType, } from "./decode-pfp.interface"; +import { platform } from "@electron-toolkit/utils"; +import { findFileCaseInsensitive } from "./decoder-utils"; const DecodePfp = async ( extensionlessFilePath: string, ): Promise => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}.PFP`); - } catch (error) { - //PFP File only has 1 location. - log.error("Error opening PFP File.", errorTypeCheck(error)); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}.PFP`); + } catch (error) { + //PFP File only has 1 location. + log.error("Error opening PFP File.", errorTypeCheck(error)); + } - if (!dbf) { - log.error(`Could not find any PFP files at ${extensionlessFilePath}`); - throw new Error(`Could not find any PFP files at ${extensionlessFilePath}`); + if (!dbf) { + log.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. diff --git a/src/main/decoder/decode-pft.ts b/src/main/decoder/decode-pft.ts index 5c9e2c3..3430660 100644 --- a/src/main/decoder/decode-pft.ts +++ b/src/main/decoder/decode-pft.ts @@ -1,23 +1,47 @@ +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 => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}.PFT`); - } catch (error) { - log.error("Error opening PFH File.", errorTypeCheck(error)); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}.PFT`); + } catch (error) { + log.error("Error opening PFH File.", errorTypeCheck(error)); + } - if (!dbf) { - log.error(`Could not find any PFT files at ${extensionlessFilePath}`); - throw new Error(`Could not find any PFT files at ${extensionlessFilePath}`); + if (!dbf) { + log.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); diff --git a/src/main/decoder/decode-stl.ts b/src/main/decoder/decode-stl.ts index 2c322a0..0706099 100644 --- a/src/main/decoder/decode-stl.ts +++ b/src/main/decoder/decode-stl.ts @@ -4,22 +4,45 @@ 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 => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}.STL`); - } catch (error) { - log.error("Error opening STL File.", errorTypeCheck(error)); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}.STL`); + } catch (error) { + log.error("Error opening STL File.", errorTypeCheck(error)); + } - if (!dbf) { - log.error(`Could not find any STL files at ${extensionlessFilePath}`); - throw new Error(`Could not find any STL files at ${extensionlessFilePath}`); + if (!dbf) { + log.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. diff --git a/src/main/decoder/decode-ttl.ts b/src/main/decoder/decode-ttl.ts index 4a9521f..0fc1f36 100644 --- a/src/main/decoder/decode-ttl.ts +++ b/src/main/decoder/decode-ttl.ts @@ -4,20 +4,44 @@ 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 => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}.TTL`); - } catch (error) { - log.error("Error opening TTL File.", errorTypeCheck(error)); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}.TTL`); + } catch (error) { + log.error("Error opening TTL File.", errorTypeCheck(error)); + } - if (!dbf) { - log.error(`Could not find any TTL files at ${extensionlessFilePath}`); - throw new Error(`Could not find any TTL files at ${extensionlessFilePath}`); + if (!dbf) { + log.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); diff --git a/src/main/decoder/decode-veh.ts b/src/main/decoder/decode-veh.ts index c503c8e..467207d 100644 --- a/src/main/decoder/decode-veh.ts +++ b/src/main/decoder/decode-veh.ts @@ -6,24 +6,47 @@ 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 => { let dbf: DBFFile | null = null; - try { - dbf = await DBFFile.open(`${extensionlessFilePath}V.VEH`); - } catch (error) { - log.error("Error opening VEH File.", errorTypeCheck(error)); - dbf = await DBFFile.open(`${extensionlessFilePath}.VEH`); - log.log("Found VEH file using regular CIECA Id."); - } + if (platform.isWindows) { + try { + dbf = await DBFFile.open(`${extensionlessFilePath}V.VEH`); + } catch (error) { + log.error("Error opening VEH File.", errorTypeCheck(error)); + dbf = await DBFFile.open(`${extensionlessFilePath}.VEH`); + log.log("Found VEH file using regular CIECA Id."); + } - if (!dbf) { - log.error(`Could not find any VEH files at ${extensionlessFilePath}`); - throw new Error(`Could not find any VEH files at ${extensionlessFilePath}`); + if (!dbf) { + log.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. diff --git a/src/main/decoder/decoder-utils.ts b/src/main/decoder/decoder-utils.ts new file mode 100644 index 0000000..1d0c5fe --- /dev/null +++ b/src/main/decoder/decoder-utils.ts @@ -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 => { + 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 };