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

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

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

var _chalk = _interopRequireDefault(require("chalk"));

var _semver = _interopRequireDefault(require("semver"));

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

var _run = _interopRequireDefault(require("../utils/run"));

var _default = newVersion => (0, _plugin.default)('UPDATE VERSION NUMBER', ({
  reporter
}) => /*#__PURE__*/(0, _asyncToGenerator2.default)(function* () {
  try {
    if (!newVersion) {
      reporter.emit('message', 'New version number missing');
    } else {
      const prerelease = _semver.default.prerelease(newVersion);

      const yarnCommand = `yarn version --new-version ${newVersion} --no-git-tag-version`;
      yield (0, _run.default)(yarnCommand); // Do not tag a prerelease version

      if (!prerelease || !prerelease.length > 0) {
        try {
          yield (0, _run.default)(`git tag ${newVersion}`);
          yield (0, _run.default)('git push --tags', false, true, {
            stdio: 'pipe'
          });
          reporter.emit('message', 'Version was tagged');
        } catch (e) {
          var _context;

          const errorString = (0, _trim.default)(_context = e.toString()).call(_context);
          reporter.emit('message', 'Error when tagging', errorString);
          reporter.emit('message', 'The tag may already exist');
        }
      } else {
        reporter.emit('message', 'Prerelease no need to tag');
      }

      reporter.emit('message', 'Update version number', `${_chalk.default.cyan('Version:')} ${_chalk.default.bold(newVersion)}`);
    }

    return _promise.default.resolve(newVersion);
  } catch (e) {
    reporter.emit('error', 'Update version number', e);
  }
}));

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