最近的一个项目,涉及到一些美术资源的操作,而且是大量的操作,然后产生了,一堆的冗余无用的资源,但是,又不知道哪些是有用到的那些是没有用到的,所以最近抽空写了个工具。
实现的操作环境:nodejs8.11.1 creator版本2.0.9.
var fs = require('fs');
var path = require('path');
var async = require('async');
var walk = require('walk')
let cp = require('child_process');
//遍历的资源路径
var rpath = path.resolve(__dirname, '../assets/modules/')
console.log('cus resources path:', rpath)
//遍历预制体资源路径
var firepath = path.resolve(__dirname, '../assets/')
var files = [], fires = [], uuids = [], notExits = [], allfile = {}
getFileList(rpath)
function getFileList(rpath){
let walker = walk.walk(rpath, { followLinks: false });
walker.on('file', function(roots, stat, next) {
files.push(roots + '/' + stat.name);
next();
});
walker.on('end', function() {
for (var i = 0; i < files.length; i ++) {
var f = files[i]
if (f.indexOf("png.meta") >= 0) {
let uuid
let subMetas = JSON.parse(fs.readFileSync(f, 'UTF-8').toString()).subMetas
for (let h in subMetas) {
uuid = subMetas[h].uuid
break
}
uuids.push(uuid)
allfile[uuid] = f
}
}
console.log('获取到所有资源列表的uuid,长度-- ' + uuids.length)
getFireFile(uuids)
});
}
let prefab = []
function getFireFile (uuids) {
let walker = walk.walk(firepath, { followLinks: false });
walker.on('file', function(roots, stat, next) {
if (stat.name.indexOf(".prefab") >= 0) {
prefab.push(roots + '/' + stat.name);
}
next();
});
walker.on('end', function() {
console.log('遍历所有预制体文件完成!')
console.log('开始遍历资源文件的使用情况---')
for (let i in uuids) {
let used = false
for (let j = 0; j < prefab.length; j ++) {
let f = prefab[j]
let uuid = uuids[i]
if (f.indexOf(".prefab") >= 0) {
let text = fs.readFileSync(f, 'UTF-8').toString()
if (text.indexOf(uuid) >= 0) {
delete allfile[uuid]
used = true
break
}
}
}
if (! used) {
notExits.push(uuids[i])
}
}
console.log('未使用的资源uuids --' + notExits.length)
writeFile(allfile)
});
}
function writeFile (res) {
let str = '无用的资源如下:\n'
for (let i in res) {
str += `uuid : ${i} <-> path : ${res[i]} \n`
// console.log(`正在删除文件 ${res[i]}`)//这里的删除资源要小心,一旦删除了,就找不到了
// fs.unlinkSync(res[i])
}
let w = __dirname + '/uuids.txt'
console.log(`正在写入:${w}`)//如果资源量不大的话,会在同级目录写入日志,有哪些资源,没用到的
fs.writeFileSync(w, str);
}
更多干活:it菜鸟