cocos 递归给bundle下的所有文件夹以及文件添加前缀
可以使用以下递归函数来给 bundle 下的所有文件夹和文件添加前缀:
function addPrefixToBundle(bundle, prefix) {
const fs = require('fs');
const path = require('path');
const stats = fs.statSync(bundle);
if (stats.isDirectory()) {
const files = fs.readdirSync(bundle);
files.forEach(file => {
const filePath = path.join(bundle, file);
addPrefixToBundle(filePath, prefix);
});
} else {
const newFilePath = bundle.replace(path.basename(bundle), `${prefix}${path.basename(bundle)}`);
fs.renameSync(bundle, newFilePath);
}
}
使用方法:
const bundlePath = 'path/to/bundle';
const prefix = 'new_prefix_';
addPrefixToBundle(bundlePath, prefix);
这个函数会递归遍历传入的 bundle 文件夹,并对其中的所有文件和文件夹进行操作。如果是文件夹,则继续递归处理,如果是文件,则给文件名添加前缀,并使用 fs.renameSync() 函数进行重命名。
原文地址: http://www.cveoy.top/t/topic/b2Ck 著作权归作者所有。请勿转载和采集!