'use strict';

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

var _path;

function _load_path() {
  return _path = _interopRequireDefault(require('path'));
}

var _fsExtra;

function _load_fsExtra() {
  return _fsExtra = _interopRequireDefault(require('fs-extra'));
}

var _symlinkOrCopy;

function _load_symlinkOrCopy() {
  return _symlinkOrCopy = _interopRequireDefault(require('symlink-or-copy'));
}

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../../logger/logger'));
}

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../../error/general-error'));
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * @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) {
  var componentId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';

  (_fsExtra || _load_fsExtra()).default.removeSync(destPath); // in case a component has been moved
  (_fsExtra || _load_fsExtra()).default.ensureDirSync((_path || _load_path()).default.dirname(destPath));
  try {
    (_logger || _load_logger()).default.debug(`generating a symlink on ${destPath} pointing to ${srcPath}`);
    (_symlinkOrCopy || _load_symlinkOrCopy()).default.sync(srcPath, destPath);
  } catch (err) {
    throw new (_generalError || _load_generalError()).default(`failed to link a component ${componentId}.
         Symlink (or maybe copy for Windows) from: ${srcPath}, to: ${destPath} was failed.
         Original error: ${err}`);
  }
}