"use strict";

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

require("core-js/modules/es.promise");

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

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

exports.default = void 0;

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

var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));

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

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

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

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

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

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

var _getPackageVersions = _interopRequireDefault(require("../utils/getPackageVersions"));

var _getCurrentVersion = _interopRequireDefault(require("../utils/getCurrentVersion"));

var _getPackageNugetName = _interopRequireDefault(require("../utils/getPackageNugetName"));

var _default = (repo, nugetPackageName) => (0, _plugin.default)('UPDATE VERSION NUMBER', ({
  reporter
}) =>
/*#__PURE__*/
(0, _asyncToGenerator2.default)(function* () {
  try {
    const currentBranch = yield (0, _gitBranch.default)();
    const currentVersion = (0, _getCurrentVersion.default)();
    const appName = nugetPackageName || (0, _getPackageNugetName.default)();
    const newVersion = yield (0, _getPackageVersions.default)(appName, currentVersion, currentBranch, repo); // console.log('develop', await getPackageVersions(appName, currentVersion, 'develop'))
    // console.log('release/1.1', await getPackageVersions(appName, currentVersion, 'release/1.1'))
    // console.log('feature/newStuff', await getPackageVersions(appName, currentVersion, 'feature/newStuff'))
    // console.log('master', await getPackageVersions(appName, currentVersion, 'master'))

    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) {
      yield (0, _run.default)('git fetch --tags', false, true, {
        stdio: 'inherit'
      });
      const tags = (yield (0, _run.default)('git tag -l')).split('\n');
      const hasBeenTagged = (0, _indexOf.default)(tags).call(tags, newVersion) > -1;

      if (!hasBeenTagged) {
        yield (0, _run.default)(`git tag ${newVersion}`);
        yield (0, _run.default)('git push --tags', false, true, {
          stdio: 'inherit'
        });
        reporter.emit('message', 'Version was tagged');
      } else {
        reporter.emit('message', 'Version has already been tagged');
      }
    } 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;