"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.listAllFiles = listAllFiles;
exports.getBuilderEnv = getBuilderEnv;
exports.agents = void 0;

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

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

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

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(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var _babel = require("@babel/core");

var babelConfig = {
  presets: [[require.resolve('@babel/preset-typescript'), {}], [require.resolve('@babel/preset-env'), {
    targets: {
      node: 6
    }
  }]],
  plugins: [require.resolve('@babel/plugin-transform-runtime'), require.resolve('@babel/plugin-proposal-export-default-from'), require.resolve('@babel/plugin-proposal-do-expressions'), require.resolve('@babel/plugin-proposal-class-properties')]
};
var agents = {
  babel: function babel(contents, filepath, env) {
    return new Promise(function (resolve, reject) {
      try {
        var _env$babel;

        var opts = (_env$babel = env.babel) !== null && _env$babel !== void 0 ? _env$babel : {};

        _babel.transform(contents, _objectSpread(_objectSpread(_objectSpread({}, babelConfig), opts), {}, {
          filename: filepath
        }), function (err, result) {
          if (err) {
            return reject(err);
          }

          return resolve(result);
        });
      } catch (error) {
        return reject(error);
      }
    });
  }
};
exports.agents = agents;

function listAllFiles(dir) {
  return _fs.default.readdirSync(dir).reduce(function (list, file) {
    var name = _path.default.join(dir, file);

    var isDir = _fs.default.statSync(name).isDirectory();

    return list.concat(isDir ? listAllFiles(name) : [name]);
  }, []);
}

function getBuilderEnv() {
  var envFile = _path.default.resolve(process.cwd(), '.builder');

  return JSON.parse(_fs.default.readFileSync(envFile, 'utf-8'));
}