"use strict";

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

var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map");

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

var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");

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

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

exports.default = void 0;

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 _stringify = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/json/stringify"));

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

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

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

var _util = require("util");

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

var _lodash = require("lodash");

function _getRequireWildcardCache(nodeInterop) { if (typeof _WeakMap !== "function") return null; var cacheBabelInterop = new _WeakMap(); var cacheNodeInterop = new _WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = _Object$defineProperty && _Object$getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? _Object$getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { _Object$defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const readdir = (0, _util.promisify)(_fs.default.readdir);
const writeFile = (0, _util.promisify)(_fs.default.writeFile);
const lstat = (0, _util.promisify)(_fs.default.lstat);
const mkdir = (0, _util.promisify)(_fs.default.mkdir);
const exists = (0, _util.promisify)(_fs.default.exists);

const tryRequire = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2.default)(function* (file) {
    try {
      return require(file);
    } catch (e) {
      var _context;

      if (e && (0, _indexOf.default)(_context = e.toString()).call(_context, 'Cannot find module' > -1)) return {};
      console.error('f', file, e);
      return {};
    }
  });

  return function tryRequire(_x) {
    return _ref.apply(this, arguments);
  };
}();

var _default = (configsFolder = (0, _path.resolve)(process.cwd(), './app/configs')) => (0, _plugin.default)('CREATE CONFIG', ({
  reporter
}) => /*#__PURE__*/(0, _asyncToGenerator2.default)(function* () {
  const loadConfig = /*#__PURE__*/function () {
    var _ref3 = (0, _asyncToGenerator2.default)(function* (file) {
      const filePath = _path.default.join(configsFolder, file);

      const context = process.env.SERVER_ENV || 'local';

      const contextFilePath = _path.default.join(configsFolder, context, file);

      const contextConfig = yield tryRequire(contextFilePath);
      const config = yield tryRequire(filePath);
      return (0, _lodash.merge)(config, contextConfig);
    });

    return function loadConfig(_x2) {
      return _ref3.apply(this, arguments);
    };
  }();

  const config = {};

  try {
    const names = yield readdir(configsFolder);
    yield _promise.default.all((0, _mapInstanceProperty(_lodash))(names, /*#__PURE__*/function () {
      var _ref4 = (0, _asyncToGenerator2.default)(function* (file) {
        if ((0, _indexOf.default)(file).call(file, '.js') === -1) return;
        const configKey = file.replace('.js', '');

        const filePath = _path.default.join(configsFolder, file);

        const isDirectory = (yield lstat(filePath)).isDirectory();

        if (!isDirectory) {
          config[configKey] = yield loadConfig(file);
        }
      });

      return function (_x3) {
        return _ref4.apply(this, arguments);
      };
    }()));
  } catch (e) {
    console.log('e', e);
  }

  const dirname = _path.default.join(process.cwd(), './config');

  if (!(yield exists(dirname))) {
    yield mkdir(dirname, {
      recursive: true
    }).catch(console.error);
  }

  const content = `${(0, _stringify.default)(config, null, 4)}`;
  yield writeFile((0, _path.resolve)(dirname, 'config.json'), content, 'utf8');
}));

exports.default = _default;
module.exports = exports.default;