"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = createSymlinkOrCopy;

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _fsExtra() {
  const data = _interopRequireDefault(require("fs-extra"));

  _fsExtra = function () {
    return data;
  };

  return data;
}

function _symlinkOrCopy() {
  const data = _interopRequireDefault(require("symlink-or-copy"));

  _symlinkOrCopy = function () {
    return data;
  };

  return data;
}

function _logger() {
  const data = _interopRequireDefault(require("../../logger/logger"));

  _logger = function () {
    return data;
  };

  return data;
}

function _generalError() {
  const data = _interopRequireDefault(require("../../error/general-error"));

  _generalError = function () {
    return data;
  };

  return data;
}

/**
 * @param srcPath the path where the symlink is pointing to
 * @param destPath the path where to write the symlink
 * @param componentId
 */
function createSymlinkOrCopy(srcPath, destPath, componentId = '') {
  _logger().default.info(`create-symlink-or-copy, deleting ${destPath}`);

  _fsExtra().default.removeSync(destPath); // in case a symlink already generated or when linking a component, when a component has been moved


  _fsExtra().default.ensureDirSync(_path().default.dirname(destPath));

  try {
    _logger().default.debug(`generating a symlink on ${destPath} pointing to ${srcPath}`);

    _symlinkOrCopy().default.sync(srcPath, destPath);
  } catch (err) {
    const errorHeader = componentId ? `failed to link a component ${componentId}` : 'failed to generate a symlink';
    throw new (_generalError().default)(`${errorHeader}.
         Symlink (or maybe copy for Windows) from: ${srcPath}, to: ${destPath} was failed.
         Original error: ${err}`);
  }
}