"use strict";var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");_Object$defineProperty(exports, "__esModule", { value: true });exports.default = prettierCode;const prettier = require('prettier');
const fs = require('fs');
const path = require('path');

function prettierCode(dir) {
  const files = [];
  traverseDirSync(dir, files, {
    include: /(wxapi-adapter\.js)|(unity_plugin(?:_\d+\.\d+\.\d+)?\.js)|(webgl\.wasm\.framework\.unityweb\.js)/
  });

  for (const file of files) {
    const content = fs.readFileSync(file, {
      encoding: 'utf-8'
    });
    if (content) {
      const options = {
        semi: true,
        singleQuote: true,
        parser: 'babel'
      };
      const formattedCode = prettier.format(content, options);
      fs.writeFileSync(file, formattedCode, 'utf8');
    }
  }
}

/**
 * 遍历目录文件 同步方法
 * @param dir
 * @param files 收集的文件列表
 */
const traverseDirSync = function (dir, files, options) {
  const st = fs.statSync(dir);
  if (st && st.isFile()) {
    if (options && options.include) {
      if (options.include.test(dir)) {
        files.push(dir);
      }
      return;
    }
    files.push(dir);
    return;
  }
  const list = fs.readdirSync(dir);
  list.forEach(function (file) {
    const isExclude = options && options.exclude && options.exclude.test(file);
    if (!isExclude) {
      file = path.join(dir, file);
      const stat = fs.statSync(file);
      if (stat && stat.isDirectory()) {
        traverseDirSync(file, files, options);
      } else {
        if (options && options.include) {
          if (options.include.test(file)) {
            files.push(file);
          }
          return;
        }
        files.push(file);
      }
    }
  });
};