Skip to content

Commit

Permalink
add summary
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasfha committed Sep 20, 2023
1 parent bfc3f2f commit bc912b9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
3 changes: 2 additions & 1 deletion netlify-plugin-cloudinary/src/data/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const ERROR_NETLIFY_HOST_CLI_SUPPORT =
export const ERROR_NETLIFY_HOST_UNKNOWN =
'Cannot determine Netlify host, can not proceed with plugin.'
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_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.'
32 changes: 13 additions & 19 deletions netlify-plugin-cloudinary/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const CLOUDINARY_ASSET_DIRECTORIES = [
*/

const _cloudinaryAssets = { images: {} } as Assets;
const errors = [];

export async function onBuild({
netlifyConfig,
Expand Down Expand Up @@ -176,12 +177,12 @@ export async function onBuild({
privateCdn,
});



// Look for any available images in the provided imagesPath to collect
// asset details and to grab a Cloudinary URL to use later

if ( typeof imagesPath === 'undefined' ) {
if (typeof imagesPath === 'undefined') {
throw new Error(ERROR_INVALID_IMAGES_PATH);
}

Expand Down Expand Up @@ -218,13 +219,7 @@ export async function onBuild({
}),
);
} catch (e) {
console.error('Error', e);
if (e instanceof Error) {
utils.build.failBuild(e.message);
} else {
utils.build.failBuild(e as string);
}
return;
errors.push(e)
}

// If the delivery type is set to upload, we need to be able to map individual assets based on their public ID,
Expand Down Expand Up @@ -261,9 +256,9 @@ export async function onBuild({

// Unsure how to type the above so that Inputs['privateCdn'] doesnt mess up types here

if ( !Array.isArray(mediaPaths) && typeof mediaPaths !== 'string' ) return;
if (!Array.isArray(mediaPaths) && typeof mediaPaths !== 'string') return;

if ( !Array.isArray(mediaPaths) ) {
if (!Array.isArray(mediaPaths)) {
mediaPaths = [mediaPaths];
}

Expand Down Expand Up @@ -293,21 +288,20 @@ export async function onBuild({
});
})
} catch (error) {
console.error('Error', error);
if (error instanceof Error) {
utils.build.failBuild(error.message);
} else {
utils.build.failBuild(error as string);
}
return;
errors.push(error)

}
}
)
);
}

console.log('[Cloudinary] Done.');
utils.status.show({
title: "[Cloudinary] Done.",
// Required.
summary: "Cloudinary build plugin has successfully completed.",
text: errors.length > 0 ? `The build was successful, but with some errors. There were ${errors.length} errors in total. Check the build logs for more information` : ""
});
}

// Post build looks through all of the output HTML and rewrites any src attributes to use a cloudinary URL
Expand Down
32 changes: 20 additions & 12 deletions netlify-plugin-cloudinary/src/lib/cloudinary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { JSDOM } from 'jsdom'
import { v2 as cloudinary, ConfigOptions } from 'cloudinary'

import { isRemoteUrl, determineRemoteUrl } from './util'
import { ERROR_ASSET_UPLOAD, ERROR_CLOUD_NAME_REQUIRED, ERROR_UPLOAD_PRESET } from '../data/errors'
import { ERROR_ASSET_UPLOAD, ERROR_CLOUD_NAME_REQUIRED, ERROR_INVALID_SRCSET, ERROR_UPLOAD_PRESET } from '../data/errors'

type CloudinaryConfig = {
apiKey?: string;
Expand Down Expand Up @@ -205,14 +205,14 @@ export async function getCloudinaryUrl(options: CloudinaryOptions) {
...uploadOptions,
})
} catch (error) {
console.error(ERROR_ASSET_UPLOAD, error)
console.error(ERROR_ASSET_UPLOAD)
console.error(error)
throw Error(ERROR_ASSET_UPLOAD)
}
} else {
// If we want to avoid signing our uploads, we don't need our API Key and Secret,
// however, we need to provide an uploadPreset
try {
console.log('Uploading')
results = await cloudinary.uploader.unsigned_upload(
fullPath,
uploadPreset,
Expand All @@ -221,7 +221,8 @@ export async function getCloudinaryUrl(options: CloudinaryOptions) {
},
)
} catch (error) {
console.error(ERROR_ASSET_UPLOAD, error)
console.error(ERROR_ASSET_UPLOAD)
console.error(error)
throw Error(ERROR_ASSET_UPLOAD)
}
}
Expand Down Expand Up @@ -342,14 +343,21 @@ export async function updateHtmlImagesToCloudinary(html: string, options: Update
if (exists && deliveryType === 'upload') {
return exists.cloudinaryUrl
}
return getCloudinaryUrl({
deliveryType,
folder,
path: url[0],
localDir,
uploadPreset,
remoteHost,
})
try {

return getCloudinaryUrl({
deliveryType,
folder,
path: url[0],
localDir,
uploadPreset,
remoteHost,
})
}catch(error){
console.error(ERROR_INVALID_SRCSET)
console.error(error)
throw Error(ERROR_INVALID_SRCSET)
}
})

const srcsetUrlsCloudinary = await Promise.all(srcsetUrlsPromises)
Expand Down
2 changes: 1 addition & 1 deletion netlify-plugin-cloudinary/tests/lib/cloudinary.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ERROR_ASSET_UPLOAD } = require('../../src/data/errors');
const { ERROR_ASSET_UPLOAD, ERROR_INVALID_SRCSET } = require('../../src/data/errors');
const { getCloudinary, createPublicId, getCloudinaryUrl, configureCloudinary, updateHtmlImagesToCloudinary } = require('../../src/lib/cloudinary');

const mockDemo = require('../mocks/demo.json');
Expand Down
6 changes: 0 additions & 6 deletions netlify-plugin-cloudinary/tests/mocks/demo.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
"assets": {
"images": [
{
"publishPath": "/images/ERROR.jpeg",
"sourceUrl": "demo/.next/images/ERROR.jpeg",
"cloudinaryUrl": "https://res.cloudinary.com/colbycloud/image/upload/f_auto,q_auto/v1/netlify-plugin-cloudinary/ERROR-b7ec1e3b78cccfac8658924c0728c095",
"publicId": "netlify-plugin-cloudinary/beach-b7ec1e3b78cccfac8658924c0728c095"
},
{
"publishPath": "/images/beach.jpeg",
"sourceUrl": "demo/.next/images/beach.jpeg",
Expand Down

0 comments on commit bc912b9

Please # to comment.