Skip to content
Open
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
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")
})
})