module TypeScript.Bindings
( TypeScript(..)
, typeScriptCompile
, typeScriptVersion
, typeScriptPrint
) where
import Control.Applicative ((<$>))
import Data.Maybe (fromMaybe)
import System.Exit (ExitCode (..))
import System.Process (rawSystem, readProcess)
data TypeScript = TypeScript
{ customCompiler :: Maybe FilePath
} deriving (Show)
typeScriptCompile :: [FilePath]
-> Maybe FilePath
-> TypeScript
-> IO ExitCode
typeScriptCompile [] _ _ = return $ ExitFailure 1
typeScriptCompile files output typeScript =
rawSystem (getCompiler typeScript) args
where args = outputPath output ++ files
typeScriptVersion :: TypeScript -> IO String
typeScriptVersion t = head . lines <$> typeScriptRead t ["-v"]
typeScriptPrint :: FilePath -> TypeScript -> IO String
typeScriptPrint file t = typeScriptRead t ["-v", file]
outputPath :: Maybe FilePath -> [FilePath]
outputPath (Just path) = ["-outDir", path]
outputPath Nothing = []
getCompiler :: TypeScript -> FilePath
getCompiler = fromMaybe "tsc" . customCompiler
typeScriptRead :: TypeScript -> [String] -> IO String
typeScriptRead typeScript args = readProcess (getCompiler typeScript) args []