-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanIcon.js
60 lines (51 loc) · 1.41 KB
/
cleanIcon.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
#!/bin/env node
let path = require("path"), fs = require("fs");
function fromDir(startPath, filter, callback) {
if (!fs.existsSync(startPath)) {
console.log("no dir ", startPath);
return;
}
let files = fs.readdirSync(startPath);
for (let i = 0; i < files.length; i++) {
let filename = path.join(startPath, files[i]);
let stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
fromDir(filename, filter, callback); //recurse
} else if (filter.test(filename)) {
callback(filename, files[i]);
}
}
}
function justPrint(filePath, fileName) {
console.log("-- found: ", fileName.split(".")[0]);
}
let iconNames = [];
function addTolist(filePath, fileName) {
iconNames.push({
iconName: fileName.split(".")[0],
filePath,
used: false
});
}
function checkIfUnsused(filePath, fileName) {
let data = fs.readFileSync(filePath);
iconNames.forEach((icon, index, iconList) => {
if (data.includes(`${icon.iconName}`)) {
console.log("--- icon used", icon.iconName);
iconList[index].used = true;
}
});
}
fromDir("www/svg", /\.svg$/, justPrint);
fromDir("www/svg", /\.svg$/, addTolist);
fromDir("www", /\.(js|css|html)$/, checkIfUnsused);
iconNames.forEach((icon) => {
if (!icon.used) {
console.log("--- will delete unused icon", icon.iconName);
fs.unlink(icon.filePath, (err) => {
if (err) {
console.error(err);
}
});
}
});