'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 _path;

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

var _logger;

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

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(path) {
    var propagateDirs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

    var _pathLib$parse, dir, files;

    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            _context.next = 3;
            return (_fsExtra || _load_fsExtra()).default.unlink(path);

          case 3:
            _context.next = 10;
            break;

          case 5:
            _context.prev = 5;
            _context.t0 = _context['catch'](0);

            if (!(_context.t0.code === 'ENOENT')) {
              _context.next = 9;
              break;
            }

            return _context.abrupt('return', false);

          case 9:
            throw _context.t0;

          case 10:
            if (propagateDirs) {
              _context.next = 12;
              break;
            }

            return _context.abrupt('return', true);

          case 12:
            _pathLib$parse = (_path || _load_path()).default.parse(path), dir = _pathLib$parse.dir;
            _context.next = 15;
            return (_fsExtra || _load_fsExtra()).default.readdir(dir);

          case 15:
            files = _context.sent;

            if (!(files.length !== 0)) {
              _context.next = 18;
              break;
            }

            return _context.abrupt('return', true);

          case 18:
            (_logger || _load_logger()).default.info(`fs-remove-file, deleting empty directory ${dir}`);
            _context.next = 21;
            return (_fsExtra || _load_fsExtra()).default.remove(dir);

          case 21:
            return _context.abrupt('return', true);

          case 22:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this, [[0, 5]]);
  }));

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

  return removeFile;
}();