"use strict";

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

require("core-js/modules/es.promise");

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.default = void 0;

var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));

var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));

var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));

var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator"));

var _fs = _interopRequireDefault(require("fs"));

var _path = _interopRequireDefault(require("path"));

var _lodash = require("lodash");

var _plugin = _interopRequireDefault(require("@start/plugin"));

const mainModules = [];

const pathName = _path.default.join(__dirname, '../../../../..');

const packagesPath = _path.default.join(pathName, 'packages');

var _default = (ver, opt) => (0, _plugin.default)('DEPENDANCY GRAPH', ({
  reporter
}) =>
/*#__PURE__*/
(0, _asyncToGenerator2.default)(function* () {
  const log = message => reporter.emit('message', 'DEPENDANCY GRAPH', message);

  try {
    var _context;

    const names = (0, _map.default)(_context = _fs.default.readdirSync(packagesPath)).call(_context, name => {
      mainModules.push(name);
      return name;
    });
    (0, _map.default)(names).call(names, name => followPath(name, log, reporter));
    return _promise.default.resolve();
  } catch (e) {
    _promise.default.reject(e);
  }
}));

exports.default = _default;

const followPath = (moduleName, log, reporter) => {
  log('Checking ' + moduleName);

  if (recursePath(moduleName, 50, [], log, reporter)) {
    log('No cycles found in ' + moduleName);
  }
};

const recursePath = (moduleName, depth, stack, log, reporter) => {
  if (depth === 0) {
    return true;
  }

  if ((0, _indexOf.default)(stack).call(stack, moduleName) > -1) {
    stack.push(moduleName);
    const errMsg = stack.join(' -> ');
    console.error('LOOP : ' + errMsg + ' -----------');
    return false;
  }

  stack.push(moduleName);
  const config = getPackageFile(moduleName);

  if (!_fs.default.existsSync(config)) {
    return true;
  }

  const contents = JSON.parse(_fs.default.readFileSync(config, 'utf8'));

  for (const key in contents.dependencies) {
    if (isMainModule(key)) {
      const copy = (0, _slice.default)(stack).call(stack, 0);

      if (!recursePath(key, depth - 1, copy, log, reporter)) {
        return false;
      }
    }
  }

  return true;
};

const getPackageFile = moduleName => {
  const modulePath = _path.default.join(packagesPath, moduleName);

  return _path.default.join(modulePath, 'package.json');
};

const isMainModule = moduleName => (0, _indexOf.default)(mainModules).call(mainModules, moduleName) > -1;

module.exports = exports.default;