"use strict";

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

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

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

exports.default = void 0;

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

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

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

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

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

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

var _express = _interopRequireDefault(require("express"));

var _lodash = require("lodash");

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

var _default = (ver, opt) => (0, _plugin.default)('DEPENDANCY GRAPH', ({
  reporter
}) => /*#__PURE__*/(0, _asyncToGenerator2.default)(function* () {
  try {
    const app = (0, _express.default)();
    app.get('/', (req, res) => {
      res.sendFile(_path.default.join(__dirname, '../../public/dep.html'));
    });
    app.get('/graph', (req, res) => {
      res.send(GetGraph());
    });
    app.listen(9500, () => reporter.emit('message', 'DEPENDANCY GRAPH', 'Running dep graph on: localhost:9500'));
    return _promise.default.resolve();
  } catch (e) {
    _promise.default.reject(e);
  }
}));

exports.default = _default;

const GetGraph = () => {
  var _context;

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

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

  const paths = (0, _map.default)(_context = _fs.default.readdirSync(packagesPath)).call(_context, name => _path.default.join(packagesPath, name));
  const mainModuleNames = [];
  const linkedModuleNames = [];
  const modules = (0, _map.default)(paths).call(paths, pathName => {
    const packageFile = _path.default.join(pathName, 'package.json');

    if (_fs.default.existsSync(packageFile)) {
      const contents = JSON.parse(_fs.default.readFileSync(packageFile, 'utf8'));
      mainModuleNames.push(contents.name);
      return {
        name: contents.name,
        dependencies: contents.dependencies
      };
    }
  });
  const graph = {
    nodes: [],
    links: []
  };
  (0, _map.default)(modules).call(modules, module => {
    if (module === undefined) {
      return;
    }

    graph.nodes.push({
      id: module.name,
      group: 1
    });

    for (const key in module.dependencies) {
      const fullKey = String(key) + module.dependencies[key];

      if ((0, _indexOf.default)(linkedModuleNames).call(linkedModuleNames, fullKey) == -1 && (0, _indexOf.default)(mainModuleNames).call(mainModuleNames, key) == -1) {
        linkedModuleNames.push(fullKey);
        graph.nodes.push({
          id: fullKey,
          group: 2
        });
      }

      if ((0, _indexOf.default)(mainModuleNames).call(mainModuleNames, key) > -1) {
        graph.links.push({
          source: module.name,
          target: key,
          value: 16
        });
      } else {
        graph.links.push({
          source: module.name,
          target: fullKey,
          value: 2
        });
      }
    }
  });
  return graph;
};

module.exports = exports.default;