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 +1 -1
- package/tasks/grab-global-images.js +117 -133
package/package.json
CHANGED
|
@@ -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
|
-
|
|
7
|
-
if (!node || typeof node !== 'object') {
|
|
8
|
-
return found;
|
|
9
|
-
}
|
|
7
|
+
const fetchWithRetry = createRetryFetch();
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
function collectUseCdnImages(node, found = []) {
|
|
10
|
+
if (!node || typeof node !== 'object') {
|
|
11
|
+
return found;
|
|
12
|
+
}
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
14
|
+
if (node.useCDN && (node.img || node.svg || node.url)) {
|
|
15
|
+
found.push(node);
|
|
16
|
+
return found;
|
|
17
|
+
}
|
|
20
18
|
|
|
21
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
|
|
125
|
+
// get all picture & backgroundImage objects with useCDN: true
|
|
126
|
+
const useCdnImages = collectUseCdnImages(config);
|
|
143
127
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
128
|
+
if (useCdnImages.length === 0) {
|
|
129
|
+
console.log('No global images to download');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
148
132
|
|
|
149
|
-
|
|
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
|
}
|