"use strict";

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

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

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

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

var _assign = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/assign"));

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

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

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

var _logSymbols = _interopRequireDefault(require("log-symbols"));

var _lodash = require("lodash");

var _inquirer = _interopRequireDefault(require("inquirer"));

require("console.table");

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

var _requirements = _interopRequireDefault(require("../../requirements"));

// import lerna from 'lerna/cli'
// console.log('lerna', lerna);
const dependencyInfo = (name, version, versionRequirements) => {
  const validVersion = _semver.default.satisfies(version, versionRequirements);

  return {
    name: name,
    version: version ? version.replace('\n', '').replace('\r', '') : 'Missing dependency',
    compatible: !versionRequirements || validVersion ? _logSymbols.default.success : _logSymbols.default.error
  };
};

const getSemverVersion = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2.default)(function* (command) {
    let semverVersion = 'Not found';

    if (process.platform !== 'win32' && (0, _indexOf.default)(command).call(command, '.exe ') > -1) {
      return semverVersion;
    }

    try {
      const preVersion = yield (0, _run.default)(command, true);

      if (Boolean(preVersion)) {
        semverVersion = _semver.default.coerce(preVersion);
      }
    } catch (err) {//No version found
    }

    return _promise.default.resolve(semverVersion);
  });

  return function getSemverVersion(_x) {
    return _ref.apply(this, arguments);
  };
}();

var _default = (packages, opt) => (0, _plugin.default)('BUILD INFO', ({
  reporter
}) => /*#__PURE__*/(0, _asyncToGenerator2.default)(function* () {
  try {
    const versions = {};
    (0, _map.default)(packages).call(packages, p => versions[p] = _requirements.default[p]);
    const options = (0, _assign.default)({}, opt);
    const nodeVersion = yield (0, _run.default)('node -v');
    const yarnVersion = yield (0, _run.default)('yarn --version');
    const gitVersion = yield (0, _run.default)('git --version');
    const isWindows = process.platform === 'win32';
    const octoCommand = isWindows ? '"../tools/Octo.exe" version' : 'dotnet octo --version';
    const nugetCommand = isWindows ? '"../tools/nuget.exe" help' : 'dotnet nuget --version';
    const octoVersion = yield getSemverVersion(octoCommand);
    const nugetVersion = yield getSemverVersion(nugetCommand);
    const lernaVersion = yield getSemverVersion('lerna -v');
    const dependancies = [dependencyInfo('Node', nodeVersion, versions.node), dependencyInfo('Yarn', yarnVersion, versions.yarn), dependencyInfo('Git', gitVersion, versions.git)];
    if (!!versions.octo) dependancies.push(dependencyInfo('Octo', octoVersion ? octoVersion.version : null, versions.octo));
    if (!!versions.nuget) dependancies.push(dependencyInfo('Nuget', nugetVersion ? nugetVersion.version : null, versions.nuget));
    if (!!versions.lerna) dependancies.push(dependencyInfo('Lerna', nugetVersion ? lernaVersion.version : null, versions.lerna));
    console.table(dependancies);

    if (!options.force && (0, _someInstanceProperty(_lodash))(dependancies, dep => dep.compatable === _logSymbols.default.error)) {
      return new _promise.default((resolve, reject) => _inquirer.default.prompt({
        name: 'confirm',
        type: 'confirm',
        message: 'Do you want to continue?'
      }).then(answer => answer.confirm ? resolve() : reject('Build terminated')));
    }

    return _promise.default.resolve();
  } catch (e) {
    _promise.default.reject(e);
  }
}));

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