module Utils.Drasil.Directory (createDirIfMissing) where

import System.Directory (createDirectoryIfMissing, doesPathExist)

-- | Creates a directory if it does not already exist (optionally with all
-- missing parent directories).
--
-- Implementation uses doesPathExist to check if the directory exists rather
-- than createDirectoryIfMissing True, which would create the directory
-- regardless of whether it exists or not, potentially leading to an error that
-- appears in `make debug` logs.
createDirIfMissing :: Bool -> FilePath -> IO ()
createDirIfMissing :: Bool -> FilePath -> IO ()
createDirIfMissing Bool
withParents FilePath
path = do
  Bool
exists <- FilePath -> IO Bool
doesPathExist FilePath
path
  if Bool
exists
    then () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    else Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
withParents FilePath
path