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

Fallback to process.cwd() for finding the cloudinary config file #20

Merged
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
3 changes: 2 additions & 1 deletion lib/cloudinary-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
*/
const _has = require("lodash/has");
const cloudinary = require("cloudinary-core");
const requireConfig = require("./helpers/requireConfig");

// https://cloudinary.com/documentation/solution_overview#configuration_parameters
const runConfig = require(`${process.env.INIT_CWD}/cloudinaryrc.json`);
const runConfig = requireConfig();

if (!_has(runConfig, "native.cloud_name")) {
throw new Error("You need to provide a **native** object with the mandatory **cloud_name** field");
Expand Down
19 changes: 19 additions & 0 deletions lib/helpers/requireConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Load the cloudinaryrc configuration from the top level path
* @throws Will throw if the file is not or the error faced during the `require` call
* @returns {NodeRequire} JSON configuration module
*/
function requireConfig() {
const configFileLocation = `${process.env.INIT_CWD || process.cwd()}/cloudinaryrc.json`;

try {
return require(configFileLocation);
} catch (e) {
if (e.code !== "MODULE_NOT_FOUND") {
throw e;
}
throw Error(`Cloudinary config could not be found at ${configFileLocation}`);
}
}

module.exports = requireConfig;
25 changes: 25 additions & 0 deletions test/requireConfig.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const requireConfig = require("../lib/helpers/requireConfig");

describe("requireConfig", () => {
it("throws when config not found at expected location", () => {
expect(requireConfig).toThrow(`Cloudinary config could not be found at ${process.env.INIT_CWD || process.cwd()}`);
});

it("loads the required file", () => {
const mockConfig = JSON.stringify({ test: 1 });

jest.mock(
`${process.cwd()}/cloudinaryrc.json`,
() => {
return mockConfig;
},
{ virtual: true }
);

const loadedConfig = requireConfig();

expect(loadedConfig).toBe(mockConfig);
});

afterAll(() => jest.resetAllMocks());
});