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
1 change: 1 addition & 0 deletions netlify-plugin-cloudinary/src/data/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const ERROR_NETLIFY_HOST_UNKNOWN = 'Cannot determine Netlify host, can no
export const ERROR_SITE_NAME_REQUIRED = 'Cannot determine the site name, can not proceed with plugin';
export const ERROR_UPLOAD_PRESET = 'To use a delivery type of "upload", please use an uploadPreset for unsigned requests or an API Key and Secret for signed requests'
export const ERROR_INVALID_SRCSET = 'Invalid srcset path. Please make sure the srcset is defined.'
export const WARNING_CLOUD_NAME_MISMATCH = 'Warning: Different Cloud Names detected. Environment variable CLOUDINARY_CLOUD_NAME and plugin input cloudName have different values. Using environment variable value.'
9 changes: 9 additions & 0 deletions netlify-plugin-cloudinary/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ERROR_NETLIFY_HOST_CLI_SUPPORT,
ERROR_NETLIFY_HOST_UNKNOWN,
ERROR_SITE_NAME_REQUIRED,
WARNING_CLOUD_NAME_MISMATCH,
} from './data/errors';

/**
Expand Down Expand Up @@ -159,6 +160,10 @@ export async function onBuild({
const apiKey = process.env.CLOUDINARY_API_KEY;
const apiSecret = process.env.CLOUDINARY_API_SECRET;

if (process.env.CLOUDINARY_CLOUD_NAME && inputs.cloudName && process.env.CLOUDINARY_CLOUD_NAME !== inputs.cloudName) {
console.warn(`[Cloudinary] ${WARNING_CLOUD_NAME_MISMATCH}`);
}

if (!cloudName) {
console.error(`[Cloudinary] ${ERROR_CLOUD_NAME_REQUIRED}`);
utils.build.failBuild(ERROR_CLOUD_NAME_REQUIRED);
Expand Down Expand Up @@ -351,6 +356,10 @@ export async function onPostBuild({
const apiKey = process.env.CLOUDINARY_API_KEY;
const apiSecret = process.env.CLOUDINARY_API_SECRET;

if (process.env.CLOUDINARY_CLOUD_NAME && inputs.cloudName && process.env.CLOUDINARY_CLOUD_NAME !== inputs.cloudName) {
console.warn(`[Cloudinary] ${WARNING_CLOUD_NAME_MISMATCH}`);
}

if (!cloudName) {
console.error(`[Cloudinary] ${ERROR_CLOUD_NAME_REQUIRED}`);
utils.build.failBuild(ERROR_CLOUD_NAME_REQUIRED);
Expand Down
30 changes: 29 additions & 1 deletion netlify-plugin-cloudinary/tests/on-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { promises as fs } from 'fs';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { onBuild } from '../src/';
import { ERROR_API_CREDENTIALS_REQUIRED } from '../src/data/errors';
import { ERROR_API_CREDENTIALS_REQUIRED, WARNING_CLOUD_NAME_MISMATCH } from '../src/data/errors';

const contexts = [
{
Expand Down Expand Up @@ -71,6 +71,34 @@ describe('onBuild', () => {
expect(console.error).toBeCalledWith(`[Cloudinary] ${ERROR_API_CREDENTIALS_REQUIRED}`);
});

test('should warn when cloud name in environment variable differs from input', async () => {
vi.spyOn(global.console, 'warn').mockImplementation();

process.env.CLOUDINARY_CLOUD_NAME = 'envcloud';
process.env.DEPLOY_PRIME_URL = 'https://deploy-preview-1234--netlify-plugin-cloudinary.netlify.app';

const imagesPath = '/images';

await onBuild({
constants: {
PUBLISH_DIR: `.next/out${imagesPath}`
},
inputs: {
cloudName: 'inputcloud'
},
netlifyConfig: {
redirects: []
},
utils: {
build: {
failBuild: () => { }
}
}
});

expect(console.warn).toBeCalledWith(`[Cloudinary] ${WARNING_CLOUD_NAME_MISMATCH}`);
});

});

describe('Redirects', () => {
Expand Down
26 changes: 26 additions & 0 deletions netlify-plugin-cloudinary/tests/on-post-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { promises as fs } from 'fs';
import path from 'path';
import { JSDOM } from 'jsdom';
import { onPostBuild } from '../src/';
import { WARNING_CLOUD_NAME_MISMATCH } from '../src/data/errors';

const mocksPath = path.join(__dirname, 'mocks/html');
const tempPath = path.join(mocksPath, 'temp');
Expand Down Expand Up @@ -56,6 +57,31 @@ describe('onPostBuild', () => {
await fs.rm(tempPath, { recursive: true, force: true })
})

describe('Config', () => {

test('should warn when cloud name in environment variable differs from input', async () => {
vi.spyOn(global.console, 'warn').mockImplementation();

process.env.CONTEXT = 'production';
process.env.CLOUDINARY_CLOUD_NAME = 'envcloud';

const tempTestPath = path.join(tempPath, expect.getState().currentTestName.replace(replaceRegEx, replaceValue));

await onPostBuild({
constants: {
PUBLISH_DIR: tempTestPath
},
inputs: {
cloudName: 'inputcloud',
folder: process.env.SITE_NAME,
},
});

expect(console.warn).toBeCalledWith(`[Cloudinary] ${WARNING_CLOUD_NAME_MISMATCH}`);
});

});

describe('Build', () => {

test('should replace with Cloudinary URLs', async () => {
Expand Down