Skip to content

Commit

Permalink
Add support for cjs config files
Browse files Browse the repository at this point in the history
closes #116
  • Loading branch information
GabeDuarteM committed Feb 20, 2024
1 parent ee3f7d2 commit 2a2d1da
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/loadEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
28 changes: 27 additions & 1 deletion test/loadEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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", () => {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -98,12 +107,29 @@ 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)

await loadEnvironment()

expect(getContentfulEnvironmentFileFactory).toHaveBeenCalledWith("js")
expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("ts")
expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("cjs")
})
})

0 comments on commit 2a2d1da

Please sign in to comment.