Extra2D/scripts/move_dirs_to_include.js

148 lines
4.4 KiB
JavaScript

/**
* @file move_dirs_to_include.js
* @brief 将 extra2d 子目录移动到 include 根目录
* @description 此脚本将 Extra2D/include/extra2d/ 下的所有子目录(如 action、animation 等)
* 移动到 Extra2D/include/ 根目录下
*/
const fs = require('fs');
const path = require('path');
// 配置路径
const SOURCE_DIR = path.join(__dirname, '..', 'Extra2D', 'include', 'extra2d');
const TARGET_DIR = path.join(__dirname, '..', 'Extra2D', 'include');
/**
* @brief 获取目录下的所有子目录
* @param {string} dir - 要扫描的目录
* @returns {string[]} 子目录路径数组
*/
function getSubdirectories(dir) {
const items = fs.readdirSync(dir);
const dirs = [];
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
dirs.push(fullPath);
}
}
return dirs;
}
/**
* @brief 移动目录到目标位置
* @param {string} sourcePath - 源目录路径
* @param {string} targetPath - 目标目录路径
* @returns {boolean} 是否成功
*/
function moveDirectory(sourcePath, targetPath) {
const dirName = path.basename(sourcePath);
// 检查目标目录是否已存在
if (fs.existsSync(targetPath)) {
console.warn(`⚠️ 目标目录已存在,跳过: ${dirName}`);
return false;
}
try {
fs.renameSync(sourcePath, targetPath);
console.log(`✅ 已移动: extra2d/${dirName} -> ${dirName}/`);
return true;
} catch (err) {
console.error(`❌ 移动失败: ${sourcePath}`, err.message);
return false;
}
}
/**
* @brief 主函数
*/
function main() {
console.log('========================================');
console.log('🚀 开始移动 extra2d 子目录到 include 根目录');
console.log('========================================');
console.log(`📁 源目录: ${SOURCE_DIR}`);
console.log(`📁 目标目录: ${TARGET_DIR}`);
console.log('');
// 检查源目录是否存在
if (!fs.existsSync(SOURCE_DIR)) {
console.error(`❌ 源目录不存在: ${SOURCE_DIR}`);
process.exit(1);
}
// 获取所有子目录
const subdirs = getSubdirectories(SOURCE_DIR);
console.log(`📊 发现 ${subdirs.length} 个子目录`);
console.log('');
if (subdirs.length === 0) {
console.log('⚠️ 没有找到子目录');
return;
}
// 移动目录
let successCount = 0;
let skipCount = 0;
for (const dirPath of subdirs) {
const dirName = path.basename(dirPath);
const targetPath = path.join(TARGET_DIR, dirName);
if (moveDirectory(dirPath, targetPath)) {
successCount++;
} else {
skipCount++;
}
}
// 移动 extra2d.h 文件(如果存在)
const extra2dHeader = path.join(SOURCE_DIR, 'extra2d.h');
if (fs.existsSync(extra2dHeader)) {
const targetHeader = path.join(TARGET_DIR, 'extra2d.h');
if (!fs.existsSync(targetHeader)) {
try {
fs.renameSync(extra2dHeader, targetHeader);
console.log(`✅ 已移动: extra2d/extra2d.h -> extra2d.h`);
successCount++;
} catch (err) {
console.error(`❌ 移动失败: extra2d.h`, err.message);
skipCount++;
}
} else {
console.warn(`⚠️ extra2d.h 已存在,跳过`);
skipCount++;
}
}
// 尝试删除空的 extra2d 目录
try {
const remainingItems = fs.readdirSync(SOURCE_DIR);
if (remainingItems.length === 0) {
fs.rmdirSync(SOURCE_DIR);
console.log('');
console.log('🗑️ 已删除空目录: extra2d/');
} else {
console.log('');
console.log(`⚠️ extra2d 目录中仍有 ${remainingItems.length} 个项目,保留目录`);
remainingItems.forEach(item => console.log(` - ${item}`));
}
} catch (err) {
// 忽略错误
}
console.log('');
console.log('========================================');
console.log('✨ 处理完成!');
console.log(` 成功移动: ${successCount} 个项目`);
console.log(` 跳过: ${skipCount} 个项目`);
console.log('========================================');
}
// 执行主函数
main();