"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 _stringifyPackage() {
  const data = _interopRequireDefault(require("stringify-package"));

  _stringifyPackage = function () {
    return data;
  };

  return data;
}

function _abstractVinyl() {
  const data = _interopRequireDefault(require("./sources/abstract-vinyl"));

  _abstractVinyl = function () {
    return data;
  };

  return data;
}

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

  _validationError = function () {
    return data;
  };

  return data;
}

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

  _logger = function () {
    return data;
  };

  return data;
}

/**
 * When writing the `package.json`, it uses the package `stringifyPackage` from the NPM guys, which
 * takes as arguments the indentation and the type of the newline. The logic used here to write the
 * package.json is exactly the same used by NPM. The indentation and newline are detected when the
 * file is loaded. (@see package-json-file.js)
 */
class PackageJsonVinyl extends _abstractVinyl().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "override", true);
  }

  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 package.json file into a symlink file at "${_this.path}"`);
        }

        if (!_this.override) {
          _logger().default.debug(`package-json-vinyl.write, ignore existing file ${_this.path}`);

          return _this.path;
        }
      }

      _logger().default.debug(`package-json-vinyl.write, path ${_this.path}`);

      yield _fsExtra().default.outputFile(_this.path, _this.contents);
      return _this.path;
    })();
  }

  static load({
    base,
    path,
    content,
    indent,
    newline,
    override = true
  }) {
    const jsonStr = (0, _stringifyPackage().default)(content, indent, newline);
    const jsonFile = new PackageJsonVinyl({
      base,
      path,
      contents: Buffer.from(jsonStr)
    });
    jsonFile.override = override;
    return jsonFile;
  }

}

exports.default = PackageJsonVinyl;