From 7394f4b71c23f0eeeae862c0229fdb9e47daded3 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Tue, 1 Oct 2024 21:20:06 +0800 Subject: [PATCH 1/5] Check if the file already starts with a UTF-8 BOM --- .../src/targets/nsis/nsisLicense.ts | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts b/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts index 817ff2ddf81..a0d830b4979 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,26 @@ import * as fs from "fs" function convertFileToUtf8WithBOMSync(filePath: string): boolean { try { - 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]) - fs.writeFileSync(filePath, dataWithBOM) - return true - } catch (err) { - console.error("Failed to convert file to UTF-8 with BOM: ", err) - return false + const data = fs.readFileSync(filePath); + const BOM = Buffer.from([0xef, 0xbb, 0xbf]); + + // Check if the file already starts with a UTF-8 BOM + if (data.length >= 3 && data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) { + log.info("File is already in UTF-8 with BOM format"); + return true; + } + + // If not, add the BOM + const dataWithBOM = Buffer.concat([BOM, data]); + fs.writeFileSync(filePath, dataWithBOM); + log.info("File successfully converted to UTF-8 with BOM"); + return true; + } catch (err: any) { + log.error("Failed to convert file to UTF-8 with BOM: ", err.toString()); + 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) { From ac50ef7ef704a9ed66d4995afdbcffebbb595f0e Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Tue, 1 Oct 2024 21:23:16 +0800 Subject: [PATCH 2/5] add changeset --- .changeset/six-spoons-heal.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/six-spoons-heal.md diff --git a/.changeset/six-spoons-heal.md b/.changeset/six-spoons-heal.md new file mode 100644 index 00000000000..5dbedd721af --- /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 From 54c43ccaa6f3f984b4ac8f9a516a9000ad2a1392 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Tue, 1 Oct 2024 21:30:48 +0800 Subject: [PATCH 3/5] format code --- .../src/targets/nsis/nsisLicense.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts b/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts index a0d830b4979..68dbd12d76c 100644 --- a/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts +++ b/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts @@ -10,23 +10,23 @@ import * as fs from "fs" function convertFileToUtf8WithBOMSync(filePath: string): boolean { try { - const data = fs.readFileSync(filePath); - const BOM = Buffer.from([0xef, 0xbb, 0xbf]); + const data = fs.readFileSync(filePath) + const BOM = Buffer.from([0xef, 0xbb, 0xbf]) // Check if the file already starts with a UTF-8 BOM if (data.length >= 3 && data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) { - log.info("File is already in UTF-8 with BOM format"); - return true; + log.info("File is already in UTF-8 with BOM format") + return true } // If not, add the BOM - const dataWithBOM = Buffer.concat([BOM, data]); - fs.writeFileSync(filePath, dataWithBOM); - log.info("File successfully converted to UTF-8 with BOM"); - return true; + const dataWithBOM = Buffer.concat([BOM, data]) + fs.writeFileSync(filePath, dataWithBOM) + log.info("File successfully converted to UTF-8 with BOM") + return true } catch (err: any) { - log.error("Failed to convert file to UTF-8 with BOM: ", err.toString()); - return false; + log.error("Failed to convert file to UTF-8 with BOM: ", err.toString()) + return false } } From 9bf6b2008369b0e72400972f636d9f48bf1f54b5 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Wed, 2 Oct 2024 08:13:14 +0800 Subject: [PATCH 4/5] fix comments --- .../src/targets/nsis/nsisLicense.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts b/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts index 68dbd12d76c..41d44182538 100644 --- a/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts +++ b/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts @@ -8,24 +8,26 @@ import { NsisScriptGenerator } from "./nsisScriptGenerator" import { nsisTemplatesDir } from "./nsisUtil" 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) - const BOM = Buffer.from([0xef, 0xbb, 0xbf]) // Check if the file already starts with a UTF-8 BOM - if (data.length >= 3 && data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) { - log.info("File is already in UTF-8 with BOM format") - return true + 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([BOM, data]) + const dataWithBOM = Buffer.concat([UTF8_BOM_HEADER, data]) fs.writeFileSync(filePath, dataWithBOM) - log.info("File successfully converted to UTF-8 with BOM") + log.debug({ file: log.filePath(filePath) }, "file successfully converted to UTF-8 with BOM") return true } catch (err: any) { - log.error("Failed to convert file to UTF-8 with BOM: ", err.toString()) + log.error({ file: log.filePath(filePath), message: err.message ?? err.stack }, "unable to convert file to UTF-8 with BOM") return false } } From 9142afa3f7b8f1fd8a3a572163127dea902f8d18 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Wed, 2 Oct 2024 08:14:35 +0800 Subject: [PATCH 5/5] format code --- packages/app-builder-lib/src/targets/nsis/nsisLicense.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts b/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts index 41d44182538..66733ad2fe1 100644 --- a/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts +++ b/packages/app-builder-lib/src/targets/nsis/nsisLicense.ts @@ -8,7 +8,6 @@ import { NsisScriptGenerator } from "./nsisScriptGenerator" import { nsisTemplatesDir } from "./nsisUtil" import * as fs from "fs" - function convertFileToUtf8WithBOMSync(filePath: string): boolean { try { const UTF8_BOM_HEADER = Buffer.from([0xef, 0xbb, 0xbf]) @@ -18,7 +17,7 @@ function convertFileToUtf8WithBOMSync(filePath: string): boolean { 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; + return true } // If not, add the BOM