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

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

var _path = require("path");

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

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

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

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

var _getApiKey = _interopRequireDefault(require("../utils/getApiKey"));

var _default = ({
  nugetName,
  apiKey
}) => (0, _plugin.default)('PUBLISH TO NUGET', ({
  reporter
}) => /*#__PURE__*/(0, _asyncToGenerator2.default)(function* () {
  try {
    const version = (0, _getCurrentVersion.default)();
    const name = nugetName || (0, _getPackageNugetName.default)();
    const basePath = (0, _path.resolve)((0, _path.join)(process.cwd(), './_dist'));
    const outFolder = (0, _path.resolve)((0, _path.join)(process.cwd(), './_build'));
    const runFrom = (0, _path.resolve)(__dirname, '../../../tools');
    const isWindows = process.platform === 'win32';
    const octoParams = `pack --id="${name}" --basePath="${basePath}" --outFolder="${outFolder}" --version="${version}" --overwrite`;
    const c = isWindows ? `${runFrom}/Octo.exe ${octoParams}` : `dotnet octo ${octoParams}`;
    const createPackage = yield (0, _run.default)(c, false, !isWindows);
    const server = 'https://artifactory.meniga.net/artifactory/api/nuget/internal-nuget-artifacts/' + name;
    const apiKeyData = 'internal-nuget-artifacts:' + (0, _getApiKey.default)(apiKey);
    const command = isWindows ? `${runFrom}/nuget.exe push "${outFolder}/${name}.${version}.nupkg" -Source "${server}" -ApiKey "${apiKeyData}"` : `dotnet nuget push "${outFolder}/${name}.${version}.nupkg" --source "${server}" --api-key "${apiKeyData}"`;
    yield (0, _run.default)(command, false, !isWindows);
    reporter.emit('message', `Publish ${name} version ${version} to nuget successful`);
  } catch (e) {
    console.log('ERROR', e);
    return _promise.default.reject(e);
  }

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

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