-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_unuse_image.js
198 lines (194 loc) · 6.46 KB
/
find_unuse_image.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
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
var path = require('path');
var fs = require('fs');
//要遍历的文件夹所在的路径
var rnRootFilePath = path.resolve('../ReactNative/');
var imgFilePath = path.resolve('../img/');
const filterImage = [".JPEG",".jpeg",".JPG",".jpg",".png",".gif",".webp",".svg"]
let allImages = [];
let allUsedImagesSet = new Set()
var readdir = promisify(fs.readdir);
var stat = promisify(fs.stat);
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function promisify(fn) {
return function() {
var args = arguments;
return new Promise(function(resolve, reject) {
[].push.call(args, function(err, result) {
if(err) {
reject(err);
}else {
resolve(result);
}
});
fn.apply(null, args);
});
}
}
/**
*筛选未使用的图片资源
*
* @param {*} arr1 所有的图片资源数组
* @param {*} arr2 已使用的图片资源数组
* @returns
*/
function filterUnusedImages(arr1,arr2){
var result=[];
arr1.forEach(function(x){
let has = arr2.some((info)=>{
let match = x.path.endsWith(info)
if (!match) {
let splitIndex = info.indexOf('.')
let twoImage = info.substring(0,splitIndex)+'@2x'+info.substring(splitIndex)
let threeImage = info.substring(0,splitIndex)+'@3x'+info.substring(splitIndex)
match = x.path.endsWith(twoImage) || x.path.endsWith(threeImage)
}
return match
})
if (!has) {
result.push(x);
} else {
return;
}
})
return result;
}
/**
*判断文件是否是图片
*
* @param {*} string 文件路径
* @returns
*/
function contantImage(string){
return filterImage.some(function(value){return string.endsWith(value)});
}
/**
*文件大小单位转换
*
* @param {*} size 文件大小(byte)
* @returns
*/
function calculateSize(size) {
if (!size || size < 0) {
return '0';
}
if (size > 1024 * 1024) {
return Math.ceil(size/(1024 * 1024)) + 'M';
} else if (size > 1024) {
return Math.ceil(size/1024) + 'KB';
} else {
return size + 'B'
}
}
/**
*遍历文件夹下的所有文件
*
* @param {*} file 待遍历的文件夹路径
* @param {*} callback
* @returns
*/
function readDirRecur(file, callback) {
return readdir(file).then(function(files) {
files = files.map(function(filename) {
//获取当前文件的绝对路径
var fullPath = path.join(file, filename);
//fs.stat 根据文件路径获取文件信息,返回一个fs.Stats对象
return stat(fullPath).then(function(stats) {
if (stats.isDirectory()) {
return readDirRecur(fullPath, callback);
}else{
if(filename[0] == '.'){
// console.log(fullPath + ' 是隐藏文件 忽略');
}else{
return dealFile(filename,fullPath,stats,callback)
}
}
})
});
return Promise.all(files);
});
}
/**
*分门别类处理文件-图片存储到图片数组、JS文件查找使用的图片资源存到使用中的图片数组中
*
* @param {*} filename
* @param {*} fullPath
* @param {*} stats
* @param {*} callback
*/
function dealFile(filename,fullPath,stats,callback) {
if (contantImage(fullPath)) {
allImages.push({
path: fullPath,
size: calculateSize(stats.size),
byteSize: stats.size
})
callback(filename, fullPath);
} else if (fullPath.endsWith('.js')) {
var fileContent = fs.readFileSync(fullPath, 'utf-8');
let ph = /require\([^\)]+\)/g
var matchResult = fileContent.match(ph)
if (matchResult) {
var requirePaths = (matchResult.toString()).split(',')
requirePaths.forEach((imagePath)=>{
var imageIndex = imagePath.search(/\/+[^.].+[png|PNG|jpeg|GPEG|gif|GIF|jpg|JPG|webp|WEBP|svg|SVG]/)
if (imageIndex != -1) {
var imageName = imagePath.substring(imageIndex,imagePath.length-2)
var images = imageName.split('/')
var image = images[images.length-1]
if (!allUsedImagesSet.has(image)) {
allUsedImagesSet.add(image)
}
} else {
// console.log('非图片资源')
}
})
}
callback(filename, fullPath);
} else {
// console.log(fullPath + '非目标文件 忽略')
}
}
readDirRecur(imgFilePath, function(item, fullPath) {
// console.log(fullPath);
}).then(function() {
readDirRecur(rnRootFilePath, function(item, fullPath) {
// console.log(fullPath);
}).then(function() {
let allUsedImages = Array.from(allUsedImagesSet)
let unUsedImages = filterUnusedImages(allImages,allUsedImages)
unUsedImages.forEach((info)=>{
console.log(info)
})
console.log('所有的图片资源数量:' + allImages.length)
console.log('使用的图片资源数量:' + allUsedImages.length)
console.log('未使用的图片资源数量:' + unUsedImages.length)
console.log('done');
rl.question(`是否删除未使用的图片(yes/no)?`, (commands) => {
if (commands == 'yes') {
let delectSize = 0;
let delectCount = 0;
unUsedImages.forEach((info)=>{
if (info.path) {
console.log('删除 '+info.path);
fs.unlinkSync(info.path)
delectSize += info.byteSize;
delectCount++;
}
})
console.log('共删除了'+ delectCount +'个文件,总共节省了'+calculateSize(delectSize))
} else if (commands != 'no') {
console.log(commands +'是无效指令')
}
rl.close();
});
}).catch(function(err) {
console.log(err);
});
}).catch(function(err) {
console.log(err);
}
);