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

Allow bom markers #168

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 14 additions & 1 deletion app/server/src/configReader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import * as fs from "fs";
import * as path from "path";

export function stripBom(str: string): string {
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
// conversion translates it to FEFF (UTF-16 BOM).
if (str.charCodeAt(0) === 0xFEFF) {
return str.slice(1);
}
return str;
}

export function readFile(filename: string): string {
return stripBom(fs.readFileSync(filename, { encoding: "utf-8" }));
}

export class ConfigReader {
rootDir: string;

Expand All @@ -15,7 +28,7 @@ export class ConfigReader {
const fullPath = [this.rootDir, ...filePath];
const filename = path.join(...fullPath);
if (fs.existsSync(filename)) {
const configText = fs.readFileSync(filename, { encoding: "utf-8" });
const configText = readFile(filename);
return { ...JSON.parse(configText), ...this.overrides };
}
return null;
Expand Down
7 changes: 6 additions & 1 deletion app/server/tests/configReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ConfigReader } from "../src/configReader";

describe("configReader", () => {
beforeEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
});

it("returns null when config not found", () => {
Expand Down Expand Up @@ -31,4 +31,9 @@ describe("configReader", () => {
expect(mockReadFileSync.mock.calls[0][0]).toBe("root/test.config");
expect(mockReadFileSync.mock.calls[0][1]).toStrictEqual({ encoding: "utf-8" });
});

it("copes with BOM markers", () => {
const res: any = new ConfigReader(__dirname).readConfigFile("examples/bom.json");
expect((res as any).title).toBe("Ebola Project");
});
});
6 changes: 6 additions & 0 deletions app/server/tests/examples/bom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"appType": "fit",
"title": "Ebola Project",
"fitProp": "",
"readOnlyCode": false
}