-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathconvert-docs-images.js
More file actions
347 lines (280 loc) · 9.13 KB
/
Copy pathconvert-docs-images.js
File metadata and controls
347 lines (280 loc) · 9.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env node
/**
* Convert Images Script
*
* Converts PNG/JPG images to WebP format across the website
* - static/screenshots/
* - content/en/ page bundle images
*
* Usage: node convert-docs-images.js [--dry-run]
*/
const fs = require("fs");
const path = require("path");
const sharp = require("sharp");
const QUALITY = 80;
// Detect if running in CI environment
const isCI = process.env.CI === "true" || process.env.GITHUB_ACTIONS === "true";
// Color codes for terminal output (disabled in CI)
const colors = isCI
? { reset: "", red: "", green: "", yellow: "", blue: "", cyan: "", dim: "" }
: {
reset: "\x1b[0m",
red: "\x1b[31m",
green: "\x1b[32m",
yellow: "\x1b[33m",
blue: "\x1b[34m",
cyan: "\x1b[36m",
dim: "\x1b[2m",
};
const dryRun = process.argv.includes("--dry-run");
// Directories to process
const DIRECTORIES = [
path.join(process.cwd(), "static", "screenshots"),
path.join(process.cwd(), "content", "en"),
];
// Directories to exclude (in addition to tmp/)
const EXCLUDE_DIRS = ["tmp", "node_modules", "themes", "favicons"];
// Files to exclude (Hugo/Docsy requires these to remain in original format)
const EXCLUDE_FILES = ["featured-background", "background"];
function log(message, color = "reset") {
if (isCI) {
console.log(message);
} else {
console.log(`${colors[color]}${message}${colors.reset}`);
}
}
function formatSize(bytes) {
if (bytes < 1024) return `${bytes} B`;
const kb = bytes / 1024;
if (kb < 1024) return `${kb.toFixed(0)} KB`;
const mb = kb / 1024;
return `${mb.toFixed(2)} MB`;
}
async function getFileSize(filePath) {
try {
const stats = fs.statSync(filePath);
return stats.size;
} catch {
return 0;
}
}
async function convertImage(inputFile) {
const ext = path.extname(inputFile).toLowerCase();
// Skip if not a convertible format
if (![".png", ".jpg", ".jpeg"].includes(ext)) {
return null;
}
const outputFile = inputFile.replace(/\.(png|jpg|jpeg)$/i, ".webp");
const originalSize = await getFileSize(inputFile);
log(` Converting: ${path.relative(process.cwd(), inputFile)}`, "cyan");
if (dryRun) {
log(
` [DRY RUN] Would convert to: ${path.relative(
process.cwd(),
outputFile
)}`,
"yellow"
);
return { input: inputFile, output: outputFile, originalSize, newSize: 0 };
}
try {
await sharp(inputFile).webp({ quality: QUALITY }).toFile(outputFile);
const newSize = await getFileSize(outputFile);
// Remove original file
fs.unlinkSync(inputFile);
log(` ✓ Converted to: ${path.basename(outputFile)}`, "green");
return { input: inputFile, output: outputFile, originalSize, newSize };
} catch (err) {
log(` ✗ Error: ${err.message}`, "red");
return null;
}
}
function findImages(dir, results = []) {
if (!fs.existsSync(dir)) {
return results;
}
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
// Skip excluded directories
if (entry.isDirectory()) {
if (EXCLUDE_DIRS.includes(entry.name)) {
continue;
}
findImages(fullPath, results);
} else if (entry.isFile()) {
const ext = path.extname(entry.name).toLowerCase();
const basename = path.basename(entry.name, ext);
// Skip excluded files (e.g., background images used by Hugo/Docsy)
const isExcluded = EXCLUDE_FILES.some((pattern) =>
basename.includes(pattern)
);
if ([".png", ".jpg", ".jpeg"].includes(ext) && !isExcluded) {
results.push(fullPath);
}
}
}
return results;
}
async function updateReferences(conversions) {
if (conversions.length === 0) return;
log("\nUpdating file references...", "blue");
// Find all markdown and HTML files that might reference images
const contentDir = path.join(process.cwd(), "content");
const markdownFiles = findMarkdownFiles(contentDir);
for (const mdFile of markdownFiles) {
let content = fs.readFileSync(mdFile, "utf8");
let modified = false;
for (const { input, output } of conversions) {
// Get relative paths for replacement
const oldBasename = path.basename(input);
const newBasename = path.basename(output);
// Also handle paths relative to static/
const oldStaticPath = input.includes("/static/")
? "/" + path.relative(path.join(process.cwd(), "static"), input)
: null;
const newStaticPath = oldStaticPath
? oldStaticPath.replace(/\.(png|jpg|jpeg)$/i, ".webp")
: null;
// Replace basename references (for page bundle images)
if (content.includes(oldBasename)) {
content = content.replace(
new RegExp(escapeRegExp(oldBasename), "g"),
newBasename
);
modified = true;
}
// Replace static path references (for /screenshots/ etc)
if (oldStaticPath && content.includes(oldStaticPath)) {
content = content.replace(
new RegExp(escapeRegExp(oldStaticPath), "g"),
newStaticPath
);
modified = true;
}
}
if (modified && !dryRun) {
fs.writeFileSync(mdFile, content, "utf8");
log(` ✓ Updated: ${path.relative(process.cwd(), mdFile)}`, "green");
} else if (modified && dryRun) {
log(
` [DRY RUN] Would update: ${path.relative(process.cwd(), mdFile)}`,
"yellow"
);
}
}
}
function findMarkdownFiles(dir, results = []) {
if (!fs.existsSync(dir)) return results;
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (!EXCLUDE_DIRS.includes(entry.name)) {
findMarkdownFiles(fullPath, results);
}
} else if (entry.isFile()) {
const ext = path.extname(entry.name).toLowerCase();
if ([".md", ".html"].includes(ext)) {
results.push(fullPath);
}
}
}
return results;
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function printComparisonTable(conversions) {
if (conversions.length === 0 || dryRun) return;
log("\n" + "=".repeat(80), "blue");
log("Image Size Comparison", "blue");
log("=".repeat(80), "blue");
// Table header
const colFile = 40;
const colOrig = 12;
const colNew = 12;
const colRed = 10;
const header = [
"File".padEnd(colFile),
"Original".padStart(colOrig),
"WebP".padStart(colNew),
"Reduction".padStart(colRed),
].join(" │ ");
log("┌" + "─".repeat(header.length + 2) + "┐", "dim");
log("│ " + header + " │", "blue");
log("├" + "─".repeat(header.length + 2) + "┤", "dim");
let totalOriginal = 0;
let totalNew = 0;
for (const { input, originalSize, newSize } of conversions) {
const filename = path.basename(input, path.extname(input));
const reduction =
originalSize > 0 ? ((1 - newSize / originalSize) * 100).toFixed(1) : 0;
totalOriginal += originalSize;
totalNew += newSize;
const row = [
filename.substring(0, colFile).padEnd(colFile),
formatSize(originalSize).padStart(colOrig),
formatSize(newSize).padStart(colNew),
`${reduction}%`.padStart(colRed),
].join(" │ ");
log("│ " + row + " │");
}
log("├" + "─".repeat(header.length + 2) + "┤", "dim");
// Totals row
const totalReduction =
totalOriginal > 0 ? ((1 - totalNew / totalOriginal) * 100).toFixed(1) : 0;
const totalsRow = [
"TOTAL".padEnd(colFile),
formatSize(totalOriginal).padStart(colOrig),
formatSize(totalNew).padStart(colNew),
`${totalReduction}%`.padStart(colRed),
].join(" │ ");
log("│ " + totalsRow + " │", "green");
log("└" + "─".repeat(header.length + 2) + "┘", "dim");
// Summary
const saved = totalOriginal - totalNew;
log(
`\nSpace saved: ${formatSize(saved)} (${totalReduction}% reduction)`,
"green"
);
}
async function main() {
log("=".repeat(60), "blue");
log("Image Conversion Script", "blue");
log("=".repeat(60), "blue");
if (dryRun) {
log("\n[DRY RUN MODE] No files will be modified\n", "yellow");
}
const allImages = [];
for (const dir of DIRECTORIES) {
log(`\nScanning: ${path.relative(process.cwd(), dir) || dir}`, "blue");
const images = findImages(dir);
allImages.push(...images);
log(` Found ${images.length} images`, "cyan");
}
if (allImages.length === 0) {
log("\nNo PNG/JPG images found to convert.", "green");
return;
}
log(`\nTotal images to convert: ${allImages.length}`, "blue");
log("-".repeat(60), "blue");
const conversions = [];
for (const imagePath of allImages) {
const result = await convertImage(imagePath);
if (result) {
conversions.push(result);
}
}
// Update references in markdown files
await updateReferences(conversions);
// Print comparison table
printComparisonTable(conversions);
log("\n" + "=".repeat(60), "blue");
log(`Conversion complete! ${conversions.length} images processed.`, "green");
log("=".repeat(60), "blue");
}
main().catch((err) => {
console.error("Fatal error:", err);
process.exit(1);
});