module Drasil.Build.Artifacts.Directory (createDirIfMissing) where

import Control.Monad (unless)
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
  Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
exists (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
withParents FilePath
path