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 +1 -1
- package/tasks/grab-global-images.js +117 -121
package/package.json
CHANGED
|
@@ -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
|
-
|
|
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 (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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
125
|
+
// get all picture & backgroundImage objects with useCDN: true
|
|
126
|
+
const useCdnImages = collectUseCdnImages(config);
|
|
131
127
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
128
|
+
if (useCdnImages.length === 0) {
|
|
129
|
+
console.log('No global images to download');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
136
132
|
|
|
137
|
-
|
|
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
|
}
|