"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var glob = require("glob");

var fs = require("fs");

var get = require("lodash.get");

var parse = require("./parse");

var defaultParserPlugins = require('./defaultParserPlugins');

var isIgnoredPath = function isIgnoredPath(_ref) {
  var path = _ref.path,
      instance = _ref.instance,
      adioRc = _ref.adioRc;
  var dirs = get(instance, "config.ignoreDirs") || [];

  for (var i = 0; i < dirs.length; i++) {
    var dir = dirs[i];

    if (path.includes(dir)) {
      return true;
    }
  }

  dirs = get(adioRc, "ignoreDirs") || [];

  for (var _i = 0; _i < dirs.length; _i++) {
    var _dir = dirs[_i];

    if (path.includes(_dir)) {
      return true;
    }
  }

  return false;
};

module.exports = function (_ref2) {
  var dir = _ref2.dir,
      instance = _ref2.instance,
      adioRc = _ref2.adioRc;
  var fileExtensions = ['js', 'ts', 'tsx'];
  var paths = [];

  for (var i = 0; i < fileExtensions.length; i++) {
    var fileExtension = fileExtensions[i];
    paths.push.apply(paths, (0, _toConsumableArray2["default"])(glob.sync(dir + "/**/*.".concat(fileExtension))));
  }

  var deps = [];
  paths.forEach(function (path) {
    if (isIgnoredPath({
      path: path,
      instance: instance,
      adioRc: adioRc
    })) {
      return true;
    }

    var src = fs.readFileSync(path, "utf8");
    var importsRequires = parse({
      path: path,
      src: src,
      config: {
        parser: _objectSpread({}, get(adioRc, "parser", get(instance, "config.parser", {})), {
          // include commonly needed plugins
          plugins: defaultParserPlugins(get(adioRc, "parser.plugins", get(instance, "config.parser.plugins", [])))
        }),
        traverse: get(adioRc, "traverse", get(instance, "config.traverse"))
      }
    });
    importsRequires.forEach(function (name) {
      // is relative import?
      if (!name || name.startsWith(".")) {
        return true;
      } // already included in deps?


      if (deps.includes(name)) {
        return true;
      }

      deps.push(name);
    });
  });
  return deps;
};
//# sourceMappingURL=extractSrcDeps.js.map