let fs = require('fs');
const { exit } = require('process');

const dirNames = __dirname.split('/');
const projectName = dirNames[dirNames.length - 2];
const path = '../play/assets'
let folderName = '';
fs.readdirSync(path).find(fileName => {
  const st = fs.statSync(`${path}/${fileName}`);
  if (st.isDirectory()) {
    folderName = fileName;
  }
});

if (projectName != folderName) {
  throw (`项目名(${projectName})与bundle文件夹名(${folderName})不相同`);
}
let same = false;
const files = fs.readdirSync(`${path}/${folderName}/scene`);
files.forEach(fileName => {
  fileName.split('.').forEach((str, idx, arr) => {
    if (str == 'fire') {
      const sceneName = arr[idx - 1];
      if (folderName == sceneName) {
        same = true;
      }
    }
  })
});

if (!same) {
  throw (`bundle文件夹名称(${folderName})与scene名称不相同`);
}

function checkFileNameWithSpace(src) {
  const exists = fs.existsSync(src);
  if (!exists) {
    return;
  }
  //读取目录
  const paths = fs.readdirSync(src);
  for (let i = 0; i < paths.length; i++) {
    let path = paths[i];
    if (path.indexOf(' ') != -1) {
      throw (`文件/文件夹名称(${src}/${path})不能有空格`);
    }
    const newpath = `${src}/${path}`;
    const st = fs.statSync(newpath);
    if (st.isDirectory()) {
      arguments.callee(newpath);
    }
  }
}

checkFileNameWithSpace('../play/assets');