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

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

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

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