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
| const fs = require('fs'); const path = require('path'); const FileHelper = require('./FileHelper'); const Utils = require('./Utils');
let compareSize = 0;
let AssetWHSize = { fileMap: null,
start(sourceFile, destFile,size) { if (!sourceFile || sourceFile.length <= 0 || !destFile || destFile.length <= 0) { console.error('Cleaner: invalid source or dest'); return; } compareSize = size; this.fileMap = new Map();
sourceFile = FileHelper.getFullPath(sourceFile); destFile = FileHelper.getFullPath(destFile);
this.lookupAssetDir(sourceFile, null); let outStr = this.getSortedResult(this.fileMap, sourceFile); FileHelper.writeFile(destFile, outStr); },
lookupAssetDir(srcDir, callback) { if (!srcDir || !fs.existsSync(srcDir)) { console.error("AssetWHSize: invalid srcDir=" + srcDir); return; }
let files = fs.readdirSync(srcDir); for (let i = 0, len = files.length; i < len; i++) { let file = files[i]; let curPath = path.join(srcDir, file); let stats = fs.statSync(curPath);
if (stats.isDirectory()) { this.lookupAssetDir(curPath); continue; }
let pathObj = path.parse(curPath); let value = this.fileMap.get(pathObj.ext); if (!value) { value = new Map(); this.fileMap.set(pathObj.ext, value); } if ([".png", ".jpg", ".webp"].includes(pathObj.ext)) { let dimensions = FileHelper.getImageWH(curPath, pathObj.ext); if(dimensions.max>compareSize){ value.set(curPath, {path: curPath, size: stats.size, dimensions, max: dimensions.max}); }
} } },
getSortedResult(fileMap, srcDir) { let allTypes = [];
fileMap.forEach((files, ext) => { let {totalSize, outStr} = this.formatByByte(files); allTypes.push({size: parseFloat(totalSize), ext, count: files.size, outStr}); });
allTypes.sort(function (a, b) { return b.size - a.size; });
let content = ''; let allSize = 0;
allTypes = allTypes.filter((value)=>{ return value.count>0 }) for (let i = 0, len = allTypes.length; i < len; i++) { let data = allTypes[i]; content += '类型=' + data.ext + ', 个数=' + data.count + ', 占用空间=' + data.size + 'MB'; if (data.max > 0) { content += ', 宽度=' + data.width + 'px' + ', 高度=' + data.height + 'px'; } content += '\n'; allSize += data.size; }
let totalStr = '总空间=' + allSize.toFixed(4) + 'MB, ' + '目录=' + srcDir + '\n\n'; totalStr += content + '\n';
for (let i = 0, len = allTypes.length; i < len; i++) { let data = allTypes[i]; totalStr += data.outStr + '\n'; }
return totalStr; },
formatByByte(files) { let totalSize = 0; let newFiles = []; files.forEach(function (file, path) { totalSize += file.size; newFiles.push(file); }); newFiles.sort(function (a, b) { return b.max - a.max; });
let outStr = ''; for (let i = 0, len = newFiles.length; i < len; i++) { let file = newFiles[i]; outStr += '最大边=' + file.dimensions.max + '宽=' + file.dimensions.width + '高=' + file.dimensions.height + 'px, 文件=' + file.path; outStr += '\n'; } totalSize = Utils.byte2MbStr(totalSize); return {totalSize, outStr}; },
};
module.exports = AssetWHSize;
|