{-# LANGUAGE TemplateHaskell, CPP #-}
module Foreign.JavaScript.Include (include) where

import           Data.FileEmbed      (makeRelativeToProject)
import           Language.Haskell.TH
import           System.IO

include :: FilePath -> Q Exp
include :: FilePath -> Q Exp
include FilePath
path = do
    FilePath
relativePath <- FilePath -> Q FilePath
makeRelativeToProject FilePath
path
    Lit -> Exp
LitE (Lit -> Exp) -> (FilePath -> Lit) -> FilePath -> Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Lit
StringL (FilePath -> Exp) -> Q FilePath -> Q Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO FilePath -> Q FilePath
forall a. IO a -> Q a
runIO (FilePath -> IO FilePath
readFileUTF8 FilePath
relativePath)

readFileUTF8 :: FilePath -> IO String
readFileUTF8 :: FilePath -> IO FilePath
readFileUTF8 FilePath
path = do
    Handle
h <- FilePath -> IOMode -> IO Handle
openFile FilePath
path IOMode
ReadMode
    Handle -> TextEncoding -> IO ()
hSetEncoding Handle
h TextEncoding
utf8
    Handle -> IO FilePath
hGetContents Handle
h