Skip to content

Commit

Permalink
Merge pull request #20 from igneel64/feature/use-process-cwd-when-env…
Browse files Browse the repository at this point in the history
…-unavailable

Fallback to process.cwd() for finding the cloudinary config file
  • Loading branch information
byara authored Nov 2, 2020
2 parents 24a76b3 + 8b1b66b commit 62bcc2d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
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());
});

0 comments on commit 62bcc2d

Please sign in to comment.