//*******************************************
// index.json 示例
// [
// {
// "name": "Act_1_ChongZhiZengSong",
// "uuid": "75fcdMzuAJOd6NfRzuA0VMq",
// "code": "75fcd333-b802-4e77-a35f-473b80d1532a"
// },
// {
// "name": "Act_10_JiuJin",
// "uuid": "488986VWURJqbl17dJPRCC9",
// "code": "48898e95-5944-49a9-b975-edd24f4420bd"
// },
// ]
// meta.json 示例
// [
// {
// "name": "Act_1_ChongZhiZengSong.ts.meta",
// "path": "src\\Act_1_ChongZhiZengSong.ts.meta",
// "type": "ts",
// "title": "Act_1_ChongZhiZengSong.ts"
// },
// {
// "name": "Act_2_YouWanZengSong.ts.meta",
// "path": "src\\Act_2_YouWanZengSong.ts.meta",
// "type": "ts",
// "title": "Act_2_YouWanZengSong.ts"
// },
// ]
// */
const fs = require('fs');
const path = require('path');
// 定义工作区路径,这里假设是当前工作目录
const workspacePath = '.';
// 定义存储meta信息的文件路径
const metaFilePath = path.join(workspacePath, 'meta.json');
const indexFilePath = path.join(workspacePath, 'index.json');
// 递归遍历目录函数
function traverseDirectory(dir) {
const metaFiles = [];
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// 如果是目录,递归遍历
metaFiles.push(...traverseDirectory(filePath));
} else if (path.extname(file) === '.meta') {
// 如果是.meta文件,记录名字、路径和类型
let type = null;
const match = file.match(/\.([^.]+)\.meta$/);
console.log('match=>',match);
// 匹配文件类型,如.ts、.js、.cs
if (match) {
type = match[1];
}
let title = null;
const titleMatch = file.match(/^([^.]+)\.([^.]+)\.meta$/);
if (titleMatch) {
title = titleMatch[1];
}
metaFiles.push({
name: file,
path: filePath,
type: type,
title: title + '.' + type
});
}
});
return metaFiles;
}
// 遍历工作区,获取所有.meta文件信息
const metaFiles = traverseDirectory(workspacePath);
// 确保type字段存在,若没有则默认为null
metaFiles.forEach(file => {
if (!file.type) {
file.type = null;
}
});
// 将meta文件信息存储到meta.json文件中
fs.writeFileSync(metaFilePath, JSON.stringify(metaFiles, null, 2));
console.log(`Meta信息已存储到 ${metaFilePath}`);
// 读取index.json文件
const indexData = JSON.parse(fs.readFileSync(indexFilePath, 'utf8'));
// 找到type为ts的meta文件并更新uuid字段
metaFiles.forEach(metaFile => {
if (metaFile.type === 'ts') {
// 去掉 metaFile.title 的后缀
const baseTitle = path.basename(metaFile.title, '.ts');
const indexItem = indexData.find(
item => item.name === baseTitle
);
if (indexItem) {
const metaContent = JSON.parse(fs.readFileSync(metaFile.path, 'utf8'));
metaContent.uuid = indexItem.code;
fs.writeFileSync(metaFile.path, JSON.stringify(metaContent, null, 2));
console.log(`已更新 ${metaFile.path} 的uuid字段为 ${indexItem.code}`);
}
}
});
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END







暂无评论内容