This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
95 lines (84 loc) · 2.51 KB
/
build.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
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
/*
* Copyright (c) 2014. LetsBlumIt Corp.
*/
'use strict';
var fs = require('fs'),
util = require('util'),
path = require('path'),
Svgo = require('svgo'),
async = require('async'),
chalk = require('chalk'),
dir = require('node-dir'),
rootDir = process.argv[2],
rootPath = path.join(__dirname, rootDir);
dir.files(rootPath, function (err, files) {
if (err) {
throw err;
}
var allZero, finishing = 3, svgo = new Svgo();
async.doUntil(function (callback1) {
allZero = true;
async.eachSeries(files, function (file, callback2) {
if (path.extname(file).toLowerCase() === '.svg') {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
callback2(err);
} else {
try {
var optimizeStat, svgData = data;
async.doUntil(function (callback3) {
svgo.optimize(svgData, function (results) {
if (results.error) {
callback3(results.error);
} else {
optimizeStat = (svgData.length - results.data.length) / svgData.length * 100;
svgData = results.data;
callback3();
}
});
}, function () {
if (optimizeStat) {
allZero = false;
util.log(chalk.red(path.basename(file) + ' ✗ ' + optimizeStat));
} else {
util.log(chalk.green(path.basename(file) + ' ✓ '));
}
return optimizeStat === 0;
}, function (err) {
if (err) {
callback2(err);
} else {
fs.writeFile(file, svgData, function (err) {
if (err) {
callback2(err);
} else {
callback2();
}
});
}
});
} catch (err) {
callback2(err);
}
}
});
} else {
callback2();
}
}, function (err) {
callback1(err);
});
}, function () {
util.log(chalk.yellow('(⌐■_■) iteration clean: ' + allZero + ' finishing:' + finishing));
if (allZero) {
finishing--;
}
return finishing === 0;
}, function (err) {
if (err) {
util.log(chalk.red(' (╯︵╰,) ' + err));
} else {
util.log(chalk.green('all svgs optimized! ٩(^‿^)۶'));
}
});
});