Repository navigation
Expand file tree
/
Copy pathcompressSvg.ts
More file actions
415 lines (348 loc) · 13.1 KB
/
Copy pathcompressSvg.ts
File metadata and controls
415 lines (348 loc) · 13.1 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import GithubUtils from '@github/libs/GithubUtils';
import type {PluginConfig} from 'svgo';
import * as github from '@actions/github';
import * as fs from 'fs';
import * as path from 'path';
import {optimize} from 'svgo';
type CompressionResult = {
filePath: string;
originalSize: number;
compressedSize: number;
savings: number;
savingsPercent: number;
};
type CompressionSummary = {
totalFiles: number;
totalCompressedFilesLength: number;
totalOriginalSize: number;
totalCompressedSize: number;
totalSavings: number;
totalSavingsPercent: number;
results: CompressionResult[];
ignoredFiles: string[];
};
// Suffix for files to be ignored from compression eg. file-ignore-compression.svg
const IGNORE_SUFFIX = '-ignore-compression';
// SVGO Default plugins
const svgoConfig: {plugins: PluginConfig[]} = {
plugins: [
'removeDoctype',
'removeXMLProcInst',
'removeComments',
'removeDeprecatedAttrs',
'removeMetadata',
'removeEditorsNSData',
'cleanupAttrs',
'mergeStyles',
// 'inlineStyles', // Cause issues with fill on Android
'minifyStyles',
'cleanupIds',
'removeUselessDefs',
'cleanupNumericValues',
'convertColors',
'removeNonInheritableGroupAttrs',
'removeUnknownsAndDefaults',
'removeUselessStrokeAndFill',
'cleanupEnableBackground',
'removeHiddenElems',
'removeEmptyText',
'convertShapeToPath',
'convertEllipseToCircle',
'moveElemsAttrsToGroup',
'moveGroupAttrsToElems',
'collapseGroups',
'convertPathData',
'convertTransform',
'removeEmptyAttrs',
'removeEmptyContainers',
'mergePaths',
'removeUnusedNS',
'sortAttrs',
'sortDefsChildren',
'removeDesc',
],
};
function findSvgFiles(dir: string): string[] {
const svgFiles: string[] = [];
function scanDirectory(currentDir: string) {
const items = fs.readdirSync(currentDir, {withFileTypes: true});
for (const item of items) {
const fullPath = path.join(currentDir, item.name);
if (item.isDirectory()) {
scanDirectory(fullPath);
} else if (item.isFile() && path.extname(item.name).toLowerCase() === '.svg') {
svgFiles.push(fullPath);
}
}
}
scanDirectory(dir);
return svgFiles;
}
function formatBytes(bytes: number): string {
return (bytes / 1024).toFixed(2);
}
function compressSvgFile(filePath: string, isSavingFile: boolean): CompressionResult {
const originalContent = fs.readFileSync(filePath, 'utf8');
const originalSize = Buffer.byteLength(originalContent, 'utf8');
try {
let currentContent = originalContent;
let currentSize = originalSize;
let totalSavings = 0;
// Perform 5 iterations, as additional savings could be gained with another pass (2–3 are usually enough).
// For checking if file is compressed, we only need to run once.
const maxPasses = isSavingFile ? 5 : 1;
for (let pass = 1; pass <= maxPasses; pass++) {
const result = optimize(currentContent, {
path: filePath,
...svgoConfig,
});
const compressedContent = result.data;
const compressedSize = Buffer.byteLength(compressedContent, 'utf8');
const passSavings = currentSize - compressedSize;
if (passSavings <= 0) {
break;
}
totalSavings += passSavings;
currentContent = compressedContent;
currentSize = compressedSize;
if (pass === maxPasses || passSavings < 10) {
break;
}
}
const finalSavingsPercent = originalSize > 0 ? (totalSavings / originalSize) * 100 : 0;
if (isSavingFile) {
fs.writeFileSync(filePath, currentContent, 'utf8');
}
return {
filePath,
originalSize,
compressedSize: currentSize,
savings: totalSavings,
savingsPercent: finalSavingsPercent,
};
} catch (error) {
console.error(`❌ Error compressing ${filePath}:`, error);
return {
filePath,
originalSize,
compressedSize: originalSize,
savings: 0,
savingsPercent: 0,
};
}
}
function validateSvgFiles(filePaths: string[]): string[] {
const validFiles: string[] = [];
const errors: string[] = [];
for (const filePath of filePaths) {
const resolvedPath = path.resolve(filePath);
if (!fs.existsSync(resolvedPath)) {
errors.push(`❌ File does not exist: ${filePath}`);
continue;
}
const stat = fs.statSync(resolvedPath);
if (!stat.isFile()) {
errors.push(`❌ Not a file: ${filePath}`);
continue;
}
if (path.extname(filePath).toLowerCase() !== '.svg') {
errors.push(`❌ Not an SVG file: ${filePath}`);
continue;
}
validFiles.push(resolvedPath);
}
if (errors.length) {
console.error('Validation errors:');
for (const error of errors) {
console.error(` ${error}`);
}
throw new Error('SVG file validation failed');
}
return validFiles;
}
function createResultsSummary(results: CompressionResult[], ignoredFiles: string[] = []): CompressionSummary {
const compressedFiles = results.filter((r) => !!r.savings);
const totalCompressedFilesLength = compressedFiles.length;
const totalOriginalSize = compressedFiles.reduce((sum, r) => sum + r.originalSize, 0);
const totalCompressedSize = compressedFiles.reduce((sum, r) => sum + r.compressedSize, 0);
const totalSavings = compressedFiles.reduce((sum, r) => sum + r.savings, 0);
const totalSavingsPercent = totalOriginalSize > 0 ? (totalSavings / totalOriginalSize) * 100 : 0;
return {
totalFiles: results.length + ignoredFiles.length,
totalCompressedFilesLength,
totalOriginalSize,
totalCompressedSize,
totalSavings,
totalSavingsPercent,
results,
ignoredFiles,
};
}
function getSummarySavingString({
prefix,
originalSize,
compressedSize,
savings,
savingsPercent,
}: {
prefix: string;
originalSize: number;
compressedSize: number;
savings: number;
savingsPercent: number;
}) {
return `${prefix} ${formatBytes(originalSize)} KB → ${formatBytes(compressedSize)} KB | ${`${formatBytes(savings)} KB (${savingsPercent.toFixed(2)}%)`}`;
}
function logIgnoredFiles(ignoredFiles: string[]) {
if (!ignoredFiles.length) {
return;
}
console.log('\nFiles skipped (ignore-compression):');
for (const filePath of ignoredFiles) {
console.log(`${filePath}: ⏭️ Skipped`);
}
}
function logSummary(summary: CompressionSummary) {
const {totalFiles, totalCompressedFilesLength, totalOriginalSize, totalCompressedSize, totalSavings, totalSavingsPercent, results, ignoredFiles} = summary;
logIgnoredFiles(ignoredFiles);
if (totalCompressedFilesLength) {
console.log('\nFiles compressed:');
for (const result of results) {
const {compressedSize, originalSize, savings, savingsPercent, filePath} = result;
if (!result.savings) {
continue;
}
const prefix = `${filePath}: ✅`;
console.log(
getSummarySavingString({
prefix,
compressedSize,
originalSize,
savings,
savingsPercent,
}),
);
}
const ignoreFilesLength = ignoredFiles.length;
console.log(`\nFiles processed: ${totalFiles}`);
console.log(`Files already properly compressed: ${totalFiles - ignoreFilesLength - totalCompressedFilesLength}`);
console.log(`Files compressed: ${totalCompressedFilesLength}`);
console.log(`Files ignored: ${ignoreFilesLength}`);
console.log(
getSummarySavingString({
prefix: 'Savings:',
originalSize: totalOriginalSize,
compressedSize: totalCompressedSize,
savings: totalSavings,
savingsPercent: totalSavingsPercent,
}),
);
} else {
console.log('\n✅ All files already compressed');
}
}
function logSummaryCheck(summary: CompressionSummary) {
const {totalFiles, totalCompressedFilesLength, results, ignoredFiles} = summary;
console.log('');
for (const result of results) {
const {filePath, savings} = result;
console.log(`${filePath}: ${savings > 0 ? 'Not properly compressed ❌' : 'Compressed ✅'}`);
}
logIgnoredFiles(ignoredFiles);
console.log(`\nFiles processed: ${totalFiles}`);
console.log(`Files ignored: ${ignoredFiles.length}`);
console.log(`Files not properly compressed: ${totalCompressedFilesLength}`);
}
function processFiles(svgFiles: string[], isSavingFile: boolean): CompressionResult[] {
const results: CompressionResult[] = [];
for (const file of svgFiles) {
const result = compressSvgFile(file, isSavingFile);
results.push(result);
}
return results;
}
function compressSvgFiles(regularSvgFiles: string[], ignoredFiles: string[] = []) {
console.log(`🚀 Starting compression ${regularSvgFiles.length} SVG file(s)`);
const results = processFiles(regularSvgFiles, true);
const summary = createResultsSummary(results, ignoredFiles);
logSummary(summary);
return summary;
}
function checkCompressedSvgFiles(regularSvgFiles: string[], ignoredFiles: string[] = []) {
console.log(`🚀 Checking if all SVG files are compressed...`);
const results = processFiles(regularSvgFiles, false);
const summary = createResultsSummary(results, ignoredFiles);
logSummaryCheck(summary);
return summary;
}
async function getChangedSvgFilesFromGithub(): Promise<string[]> {
try {
const pullRequestNumber = github.context.payload.pull_request?.number;
if (!pullRequestNumber) {
console.log('No pull request number found');
return [];
}
const changedFiles = await GithubUtils.getPullRequestChangedSVGFileNames(pullRequestNumber);
const svgFiles = changedFiles
.filter((file) => path.extname(file.toLowerCase()) === '.svg')
.map((file) => path.resolve(file))
.filter((file) => fs.existsSync(file));
console.log(`Found ${svgFiles.length} changed SVG file(s) in PR`);
return svgFiles;
} catch (error) {
console.error('❌ Error getting files from GitHub:', error);
return [];
}
}
function splitFilesBySuffix(files: string[]): {regular: string[]; ignored: string[]} {
const regularFiles: string[] = [];
const ignoredFiles: string[] = [];
for (const file of files) {
const fileName = path.basename(file);
if (fileName.endsWith(`${IGNORE_SUFFIX}.svg`)) {
ignoredFiles.push(file);
} else {
regularFiles.push(file);
}
}
return {regular: regularFiles, ignored: ignoredFiles};
}
async function run(mode: 'directory' | 'files' | 'pullRequest', options?: {targetDir?: string; filePaths?: string[]; token?: string}): Promise<CompressionSummary> {
console.log('🔍 Searching for SVG files...');
switch (mode) {
case 'directory': {
if (!options?.targetDir) {
throw new Error('targetDir is required for directory mode');
}
const svgFiles = findSvgFiles(options.targetDir);
if (!svgFiles.length) {
console.log('❌ No SVG files found in the specified directory.');
}
const {regular: regularSvgFiles, ignored: ignoredFiles} = splitFilesBySuffix(svgFiles);
return compressSvgFiles(regularSvgFiles, ignoredFiles);
}
case 'files': {
if (!options?.filePaths?.length) {
throw new Error('filePaths is required for files mode');
}
const svgFiles = validateSvgFiles(options.filePaths);
if (!svgFiles.length) {
console.log('❌ No valid SVG files provided.');
}
const {regular: regularSvgFiles, ignored: ignoredFiles} = splitFilesBySuffix(svgFiles);
return compressSvgFiles(regularSvgFiles, ignoredFiles);
}
case 'pullRequest': {
const svgFiles = await getChangedSvgFilesFromGithub();
if (!svgFiles.length) {
console.log('❌ No changed SVG files found in Pull Request');
}
const {regular: regularSvgFiles, ignored: ignoredFiles} = splitFilesBySuffix(svgFiles);
return checkCompressedSvgFiles(regularSvgFiles, ignoredFiles);
}
default:
throw new Error(`Unknown compression mode: ${mode as string}`);
}
}
export default run;
export {type CompressionSummary};