61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
const { log } = require("console");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const folderPath = "./configs";
|
|
const scope = [
|
|
"StdItems","Monster"
|
|
// "ItemMergeConfig", "MergeConfig", "MergeTotal", "RecyclingSettingConfig", "UpstarConfig", "Monster"
|
|
];
|
|
|
|
// 自定义排序函数
|
|
function customSort(arr) {
|
|
return arr.sort((a, b) => {
|
|
const isANumeric = !isNaN(a);
|
|
const isBNumeric = !isNaN(b);
|
|
if (isANumeric && isBNumeric) {
|
|
return parseFloat(a) - parseFloat(b);
|
|
} else if (isANumeric) {
|
|
return -1;
|
|
} else if (typeof a == "object") {
|
|
return -1;
|
|
} else if (isBNumeric) {
|
|
return 1;
|
|
} else {
|
|
return a.localeCompare(b);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 递归排序函数
|
|
function sortObject(obj) {
|
|
if (Array.isArray(obj)) {
|
|
// 如果是数组,使用自定义排序函数
|
|
return customSort(obj.map((item) => sortObject(item)));
|
|
} else if (typeof obj === "object" && obj !== null) {
|
|
// 如果是对象,递归处理每个属性
|
|
const sortedObj = {};
|
|
Object.keys(obj)
|
|
.sort()
|
|
.forEach((key) => {
|
|
sortedObj[key] = sortObject(obj[key]);
|
|
});
|
|
return sortedObj;
|
|
} else {
|
|
// 如果是基本类型,直接返回
|
|
return obj;
|
|
}
|
|
}
|
|
const start = () => {
|
|
const files = fs.readdirSync(folderPath);
|
|
// 过滤出所有的 JSON 文件
|
|
const jsonFiles = files.filter((file) => path.extname(file) === ".json").filter((file) => scope.map((e) => `${e}.json`).includes(file));
|
|
jsonFiles.forEach((file) => {
|
|
const json = JSON.parse(fs.readFileSync(path.join(folderPath, file), "utf8"));
|
|
const filename = path.basename(file, path.extname(file));
|
|
const sortedJson = sortObject(json);
|
|
const jsonstr = JSON.stringify(sortedJson, null, 2);
|
|
fs.writeFileSync(`${folderPath}/${filename}.json`, jsonstr);
|
|
});
|
|
};
|
|
start();
|