"use strict";

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

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 _startsWith = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/starts-with"));

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 _lodash = require("lodash");

var _gitBranch = _interopRequireDefault(require("git-branch"));

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

var _getDistTag = _interopRequireDefault(require("../utils/getDistTag"));

require("console.table");

/**
 * Remove all resolutions to cosmic packages before yarn install if you are on the master branch
 */
var _default = () => (0, _plugin.default)('SET COSMIC RESOLUTIONS', ({
  reporter
}) => /*#__PURE__*/(0, _asyncToGenerator2.default)(function* () {
  const currentBranch = yield (0, _gitBranch.default)(); // Only run on master branch

  if (currentBranch !== 'master' && !(0, _startsWith.default)(currentBranch).call(currentBranch, 'release')) {
    //Temporary code to fix issue with Yarn https://github.com/yarnpkg/yarn/issues/6666
    try {
      return new _promise.default((resolve, reject) => {
        _fs.default.readFile('./package.json', 'utf-8', /*#__PURE__*/function () {
          var _ref2 = (0, _asyncToGenerator2.default)(function* (err, data) {
            if (!!err) reject(err);
            const packagejson = JSON.parse(data);

            if (!packagejson.resolutions) {
              packagejson.resolutions = {};
            }

            yield _promise.default.all((0, _mapInstanceProperty(_lodash))(packagejson.dependencies, /*#__PURE__*/function () {
              var _ref3 = (0, _asyncToGenerator2.default)(function* (item, key) {
                if (item === 'latest') {
                  const distTags = yield (0, _getDistTag.default)(key);

                  if (!!distTags.next) {
                    packagejson.dependencies[key] = distTags.next;
                    packagejson.resolutions[key] = distTags.next;
                  }
                }
              });

              return function (_x3, _x4) {
                return _ref3.apply(this, arguments);
              };
            }()));
            console.log(packagejson.resolutions);

            _fs.default.writeFile('./package.json', (0, _stringify.default)(packagejson, 'utf-8', '\t'), err => {
              if (err) reject(err);
              return resolve();
            });
          });

          return function (_x, _x2) {
            return _ref2.apply(this, arguments);
          };
        }());
      });
    } catch (e) {
      return _promise.default.reject(e);
    }

    return;
  }
}));

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