{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TemplateHaskell #-}

module Language.JavaScript.Inline.JSParse (jsParse) where

import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.FileEmbed
import Language.JavaScript.Inline.Core
import UnliftIO
import UnliftIO.Process

jsParse :: String -> IO (Bool, Bool, [String])
jsParse :: String -> IO (Bool, Bool, [String])
jsParse String
src =
  do
    let node_path :: String
node_path = Config -> String
nodePath Config
defaultConfig
    String
o <- String -> (String -> Handle -> IO String) -> IO String
forall (m :: * -> *) a.
MonadUnliftIO m =>
String -> (String -> Handle -> m a) -> m a
withSystemTempFile String
"main.js" ((String -> Handle -> IO String) -> IO String)
-> (String -> Handle -> IO String) -> IO String
forall a b. (a -> b) -> a -> b
$
      \String
p Handle
h -> do
        Handle -> IO ()
forall (m :: * -> *). MonadIO m => Handle -> m ()
hClose Handle
h
        String -> ByteString -> IO ()
BS.writeFile String
p ByteString
parserSrc
        String -> [String] -> String -> IO String
forall (m :: * -> *).
MonadIO m =>
String -> [String] -> String -> m String
readProcess String
node_path [String
p] String
src
    let String
is_sync_str : String
is_expr_str : [String]
toks = String -> [String]
lines String
o
        !is_sync :: Bool
is_sync = String -> Bool
forall a. Read a => String -> a
read String
is_sync_str
        !is_expr :: Bool
is_expr = String -> Bool
forall a. Read a => String -> a
read String
is_expr_str
    (Bool, Bool, [String]) -> IO (Bool, Bool, [String])
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool
is_sync, Bool
is_expr, [String]
toks)

parserSrc :: ByteString
parserSrc :: ByteString
parserSrc = $(makeRelativeToProject "jsbits/main.js" >>= embedFile)