"use strict";

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

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

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

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

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

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 path = require("path");

var cosmiconfig = require("cosmiconfig");

var fs = require("fs");

var explorer = cosmiconfig("adio");

var _require = require("./utils/testPackage"),
    extractDepsFromPackageJson = _require.extractDepsFromPackageJson,
    isIgnoredDep = _require.isIgnoredDep;

var extractSrcDeps = require("./utils/extractSrcDeps");

var Adio =
/*#__PURE__*/
function () {
  function Adio(config) {
    (0, _classCallCheck2["default"])(this, Adio);
    this.config = _objectSpread({
      ignoreDirs: ["node_modules"],
      cwd: process.cwd()
    }, config);
  }

  (0, _createClass2["default"])(Adio, [{
    key: "test",
    value: function test() {
      var _this = this;

      var ignoreDirs = this.config.ignoreDirs;

      var normalizedPackagesList = this.__normalizePackagesList();

      var packages = [];
      normalizedPackagesList.forEach(function (dir) {
        glob.sync(dir, {
          cwd: _this.config.cwd,
          ignore: ignoreDirs
        }).forEach(function (packageJsonDir) {
          packages.push(path.join(_this.config.cwd, packageJsonDir));
        });
      });

      if (packages.length === 0) {
        throw new Error("No packages detected.\nDid you specify the correct path via the --package or --packages params?\nFor example: adio --package=src/my-package\nHint: you can also specify these params via the .adiorc.js config or package.json.");
      }

      var checks = [];

      for (var i = 0; i < packages.length; i++) {
        checks.push(this.testPackage(packages[i]));
      }

      return checks;
    }
  }, {
    key: "testPackage",
    value: function testPackage(dir) {
      var _this2 = this;

      var packageJson;

      try {
        packageJson = fs.readFileSync(path.join(dir, "package.json"), "utf8");
      } catch (e) {
        throw Error("Could not open package.json located at ".concat(dir, "."));
      }

      try {
        packageJson = JSON.parse(packageJson);
      } catch (e) {
        throw Error("Could not parse package.json located at ".concat(dir, "."));
      }

      var adioRc = explorer.searchSync(dir);

      if (adioRc) {
        adioRc = adioRc.config;
      }

      var deps = {
        src: extractSrcDeps({
          dir: dir,
          adioRc: adioRc,
          packageJson: packageJson,
          instance: this
        }),
        packageJson: extractDepsFromPackageJson(packageJson)
      };
      var output = {
        packageJson: packageJson,
        dir: dir,
        errors: {
          count: 0,
          deps: {
            src: deps.src.filter(function (dep) {
              if (dep === packageJson.name) {
                return false;
              }

              if (deps.packageJson.dependencies.includes(dep) || deps.packageJson.devDependencies.includes(dep) || deps.packageJson.peerDependencies.includes(dep)) {
                return false;
              }

              return !isIgnoredDep({
                type: "src",
                instance: _this2,
                adioRc: adioRc,
                dep: dep
              });
            }),
            dependencies: deps.packageJson.dependencies.filter(function (dep) {
              if (deps.src.includes(dep)) {
                return false;
              }

              return !isIgnoredDep({
                type: "dependencies",
                instance: _this2,
                adioRc: adioRc,
                dep: dep
              });
            }),
            devDependencies: deps.packageJson.devDependencies.filter(function (dep) {
              if (deps.src.includes(dep)) {
                return false;
              }

              return !isIgnoredDep({
                type: "devDependencies",
                instance: _this2,
                adioRc: adioRc,
                dep: dep
              });
            }),
            peerDependencies: deps.packageJson.peerDependencies.filter(function (dep) {
              if (deps.src.includes(dep)) {
                return false;
              }

              return !isIgnoredDep({
                type: "peerDependencies",
                instance: _this2,
                adioRc: adioRc,
                dep: dep
              });
            })
          }
        }
      };
      output.errors.count = output.errors.deps.src.length + output.errors.deps.dependencies.length + output.errors.deps.devDependencies.length + output.errors.deps.peerDependencies.length;
      return output;
    }
  }, {
    key: "__normalizePackagesList",
    value: function __normalizePackagesList() {
      var normalizedPackagesList = [];
      var _this$config = this.config,
          singlePackage = _this$config["package"],
          multiplePackages = _this$config.packages,
          ignoreDirs = _this$config.ignoreDirs;

      if (typeof singlePackage === "string") {
        normalizedPackagesList.push(singlePackage);
      } else if (Array.isArray(singlePackage)) {
        normalizedPackagesList.push.apply(normalizedPackagesList, (0, _toConsumableArray2["default"])(singlePackage));
      }

      if (typeof multiplePackages === "string") {
        !normalizedPackagesList.includes(multiplePackages) && normalizedPackagesList.push(multiplePackages);
      } else if (Array.isArray(multiplePackages)) {
        multiplePackages.forEach(function (item) {
          !normalizedPackagesList.includes(item) && normalizedPackagesList.push(item);
        });
      }

      return normalizedPackagesList;
    }
  }]);
  return Adio;
}();

module.exports = Adio;
//# sourceMappingURL=Adio.js.map