const fs = require('fs');
const path = require('path');

// 服务目录
const SERVICES_DIR = __dirname;

// 递归将目录重命名为小写
const normalizeDirectoryCase = (dirPath) => {
  const items = fs.readdirSync(dirPath);
  
  items.forEach(item => {
    const itemPath = path.join(dirPath, item);
    const stat = fs.statSync(itemPath);
    
    if (stat.isDirectory()) {
      // 跳过特殊目录
      if (item === '__tests__' || item === 'node_modules') {
        return;
      }
      
      // 将目录名转换为小写
      const lowercaseName = item.toLowerCase();
      const lowercasePath = path.join(dirPath, lowercaseName);
      
      if (item !== lowercaseName && !fs.existsSync(lowercasePath)) {
        fs.renameSync(itemPath, lowercasePath);
        console.log(`Renamed directory: ${item} -> ${lowercaseName}`);
        // 递归处理子目录
        normalizeDirectoryCase(lowercasePath);
      } else if (item !== lowercaseName && fs.existsSync(lowercasePath)) {
        console.log(`Skipped directory rename: ${item} -> ${lowercaseName} (lowercase directory already exists)`);
      } else {
        // 目录名已经是小写，递归处理子目录
        normalizeDirectoryCase(itemPath);
      }
    }
  });
};

// 运行目录大小写规范化
const runNormalization = () => {
  console.log('Starting directory case normalization...');
  normalizeDirectoryCase(SERVICES_DIR);
  console.log('\nDirectory case normalization completed!');
};

// 运行规范化
runNormalization();
