@@ -4,6 +4,7 @@ const glob = require('glob');
4
4
5
5
const { configureCloudinary, updateHtmlImagesToCloudinary, getCloudinaryUrl } = require ( './lib/cloudinary' ) ;
6
6
const { PUBLIC_ASSET_PATH } = require ( './data/cloudinary' ) ;
7
+ const { ERROR_CLOUD_NAME_REQUIRED } = require ( './data/errors' ) ;
7
8
8
9
const CLOUDINARY_ASSET_DIRECTORIES = [
9
10
{
@@ -19,7 +20,7 @@ const CLOUDINARY_ASSET_DIRECTORIES = [
19
20
*/
20
21
21
22
module . exports = {
22
- async onPreBuild ( { netlifyConfig, constants, inputs } ) {
23
+ async onPreBuild ( { netlifyConfig, constants, inputs, utils } ) {
23
24
const host = process . env . DEPLOY_PRIME_URL || process . env . NETLIFY_HOST ;
24
25
25
26
if ( ! host ) {
@@ -50,7 +51,8 @@ module.exports = {
50
51
const apiSecret = process . env . CLOUDINARY_API_SECRET ;
51
52
52
53
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 ;
54
56
}
55
57
56
58
configureCloudinary ( {
@@ -62,34 +64,41 @@ module.exports = {
62
64
const imagesDirectory = glob . sync ( `${ PUBLISH_DIR } /images/**/*` ) ;
63
65
const imagesFiles = imagesDirectory . filter ( file => ! ! path . extname ( file ) ) ;
64
66
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 ;
70
68
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 } ` ;
79
75
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
+ }
86
95
87
96
netlifyConfig . build . environment . CLOUDINARY_ASSETS = {
88
97
images
89
98
}
90
99
} ,
91
100
92
- async onBuild ( { netlifyConfig, constants, inputs } ) {
101
+ async onBuild ( { netlifyConfig, constants, inputs, utils } ) {
93
102
const host = process . env . DEPLOY_PRIME_URL || process . env . NETLIFY_HOST ;
94
103
95
104
if ( ! host ) {
@@ -111,7 +120,8 @@ module.exports = {
111
120
const apiSecret = process . env . CLOUDINARY_API_SECRET ;
112
121
113
122
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 ;
115
125
}
116
126
117
127
configureCloudinary ( {
@@ -177,7 +187,7 @@ module.exports = {
177
187
// Post build looks through all of the output HTML and rewrites any src attributes to use a cloudinary URL
178
188
// This only solves on-page references until any JS refreshes the DOM
179
189
180
- async onPostBuild ( { netlifyConfig, constants, inputs } ) {
190
+ async onPostBuild ( { netlifyConfig, constants, inputs, utils } ) {
181
191
const host = process . env . DEPLOY_PRIME_URL || process . env . NETLIFY_HOST ;
182
192
183
193
if ( ! host ) {
@@ -198,7 +208,8 @@ module.exports = {
198
208
const apiSecret = process . env . CLOUDINARY_API_SECRET ;
199
209
200
210
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 ;
202
213
}
203
214
204
215
configureCloudinary ( {
0 commit comments