
// const fs = require('fs');
// const path = require('path');
import * as path from 'path'
import * as fs from 'fs';
import { log } from 'console';

// 选定assets文件夹路径
const assetsPath = 'assets';

// 选定ts文件夹路径
const tsPath = './libs';

// 生成ts文件路径
const tsAssetsPath = path.join(tsPath, assetsPath);

// 读取assets文件夹下的所有文件
fs.readdir(assetsPath, (err, files) => {
  if (err) {
    console.error(err);
    return;
  }

  // 遍历每个文件
  files.forEach((file) => {
    const filePath = path.join(assetsPath, file);
    const stat = fs.statSync(filePath);

    if (stat.isDirectory()) {
      console.log(`dir: ${filePath}`);
    } else if (stat.isFile()) {
      // console.log(`file: ${filePath}`);
      // 生成ts文件名称和内容
      const fileName = path.basename(file, path.extname(file)).replace(/-([a-z])/g, (_, letter) => letter.toUpperCase())
      const tsFileName = fileName + '.ts';
      const tsFilePath = path.join(tsAssetsPath, tsFileName);
      console.log()
      console.log('tsName:' + tsFileName)
      // console.log('tsFilePath:' + tsFilePath)
      let tsFileContent = `import ${fileName} from '../../assets/${path.basename(file).toString()}' \n export default ${fileName}`;


      // try {
      //   // 读取assets文件的内容
      //   const fileContent = fs.readFileSync(filePath, 'utf8');
      //   if (fileContent.includes('<svg')) {
      //     // svg文件内容
      //     const match = fileContent.match(/<svg[^>]*>(.*?)<\/svg>/s);
      //     if (match) {
      //       tsFileContent += `import icon${path.basename(file, path.extname(file))} from './assets/${file}';\n`;
      //       tsFileContent += `export default ${match[1].trim()};\n\n`;
      //     }
      //   } else if (fileContent.includes('.png')) {
      //     // png文件内容
      //     const match = fileContent.match(/data:image\/png;,\s*base64\((.*?)\)/);
      //     if (match) {
      //       tsFileContent += `import icon${path.basename(file, path.extname(file))} from './assets/${file}';\n`;
      //       tsFileContent += `export default ${match[1].trim()};\n\n`;
      //     }
      //   }
      // } catch (error) {
      //   console.error(error);
      //   return;
      // }

      // 写入ts文件
      fs.writeFileSync(tsFilePath, tsFileContent);

      console.log(`ts file ${tsFileName} created!`);
    }
  });
});
