From a905bb41a20e8b83558745880d7ccd18655cbed8 Mon Sep 17 00:00:00 2001 From: coldworld22 Date: Thu, 16 Apr 2026 16:31:04 +0300 Subject: [PATCH] fix: handle BOM in JSON parsing using custom DataFile parser --- src/bin.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 3cc39d83b..86e09a08e 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -126,13 +126,18 @@ if (readFileSync(file, "utf-8").trim() === "") { // Set up database let adapter: Adapter; +const stripBom = (text: string) => text.replace(/^\uFEFF/, '') + if (extname(file) === ".json5") { adapter = new DataFile(file, { - parse: JSON5.parse, + parse: (text) => JSON5.parse(stripBom(text)), stringify: JSON5.stringify, - }); + }) } else { - adapter = new JSONFile(file); + adapter = new DataFile(file, { + parse: (text) => JSON.parse(stripBom(text)) as RawData, + stringify: (data) => JSON.stringify(data, null, 2), + }) } const observer = new Observer(new NormalizedAdapter(adapter));