"use strict";

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

var _directoryTree = _interopRequireDefault(require("directory-tree"));

var _fs = _interopRequireDefault(require("fs"));

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

/**
 * Delete all files in a directory. Sub-directory won't be deleted.
 * If file extension is provided, it'll only delete files of that type
 * @param {string} dir directory in which all files will be deleted
 * @param {string} ext file extension to delete
 */
function deleteFilesInFolder(dir, ext = null) {
  const dt = (0, _directoryTree.default)(dir);

  if (!dt || !dt.children || !dt.children.length) {
    return;
  }

  for (let i = 0, len = dt.children.length; i < len; i += 1) {
    const child = dt.children[i];

    if (child.type === 'file') {
      if (ext) {
        child.extension === `.${ext}` && _fs.default.unlinkSync(child.path);
      } else {
        _fs.default.unlinkSync(child.path);
      }
    }
  } //.for

}