import fs from 'fs';

export const rmDir = (dirPath, removeSelf = true) => {
  const files = fs.readdirSync(dirPath);

  if (files.length > 0) {
    files.forEach((file) => {
      const filePath = `${dirPath}/${file}`;
      if (fs.statSync(filePath).isFile()) {
        fs.unlinkSync(filePath);
      } else {
        rmDir(filePath);
      }
    });
  }
  if (removeSelf) {
    fs.rmdirSync(dirPath);
  }
};

export default {
  rmDir,
};

import fs from 'fs';

export const rmDir = (dirPath, removeSelf = true) => {
  let files = [];
  try {
    files = fs.readdirSync(dirPath);
  } catch (err) {
    return;
  }

  if (files.length > 0) {
    files.forEach((file) => {
      const filePath = `${dirPath}/${file}`;
      if (fs.statSync(filePath).isFile()) {
        fs.unlinkSync(filePath);
      } else {
        rmDir(filePath);
      }
    });
  }
  if (removeSelf) {
    fs.rmdirSync(dirPath);
  }
};

export default {
  rmDir,
};

fix removing directory — catch error if the directory is not exists and return