From 2a2d1daf6e370d49ab5d2ecb2e60a45646f1c7af Mon Sep 17 00:00:00 2001 From: Gabriel Duarte Date: Tue, 20 Feb 2024 19:48:02 +0100 Subject: [PATCH] Add support for cjs config files closes #116 --- README.md | 2 +- src/loadEnvironment.ts | 2 ++ test/loadEnvironment.test.ts | 28 +++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 056913e3..2cba49ca 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Then, add the following to your `package.json`: Feel free to change the output path to whatever you like. -Next, the codegen will expect you to have created a file called either `getContentfulEnvironment.js` or `getContentfulEnvironment.ts` +Next, the codegen will expect you to have created a file called `getContentfulEnvironment.js`, `getContentfulEnvironment.cjs` or `getContentfulEnvironment.ts` in the root of your project directory, which should export a promise that resolves with your Contentful environment. The reason for this is that you can do whatever you like to set up your Contentful Management diff --git a/src/loadEnvironment.ts b/src/loadEnvironment.ts index 75c59c2f..9c0df94d 100644 --- a/src/loadEnvironment.ts +++ b/src/loadEnvironment.ts @@ -60,6 +60,8 @@ function determineEnvironmentPath() { if (fs.existsSync(`${pathWithoutExtension}.ts`)) { return `${pathWithoutExtension}.ts` + } else if (fs.existsSync(`${pathWithoutExtension}.cjs`)) { + return `${pathWithoutExtension}.cjs` } return `${pathWithoutExtension}.js` diff --git a/test/loadEnvironment.test.ts b/test/loadEnvironment.test.ts index edff61c1..011c75de 100644 --- a/test/loadEnvironment.test.ts +++ b/test/loadEnvironment.test.ts @@ -14,6 +14,12 @@ jest.mock( { virtual: true }, ) +jest.mock( + require("path").resolve(process.cwd(), "./getContentfulEnvironment.cjs"), + () => getContentfulEnvironmentFileFactory("cjs"), + { virtual: true }, +) + jest.mock( require("path").resolve(process.cwd(), "./getContentfulEnvironment.ts"), () => getContentfulEnvironmentFileFactory("ts"), @@ -37,7 +43,9 @@ describe("loadEnvironment", () => { describe("when getContentfulEnvironment.ts exists", () => { beforeEach(() => { - jest.spyOn(fs, "existsSync").mockReturnValue(true) + jest.spyOn(fs, "existsSync").mockImplementation(path => { + return (path as string).endsWith(".ts") + }) }) describe("when ts-node is not found", () => { @@ -89,6 +97,7 @@ describe("loadEnvironment", () => { expect(getContentfulEnvironmentFileFactory).toHaveBeenCalledWith("ts") expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("js") + expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("cjs") }) it("disables the registerer afterwards", async () => { @@ -98,6 +107,22 @@ describe("loadEnvironment", () => { }) }) + describe("when getContentfulEnvironment.cjs exists", () => { + beforeEach(() => { + jest.spyOn(fs, "existsSync").mockImplementation(path => { + return (path as string).endsWith(".cjs") + }) + }) + + it("requires the javascript config on ESM environments", async () => { + await loadEnvironment() + + expect(getContentfulEnvironmentFileFactory).toHaveBeenCalledWith("cjs") + expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("js") + expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("ts") + }) + }) + it("requires the javascript config", async () => { jest.spyOn(fs, "existsSync").mockReturnValue(false) @@ -105,5 +130,6 @@ describe("loadEnvironment", () => { expect(getContentfulEnvironmentFileFactory).toHaveBeenCalledWith("js") expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("ts") + expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("cjs") }) })