import log from "electron-log/main";
import fs from "fs";
import path from "path";
import errorTypeCheck from "../../util/errorTypeCheck";

const createdDirectoryIfNotExist = async (dirPath: string) => {
  try {
    const directoryPath = path.dirname(dirPath);
    if (!fs.existsSync(directoryPath)) {
      log.info(`Directory does not exist. Creating: ${directoryPath}`);
      fs.mkdirSync(directoryPath, { recursive: true });
    }
  } catch (error) {
    log.error("Error creating directory as needed", errorTypeCheck(error));
    throw new Error(
      "Error creating directory: " + errorTypeCheck(error).message,
    );
  }
};

export default createdDirectoryIfNotExist;
