-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
44 lines (36 loc) · 1.31 KB
/
index.js
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
const { optimize } = require('svgo');
const fs = require('fs').promises;
const path = require('path');
const svgDirPath = './covers/svg';
const outputJsonPath = './optimized.json';
const startTime = Date.now();
fs.readdir(svgDirPath)
.then(files => {
const svgFiles = files.filter(file => path.extname(file) === '.svg');
const processingPromises = svgFiles.map(file => {
const svgFilePath = path.join(svgDirPath, file);
return fs.readFile(svgFilePath, 'utf-8')
.then(svgData => {
const result = optimize(svgData, { path: svgFilePath });
return fs.writeFile(svgFilePath, result.data)
.then(() => {
console.log(`Optimized: ${file}`);
});
});
});
return Promise.all(processingPromises);
})
.then(() => {
const endTime = Date.now();
const timeTaken = endTime - startTime;
const resultJson = {
timeTaken: `${timeTaken} ms`
};
return fs.writeFile(outputJsonPath, JSON.stringify(resultJson, null, 2))
.then(() => {
console.log(`Time taken: ${timeTaken} ms`);
});
})
.catch(err => {
console.error('Error:', err);
});