'use strict';

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

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

var _fsExtra;

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

var _constants;

function _load_constants() {
  return _constants = require('../constants');
}

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

exports.default = function () {
  var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(_ref2) {
    var filePath = _ref2.filePath,
        content = _ref2.content,
        _ref2$writeAutoGenera = _ref2.writeAutoGeneratedMessage,
        writeAutoGeneratedMessage = _ref2$writeAutoGenera === undefined ? true : _ref2$writeAutoGenera,
        _ref2$override = _ref2.override,
        override = _ref2$override === undefined ? true : _ref2$override;
    var fileContent, data;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!(!override && (_fsExtra || _load_fsExtra()).default.existsSync(filePath))) {
              _context.next = 4;
              break;
            }

            fileContent = (_fsExtra || _load_fsExtra()).default.readFileSync(filePath).toString();

            if (fileContent.includes((_constants || _load_constants()).AUTO_GENERATED_STAMP)) {
              _context.next = 4;
              break;
            }

            return _context.abrupt('return', Promise.resolve(filePath));

          case 4:
            data = writeAutoGeneratedMessage ? (_constants || _load_constants()).AUTO_GENERATED_MSG + content : content;
            _context.next = 7;
            return (_fsExtra || _load_fsExtra()).default.outputFile(filePath, data);

          case 7:
            return _context.abrupt('return', Promise.resolve(filePath));

          case 8:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  function outputFile(_x) {
    return _ref.apply(this, arguments);
  }

  return outputFile;
}();