Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if the file already starts with a UTF-8 BOM #8551

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/six-spoons-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-builder-lib": patch
---

Check if the file already starts with a UTF-8 BOM
16 changes: 13 additions & 3 deletions packages/app-builder-lib/src/targets/nsis/nsisLicense.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -10,16 +11,25 @@ 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])

// Check if the file already starts with a UTF-8 BOM
if (data.length >= 3 && data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) {
mmaietta marked this conversation as resolved.
Show resolved Hide resolved
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) {
console.error("Failed to convert file to UTF-8 with BOM: ", err)
} 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<string>): Promise<void> {
const license = await getNotLocalizedLicenseFile(options.license, packager)
if (license != null) {
Expand Down
Loading