diff --git a/.changeset/six-spoons-heal.md b/.changeset/six-spoons-heal.md new file mode 100644 index 0000000000..5dbedd721a --- /dev/null +++ b/.changeset/six-spoons-heal.md @@ -0,0 +1,5 @@ +--- +"app-builder-lib": patch +--- + +Check if the file already starts with a UTF-8 BOM diff --git a/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts b/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts index 817ff2ddf8..66733ad2fe 100644 --- a/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts +++ b/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts @@ -1,3 +1,4 @@ +import { log } from "builder-util" import { lcid } from "../../util/langs" import { getLicenseFiles, getNotLocalizedLicenseFile } from "../../util/license" import * as path from "path" @@ -9,17 +10,27 @@ import * as fs from "fs" function convertFileToUtf8WithBOMSync(filePath: string): boolean { try { + const UTF8_BOM_HEADER = Buffer.from([0xef, 0xbb, 0xbf]) const data = fs.readFileSync(filePath) - // UTF-8 BOM is EF BB BF - const BOM = Buffer.from([0xef, 0xbb, 0xbf]) - const dataWithBOM = Buffer.concat([BOM, data]) + + // Check if the file already starts with a UTF-8 BOM + log.debug({ file: log.filePath(filePath) }, "checking file for BOM header") + if (data.length >= UTF8_BOM_HEADER.length && data.subarray(0, UTF8_BOM_HEADER.length).equals(UTF8_BOM_HEADER)) { + log.debug({ file: log.filePath(filePath) }, "file is already in BOM format, skipping conversion.") + return true + } + + // If not, add the BOM + const dataWithBOM = Buffer.concat([UTF8_BOM_HEADER, data]) fs.writeFileSync(filePath, dataWithBOM) + log.debug({ file: log.filePath(filePath) }, "file successfully converted to UTF-8 with BOM") return true - } catch (err) { - console.error("Failed to convert file to UTF-8 with BOM: ", err) + } catch (err: any) { + log.error({ file: log.filePath(filePath), message: err.message ?? err.stack }, "unable to convert file to UTF-8 with BOM") return false } } + export async function computeLicensePage(packager: WinPackager, options: NsisOptions, scriptGenerator: NsisScriptGenerator, languages: Array): Promise { const license = await getNotLocalizedLicenseFile(options.license, packager) if (license != null) {