"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.getLatestAdapters = exports.getLatestAPIs = void 0;
var _path = _interopRequireDefault(require("path"));
var fs = _interopRequireWildcard(require("fs-extra"));
var _axios = _interopRequireDefault(require("axios"));
var _preferDefault = require("../bootstrap/prefer-default");
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 ROOT = _path.default.join(__dirname, `..`, `..`);
const UNPKG_ROOT = `https://unpkg.com/gatsby/`;
const FILE_NAMES = {
  APIS: `apis.json`,
  ADAPTERS: `adapters.js`
};
const OUTPUT_FILES = {
  APIS: _path.default.join(ROOT, `latest-apis.json`),
  ADAPTERS: _path.default.join(ROOT, `latest-adapters.js`)
};
const _getFile = async ({
  fileName,
  outputFileName,
  defaultReturn
}) => {
  try {
    const {
      data
    } = await _axios.default.get(`${UNPKG_ROOT}${fileName}`, {
      timeout: 5000
    });
    await fs.writeFile(outputFileName, JSON.stringify(data, null, 2), `utf8`);
    return data;
  } catch (e) {
    if (await fs.pathExists(outputFileName)) {
      return fs.readJSON(outputFileName);
    }
    if (fileName.endsWith(`.json`)) {
      return fs.readJSON(_path.default.join(ROOT, fileName)).catch(() => defaultReturn);
    } else {
      try {
        const importedFile = await Promise.resolve(`${_path.default.join(ROOT, fileName)}`).then(s => _interopRequireWildcard(require(s)));
        const adapters = (0, _preferDefault.preferDefault)(importedFile);
        return adapters;
      } catch (e) {
        // no-op
        return defaultReturn;
      }
    }
  }
};
const getLatestAPIs = async () => _getFile({
  fileName: FILE_NAMES.APIS,
  outputFileName: OUTPUT_FILES.APIS,
  defaultReturn: {
    browser: {},
    node: {},
    ssr: {}
  }
});
exports.getLatestAPIs = getLatestAPIs;
const getLatestAdapters = async () => _getFile({
  fileName: FILE_NAMES.ADAPTERS,
  outputFileName: OUTPUT_FILES.ADAPTERS,
  defaultReturn: []
});
exports.getLatestAdapters = getLatestAdapters;
//# sourceMappingURL=get-latest-gatsby-files.js.map