Skip to content

Commit d7dd264

Browse files
authored
Merge pull request #16 from colbyfayock/feat/14-error-reporting
using Netlify build utils to report plugin errors
2 parents 261903a + 8b1a45c commit d7dd264

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

src/data/errors.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
ERROR_CLOUD_NAME_REQUIRED: 'A Cloudinary Cloud Name is required. Please set cloudName input or use the environment variable CLOUDINARY_CLOUD_NAME'
3+
}

src/index.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const glob = require('glob');
44

55
const { configureCloudinary, updateHtmlImagesToCloudinary, getCloudinaryUrl } = require('./lib/cloudinary');
66
const { PUBLIC_ASSET_PATH } = require('./data/cloudinary');
7+
const { ERROR_CLOUD_NAME_REQUIRED } = require('./data/errors');
78

89
const CLOUDINARY_ASSET_DIRECTORIES = [
910
{
@@ -19,7 +20,7 @@ const CLOUDINARY_ASSET_DIRECTORIES = [
1920
*/
2021

2122
module.exports = {
22-
async onPreBuild({ netlifyConfig, constants, inputs }) {
23+
async onPreBuild({ netlifyConfig, constants, inputs, utils }) {
2324
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
2425

2526
if ( !host ) {
@@ -50,7 +51,8 @@ module.exports = {
5051
const apiSecret = process.env.CLOUDINARY_API_SECRET;
5152

5253
if ( !cloudName ) {
53-
throw new Error('A Cloudinary Cloud Name is required. Please set cloudName input or use the environment variable CLOUDINARY_CLOUD_NAME');
54+
utils.build.failBuild(ERROR_CLOUD_NAME_REQUIRED);
55+
return;
5456
}
5557

5658
configureCloudinary({
@@ -62,34 +64,41 @@ module.exports = {
6264
const imagesDirectory = glob.sync(`${PUBLISH_DIR}/images/**/*`);
6365
const imagesFiles = imagesDirectory.filter(file => !!path.extname(file));
6466

65-
const images = await Promise.all(imagesFiles.map(async image => {
66-
const publishPath = image.replace(PUBLISH_DIR, '');
67-
const publishUrl = `${host}${publishPath}`;
68-
const cldAssetPath = `/${path.join(PUBLIC_ASSET_PATH, publishPath)}`;
69-
const cldAssetUrl = `${host}${cldAssetPath}`;
67+
let images;
7068

71-
const cloudinary = await getCloudinaryUrl({
72-
deliveryType,
73-
folder,
74-
path: publishPath,
75-
localDir: PUBLISH_DIR,
76-
uploadPreset,
77-
remoteHost: host,
78-
});
69+
try {
70+
await Promise.all(imagesFiles.map(async image => {
71+
const publishPath = image.replace(PUBLISH_DIR, '');
72+
const publishUrl = `${host}${publishPath}`;
73+
const cldAssetPath = `/${path.join(PUBLIC_ASSET_PATH, publishPath)}`;
74+
const cldAssetUrl = `${host}${cldAssetPath}`;
7975

80-
return {
81-
publishPath,
82-
publishUrl,
83-
...cloudinary
84-
}
85-
}));
76+
const cloudinary = await getCloudinaryUrl({
77+
deliveryType,
78+
folder,
79+
path: publishPath,
80+
localDir: PUBLISH_DIR,
81+
uploadPreset,
82+
remoteHost: host,
83+
});
84+
85+
return {
86+
publishPath,
87+
publishUrl,
88+
...cloudinary
89+
}
90+
}));
91+
} catch(e) {
92+
utils.build.failBuild(e.message);
93+
return;
94+
}
8695

8796
netlifyConfig.build.environment.CLOUDINARY_ASSETS = {
8897
images
8998
}
9099
},
91100

92-
async onBuild({ netlifyConfig, constants, inputs }) {
101+
async onBuild({ netlifyConfig, constants, inputs, utils }) {
93102
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
94103

95104
if ( !host ) {
@@ -111,7 +120,8 @@ module.exports = {
111120
const apiSecret = process.env.CLOUDINARY_API_SECRET;
112121

113122
if ( !cloudName ) {
114-
throw new Error('A Cloudinary Cloud Name is required. Please set cloudName input or use the environment variable CLOUDINARY_CLOUD_NAME');
123+
utils.build.failBuild(ERROR_CLOUD_NAME_REQUIRED);
124+
return;
115125
}
116126

117127
configureCloudinary({
@@ -177,7 +187,7 @@ module.exports = {
177187
// Post build looks through all of the output HTML and rewrites any src attributes to use a cloudinary URL
178188
// This only solves on-page references until any JS refreshes the DOM
179189

180-
async onPostBuild({ netlifyConfig, constants, inputs }) {
190+
async onPostBuild({ netlifyConfig, constants, inputs, utils }) {
181191
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
182192

183193
if ( !host ) {
@@ -198,7 +208,8 @@ module.exports = {
198208
const apiSecret = process.env.CLOUDINARY_API_SECRET;
199209

200210
if ( !cloudName ) {
201-
throw new Error('A Cloudinary Cloud Name is required. Please set cloudName input or use the environment variable CLOUDINARY_CLOUD_NAME');
211+
utils.build.failBuild(ERROR_CLOUD_NAME_REQUIRED);
212+
return;
202213
}
203214

204215
configureCloudinary({

src/lib/cloudinary.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { JSDOM } = require('jsdom');
55
const cloudinary = require('cloudinary').v2;
66

77
const { isRemoteUrl, determineRemoteUrl } = require('./util');
8+
const { ERROR_CLOUD_NAME } = require('../data/errors');
89

910
/**
1011
* getCloudinary
@@ -74,7 +75,7 @@ async function getCloudinaryUrl(options = {}) {
7475
const canSignUpload = apiKey && apiSecret;
7576

7677
if ( !cloudName ) {
77-
throw new Error('Cloudinary Cloud Name required.');
78+
throw new Error(ERROR_CLOUD_NAME);
7879
}
7980

8081
if ( deliveryType === 'upload' && !canSignUpload && !uploadPreset ) {

0 commit comments

Comments
 (0)