"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 _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 _logger() {
  const data = _interopRequireDefault(require("../logger/logger"));

  _logger = function () {
    return data;
  };

  return data;
}

var _default =
/*#__PURE__*/
function () {
  var _removeFile = (0, _bluebird().coroutine)(function* (path, propagateDirs = false) {
    try {
      yield _fsExtra().default.unlink(path);
    } catch (err) {
      if (err.code === 'ENOENT') {
        // the file doesn't exist, that's fine, no need to do anything
        return false;
      }

      throw err;
    }

    if (!propagateDirs) return true;

    const {
      dir
    } = _path().default.parse(path);

    const files = yield _fsExtra().default.readdir(dir);
    if (files.length !== 0) return true;

    _logger().default.info(`fs-remove-file, deleting empty directory ${dir}`);

    yield _fsExtra().default.remove(dir);
    return true;
  });

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

  return removeFile;
}();

exports.default = _default;