diff --git a/src/commands/firestore-operations-describe.spec.ts b/src/commands/firestore-operations-describe.spec.ts index 96446444a19..bcb371b596c 100644 --- a/src/commands/firestore-operations-describe.spec.ts +++ b/src/commands/firestore-operations-describe.spec.ts @@ -86,9 +86,9 @@ describe("firestore:operations:describe", () => { it("should throw a FirebaseError if operation name is invalid", async () => { const options = { project: "test-project" }; - await expect(command.runner()("", options)).to.be.rejectedWith( + await expect(command.runner()("/databases/blah", options)).to.be.rejectedWith( FirebaseError, - '"" is not a valid operation name.', + '"/databases/blah" is not a valid operation name.', ); await expect(command.runner()("projects/p/databases/d", options)).to.be.rejectedWith( FirebaseError, diff --git a/src/config.spec.ts b/src/config.spec.ts index 5c9db806931..e381a3aff3f 100644 --- a/src/config.spec.ts +++ b/src/config.spec.ts @@ -7,6 +7,7 @@ import { Config } from "./config"; import { FIREBASE_JSON_PATH as VALID_CONFIG_PATH } from "./test/fixtures/valid-config"; import { FIXTURE_DIR as SIMPLE_CONFIG_DIR } from "./test/fixtures/config-imports"; import { FIXTURE_DIR as DUP_TOP_LEVEL_CONFIG_DIR } from "./test/fixtures/dup-top-level"; +import { FIREBASE_JSON_PATH as EMPTY_CONFIG_PATH } from "./test/fixtures/empty-config"; describe("Config", () => { describe("#load", () => { @@ -17,9 +18,16 @@ describe("Config", () => { configPath: path.relative(cwd, VALID_CONFIG_PATH), }); expect(config).to.not.be.null; - if (config) { - expect(config.get("database.rules")).to.eq("config/security-rules.json"); - } + expect(config?.get("database.rules")).to.eq("config/security-rules.json"); + }); + it("should fall back to {} when the file is empty", () => { + const cwd = __dirname; + const config = Config.load({ + cwd, + configPath: path.relative(cwd, EMPTY_CONFIG_PATH), + }); + expect(config).to.not.be.null; + expect(config?.data).to.deep.eq({}); }); }); diff --git a/src/config.ts b/src/config.ts index 1f337e6877a..b42c6121d84 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,7 +6,6 @@ import * as _ from "lodash"; import * as clc from "colorette"; import * as fs from "fs-extra"; import * as path from "path"; -const cjson = require("cjson"); import { detectProjectRoot } from "./detectProjectRoot"; import { FirebaseError } from "./error"; @@ -303,7 +302,10 @@ export class Config { if (pd) { try { const filePath = path.resolve(pd, path.basename(filename)); - const data = cjson.load(filePath); + let data: unknown = {}; + if (fs.statSync(filePath).size > 0) { + data = loadCJSON(filePath); + } // Validate config against JSON Schema. For now we just print these to debug // logs but in a future CLI version they could be warnings and/or errors. diff --git a/src/test/fixtures/empty-config/firebase.json b/src/test/fixtures/empty-config/firebase.json new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/test/fixtures/empty-config/index.ts b/src/test/fixtures/empty-config/index.ts new file mode 100644 index 00000000000..dbf104ec838 --- /dev/null +++ b/src/test/fixtures/empty-config/index.ts @@ -0,0 +1,8 @@ +import { resolve } from "path"; + +/** + * A directory containing an empty rebase.json. + */ +export const FIXTURE_DIR = __dirname; + +export const FIREBASE_JSON_PATH = resolve(__dirname, "firebase.json");