"use strict";var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");_Object$defineProperty(exports, "__esModule", { value: true });exports.default = readdir;exports.noDotFiles = noDotFiles;exports.noDotSymbolsFiles = noDotSymbolsFiles;var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _index = require("../../cli-shared-utils/index.js");

/**
 * 递归扫描所有文件
 * @param {String} root 要扫描的根目录
 * @param {Functioin} filter 要过滤的文件， 默认过滤.xxx
 * @param {Array} files 扫描出来的的文件集合
 * @param {String} prefix 用于递归时传递内层的dir
 */
function readdir(root, filter = noDotFiles, files = [], prefix = '') {
  const dir = _path.default.posix.join(root, prefix);
  if (!_fs.default.existsSync(dir)) return files;
  if (_fs.default.statSync(dir).isDirectory()) {
    _fs.default.readdirSync(dir).
    filter(function (name, index) {
      return filter(name, index, dir);
    }).
    forEach(function (name) {
      readdir(root, filter, files, _path.default.posix.join(prefix, name));
    });
  } else {
    files.push(prefix);
  }

  return files;
}

function noDotFiles(x) {
  return x[0] !== '.';
}

function noDotSymbolsFiles(filename, index, dir) {
  try {
    if (filename.includes('symbols')) {
      var filePath = _path.default.resolve(dir, filename);
      var content = _fs.default.readFileSync(filePath, {
        encoding: 'utf-8'
      });
      if (isJsonString(content) && content.includes('invoke_iiii')) {
        (0, _index.info)('release打包过滤掉symbols文件:' + filePath);
        return false;
      }
    }
  } catch (err) {
  }
  return true;
}

function isJsonString(str) {
  try {
    JSON.parse(str);
    return true;
  } catch (e) {
    return false;
  }
}