"use strict";

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

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

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

  _fsExtra = function () {
    return data;
  };

  return data;
}

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

  _path = function () {
    return data;
  };

  return data;
}

function _sources() {
  const data = require("../consumer/component/sources");

  _sources = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../constants");

  _constants = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = require("../bit-id");

  _bitId = function () {
    return data;
  };

  return data;
}

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

  _logger = function () {
    return data;
  };

  return data;
}

function _validationError() {
  const data = _interopRequireDefault(require("../error/validation-error"));

  _validationError = function () {
    return data;
  };

  return data;
}

class LinkFile extends _sources().AbstractVinyl {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "override", false);
    (0, _defineProperty2().default)(this, "writeAutoGeneratedMessage", true);
  }

  // needed for logging purposes
  write() {
    var _this = this;

    return (0, _bluebird().coroutine)(function* () {
      const stat = yield _this._getStatIfFileExists();

      if (stat) {
        if (stat.isSymbolicLink()) {
          throw new (_validationError().default)(`fatal: trying to write a link file into a symlink file at "${_this.path}"`);
        }

        if (!_this.override) {
          const fileContent = _fsExtra().default.readFileSync(_this.path).toString();

          if (!fileContent.includes(_constants().AUTO_GENERATED_STAMP)) return _this.path;
        }
      }

      const data = _this.writeAutoGeneratedMessage ? _constants().AUTO_GENERATED_MSG + _this.contents : _this.contents;

      _logger().default.debug(`link-file.write, path ${_this.path}`);

      try {
        yield _fsExtra().default.outputFile(_this.path, data);
      } catch (err) {
        if (err.code === 'EISDIR') {
          _logger().default.debug(`deleting a directory ${_this.path} in order to write a link file with the same name`);

          yield _fsExtra().default.remove(_this.path);
          yield _fsExtra().default.outputFile(_this.path, data);
        } else {
          throw err;
        }
      }

      return _this.path;
    })();
  }

  static load({
    filePath,
    base,
    content,
    override = false,
    writeAutoGeneratedMessage = true,
    srcPath,
    componentId
  }) {
    const linkFile = new LinkFile({
      base: base || _path().default.dirname(filePath),
      path: filePath,
      contents: Buffer.from(content)
    });
    linkFile.override = override;
    linkFile.writeAutoGeneratedMessage = writeAutoGeneratedMessage;
    linkFile.srcPath = srcPath;
    linkFile.componentId = componentId;
    return linkFile;
  }

}

exports.default = LinkFile;