import Text.JSON
import Text.JSON.Generic
import Util
+import Data.Bool (bool)
main :: IO ()
main = getArgs >>= parse
+getGlobalProjectDir :: IO FilePath
+getGlobalProjectDir = getHomeDirectory >>= return . (++ "/.xyz")
+
+getGlobalProjectConfig :: IO FilePath
+getGlobalProjectConfig = getGlobalProjectDir >>= return . (++ "/xyz.json")
+
continueIfValidProject :: IO () -> IO ()
-continueIfValidProject nextfn = do
- jsonExists <- doesFileExist "xyz.json"
- if jsonExists
- then nextfn
- else putStrLn "This does not appear to be a valid project directory"
+continueIfValidProject next = doesFileExist "xyz.json" >>= bool (putStrLn "This does not appear to be a valid project directory") next
+
+initGlobalIfNeeded :: IO () -> IO ()
+initGlobalIfNeeded next = getGlobalProjectConfig >>= doesFileExist >>= bool globalInit (return ()) >> next
+
+globalInit :: IO ()
+globalInit = do
+ home <- getHomeDirectory
+ putStrLn "\nWelcome to xyz! Looks like this is your first time here. Let's configure a few things...\n"
+ editor <- editorPrompt
+ writeFile (home ++ "/.xyz/xyz.json") (pack . encode . toJSObject $ [("editor", editor)])
+ putStrLn "\nThanks! Now let's continue with initialising your project...\n"
init :: IO ()
init = do
projectSpec <- initPrompt (map pack themes)
createDirectory "entries"
writeFile "xyz.json" . pack . encode . toJSObject $ projectSpec
- putStrLn "\n\nInitialised empty project"
+ putStrLn "\nInitialised empty project"
build :: IO ()
build = do
]
]
-parse ["init"] = init >> exitSuccess
+parse ["init"] = initGlobalIfNeeded init >> exitSuccess
parse ["build"] = continueIfValidProject build >> exitSuccess
parse ["entry"] = continueIfValidProject entry >> exitSuccess
parse [_] = usage >> exit