mod-build 4.0.106-beta.5 → 4.0.106-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mod-build",
3
- "version": "4.0.106-beta.5",
3
+ "version": "4.0.106-beta.6",
4
4
  "description": "Share components for S3 sites.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -1,153 +1,137 @@
1
- import { defaultSettings } from "../src/data/config";
1
+ import { defaultSettings } from "../src/data/config.js";
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
4
  import addFilesToGitignore from './add-files-to-gitignore.js';
5
+ import { createRetryFetch } from '../src/scripts/retry-fetch.js';
5
6
 
6
- function getPictureObjectsWithUseCDN(node, found = []) {
7
- if (!node || typeof node !== 'object') {
8
- return found;
9
- }
7
+ const fetchWithRetry = createRetryFetch();
10
8
 
11
- if (node.useCDN && (node.img || node.svg || node.url)) {
12
- found.push(node);
13
- }
9
+ function collectUseCdnImages(node, found = []) {
10
+ if (!node || typeof node !== 'object') {
11
+ return found;
12
+ }
14
13
 
15
- if (Array.isArray(node)) {
16
- node.forEach((item) => getPictureObjectsWithUseCDN(item, found));
17
- } else {
18
- Object.values(node).forEach((value) => getPictureObjectsWithUseCDN(value, found));
19
- }
14
+ if (node.useCDN && (node.img || node.svg || node.url)) {
15
+ found.push(node);
16
+ return found;
17
+ }
20
18
 
21
- return found;
19
+ if (Array.isArray(node)) {
20
+ node.forEach((item) => collectUseCdnImages(item, found));
21
+ } else {
22
+ Object.values(node).forEach((value) => collectUseCdnImages(value, found));
23
+ }
24
+
25
+ return found;
22
26
  }
23
27
 
24
- async function fetchGlobalImageAndUpdateSrc(picture) {
25
- const { img, svg, source, url } = picture;
26
- let imagesToDownload = [];
27
- if (img) {
28
- if (img.attributes && img.attributes.src) {
29
- // remove leading slash from src
30
- let src = img.attributes.src;
31
- if (src.startsWith('/')) {
32
- src = src.slice(1);
33
- }
34
- imagesToDownload.push(src);
35
- }
36
- }
37
-
38
- if (svg) {
39
- if (svg.use && svg.use.attributes && svg.use.attributes.href) {
40
- let href = svg.use.attributes.href;
41
- if (href.startsWith('/')) {
42
- href = href.slice(1);
43
- }
44
- href = href.split('#')[0];
45
- if (href) {
46
- imagesToDownload.push(href);
47
- }
48
- }
49
- }
50
-
51
- if (source && Array.isArray(source)) {
52
- source.forEach((item) => {
53
- if (item.attributes && item.attributes.srcset) {
54
- let srcset = item.attributes.srcset;
55
- if (srcset.startsWith('/')) {
56
- srcset = srcset.slice(1);
57
- }
58
- imagesToDownload.push(srcset);
59
- }
60
- });
61
- }
62
-
63
- if (url) {
64
- let bgUrl = url;
65
- if (bgUrl.startsWith('/')) {
66
- bgUrl = bgUrl.slice(1);
67
- }
68
- imagesToDownload.push(bgUrl);
69
- }
70
-
71
- if (imagesToDownload.length === 0) {
72
- return;
73
- }
74
-
75
- // download the images
76
- let imageDirs = await Promise.all(imagesToDownload.map((url) => downloadImage(url)));
77
- // add the image dirs to gitignore
78
- await addFilesToGitignore([...imageDirs.filter(Boolean)], 'Global images');
79
-
80
- // now in all src, srcset and href values add '/assets' in front of them
81
- if (img) {
82
- if (img.attributes && img.attributes.src) {
83
- img.attributes.src = `/${defaultSettings.assetsSubfolder}${img.attributes.src}`;
84
- }
85
- }
86
-
87
- if (svg) {
88
- if (svg.use && svg.use.attributes && svg.use.attributes.href) {
89
- svg.use.attributes.href = `/${defaultSettings.assetsSubfolder}${svg.use.attributes.href}`;
90
- }
91
- }
92
-
93
- if (source && Array.isArray(source)) {
94
- source.forEach((item) => {
95
- if (item.attributes && item.attributes.srcset) {
96
- item.attributes.srcset = `/${defaultSettings.assetsSubfolder}${item.attributes.srcset}`;
97
- }
98
- });
99
- }
100
-
101
- if (url) {
102
- picture.url = `/${defaultSettings.assetsSubfolder}${url}`;
103
- }
104
-
105
- // remove picture.useCDN
106
- delete picture.useCDN;
28
+ function getImagePath(url, stripHash = false) {
29
+ if (!url) {
30
+ return null;
31
+ }
32
+
33
+ url = url.replace(/^\/+/, '');
34
+
35
+ if (stripHash) {
36
+ url = url.split('#')[0];
37
+ }
38
+
39
+ return url;
40
+
41
+ }
42
+
43
+ function addAssetsSubfolder(url) {
44
+ if (!url) {
45
+ return null;
46
+ }
47
+
48
+ return `/${defaultSettings.assetsSubfolder}${url}`;
49
+ }
107
50
 
51
+ async function fetchGlobalImageAndUpdateSrc(picture) {
52
+ const { img, svg, source, url } = picture;
53
+ let imagesToDownload = [];
54
+ if (img) {
55
+ if (img.attributes && img.attributes.src) {
56
+ imagesToDownload.push(getImagePath(img.attributes.src));
57
+ img.attributes.src = addAssetsSubfolder(img.attributes.src);
58
+ }
59
+ }
60
+
61
+ if (svg) {
62
+ if (svg.use && svg.use.attributes && svg.use.attributes.href) {
63
+ imagesToDownload.push(getImagePath(svg.use.attributes.href, true));
64
+ svg.use.attributes.href = addAssetsSubfolder(svg.use.attributes.href);
65
+ }
66
+ }
67
+
68
+ if (source && Array.isArray(source)) {
69
+ source.forEach((item) => {
70
+ if (item.attributes && item.attributes.srcset) {
71
+ imagesToDownload.push(getImagePath(item.attributes.srcset));
72
+ item.attributes.srcset = addAssetsSubfolder(item.attributes.srcset);
73
+ }
74
+ });
75
+ }
76
+
77
+ if (url) {
78
+ imagesToDownload.push(getImagePath(url));
79
+ picture.url = addAssetsSubfolder(url);
80
+ }
81
+
82
+ imagesToDownload = imagesToDownload.filter(Boolean);
83
+
84
+ // download the images
85
+ let downloadedPaths = await Promise.all(imagesToDownload.map((url) => downloadImage(url)));
86
+
87
+ await addFilesToGitignore([...downloadedPaths.filter(Boolean)], 'Global images');
88
+
89
+ delete picture.useCDN;
108
90
  }
109
91
 
110
92
  async function downloadImage(url) {
111
- // first check if the image already exists in public/assets folder
112
- const imagePath = path.join(defaultSettings.publicFolder, defaultSettings.assetsSubfolder, url);
113
- if (fs.existsSync(imagePath)) {
114
- console.log(`Image ${url} already exists`);
115
- return null;
116
- }
117
- // download the image and put in imagePath
118
- const fileUrl = `https://${defaultSettings.nodeEnv}/quote/resources/assets/${url}`
119
- try {
120
- const response = await fetch(fileUrl);
121
- if (!response.ok) {
122
- throw new Error(`Error downloading image ${fileUrl}: ${response.statusText}`);
123
- }
124
- const buffer = Buffer.from(await response.arrayBuffer());
125
- fs.mkdirSync(path.dirname(imagePath), { recursive: true });
126
- fs.writeFileSync(imagePath, buffer);
127
- console.log(`Downloaded image ${fileUrl}`);
128
- return imagePath;
129
- } catch (error) {
130
- throw new Error(`Error downloading image ${fileUrl}: ${error}`);
131
- }
93
+ const imagePath = path.join(defaultSettings.publicFolder, defaultSettings.assetsSubfolder, url);
94
+ if (fs.existsSync(imagePath)) {
95
+ console.log(`Image ${url} already exists`);
96
+ return null;
97
+ }
98
+
99
+ const fileUrl = `https://${defaultSettings.nodeEnv}/quote/resources/assets/${url}`;
100
+ try {
101
+ const response = await fetchWithRetry(fileUrl);
102
+ if (!response.ok) {
103
+ throw new Error(`Error downloading image ${fileUrl}: ${response.statusText}`);
104
+ }
105
+ const buffer = Buffer.from(await response.arrayBuffer());
106
+ fs.mkdirSync(path.dirname(imagePath), { recursive: true });
107
+ fs.writeFileSync(imagePath, buffer);
108
+ console.log(`Downloaded image ${fileUrl}`);
109
+ return imagePath;
110
+ } catch (error) {
111
+ console.error(`Error downloading image ${fileUrl}: ${error.message}`);
112
+ throw error;
113
+ }
132
114
  }
133
115
 
134
- export default async function(config) {
135
- console.log('Starting grab-global-images...');
136
- // check if assets/images exists in public folder
137
- const imagesPath = path.join(defaultSettings.publicFolder, defaultSettings.assetsSubfolder, defaultSettings.imagesSubfolder);
138
- if (!fs.existsSync(imagesPath)) {
139
- fs.mkdirSync(imagesPath, { recursive: true });
140
- }
116
+ export default async function (config) {
117
+ console.log('Starting grab-global-images...');
118
+
119
+ // create public/assets/images folder if it doesnt exist
120
+ const imagesPath = path.join(defaultSettings.publicFolder, defaultSettings.assetsSubfolder, defaultSettings.imagesSubfolder);
121
+ if (!fs.existsSync(imagesPath)) {
122
+ fs.mkdirSync(imagesPath, { recursive: true });
123
+ }
141
124
 
142
- const pictureObjects = getPictureObjectsWithUseCDN(config);
125
+ // get all picture & backgroundImage objects with useCDN: true
126
+ const useCdnImages = collectUseCdnImages(config);
143
127
 
144
- if (pictureObjects.length === 0) {
145
- console.log('No global images to download');
146
- return;
147
- }
128
+ if (useCdnImages.length === 0) {
129
+ console.log('No global images to download');
130
+ return;
131
+ }
148
132
 
149
- await Promise.all(pictureObjects.map((picture) => fetchGlobalImageAndUpdateSrc(picture)));
133
+ // download the images in public folder and update the src & href values to use local path
134
+ await Promise.all(useCdnImages.map((image) => fetchGlobalImageAndUpdateSrc(image)));
150
135
 
151
-
152
- console.log('Finished grab-global-images...');
136
+ console.log('Finished grab-global-images...');
153
137
  }