mod-build 4.0.106-beta.4 → 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.4",
3
+ "version": "4.0.106-beta.6",
4
4
  "description": "Share components for S3 sites.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -1,141 +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)) {
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 } = 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 (imagesToDownload.length === 0) {
64
- return;
65
- }
66
-
67
- // download the images
68
- let imageDirs = await Promise.all(imagesToDownload.map((url) => downloadImage(url)));
69
- // add the image dirs to gitignore
70
- await addFilesToGitignore([...imageDirs.filter(Boolean)], 'Global images');
71
-
72
- // now in all src, srcset and href values add '/assets' in front of them
73
- if (img) {
74
- if (img.attributes && img.attributes.src) {
75
- img.attributes.src = `/${defaultSettings.assetsSubfolder}${img.attributes.src}`;
76
- }
77
- }
78
-
79
- if (svg) {
80
- if (svg.use && svg.use.attributes && svg.use.attributes.href) {
81
- svg.use.attributes.href = `/${defaultSettings.assetsSubfolder}${svg.use.attributes.href}`;
82
- }
83
- }
84
-
85
- if (source && Array.isArray(source)) {
86
- source.forEach((item) => {
87
- if (item.attributes && item.attributes.srcset) {
88
- item.attributes.srcset = `/${defaultSettings.assetsSubfolder}${item.attributes.srcset}`;
89
- }
90
- });
91
- }
92
-
93
- // remove picture.useCDN
94
- 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
+ }
95
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;
96
90
  }
97
91
 
98
92
  async function downloadImage(url) {
99
- // first check if the image already exists in public/assets folder
100
- const imagePath = path.join(defaultSettings.publicFolder, defaultSettings.assetsSubfolder, url);
101
- if (fs.existsSync(imagePath)) {
102
- console.log(`Image ${url} already exists`);
103
- return null;
104
- }
105
- // download the image and put in imagePath
106
- const fileUrl = `https://${defaultSettings.nodeEnv}/quote/resources/assets/${url}`
107
- try {
108
- const response = await fetch(fileUrl);
109
- if (!response.ok) {
110
- throw new Error(`Error downloading image ${fileUrl}: ${response.statusText}`);
111
- }
112
- const buffer = Buffer.from(await response.arrayBuffer());
113
- fs.mkdirSync(path.dirname(imagePath), { recursive: true });
114
- fs.writeFileSync(imagePath, buffer);
115
- console.log(`Downloaded image ${fileUrl}`);
116
- return imagePath;
117
- } catch (error) {
118
- throw new Error(`Error downloading image ${fileUrl}: ${error}`);
119
- }
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
+ }
120
114
  }
121
115
 
122
- export default async function(config) {
123
- console.log('Starting grab-global-images...');
124
- // check if assets/images exists in public folder
125
- const imagesPath = path.join(defaultSettings.publicFolder, defaultSettings.assetsSubfolder, defaultSettings.imagesSubfolder);
126
- if (!fs.existsSync(imagesPath)) {
127
- fs.mkdirSync(imagesPath, { recursive: true });
128
- }
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
+ }
129
124
 
130
- const pictureObjects = getPictureObjectsWithUseCDN(config);
125
+ // get all picture & backgroundImage objects with useCDN: true
126
+ const useCdnImages = collectUseCdnImages(config);
131
127
 
132
- if (pictureObjects.length === 0) {
133
- console.log('No global images to download');
134
- return;
135
- }
128
+ if (useCdnImages.length === 0) {
129
+ console.log('No global images to download');
130
+ return;
131
+ }
136
132
 
137
- 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)));
138
135
 
139
-
140
- console.log('Finished grab-global-images...');
136
+ console.log('Finished grab-global-images...');
141
137
  }