More prompts
[xyz.git] / app / Main.hs
index 07f31d8..597bb89 100644 (file)
@@ -19,16 +19,30 @@ import System.Process
 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
@@ -40,7 +54,7 @@ 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
@@ -91,7 +105,7 @@ usage =  putStr . ununlines $ [
                        ]
   ]
 
-parse ["init"] = init >> exitSuccess
+parse ["init"] = initGlobalIfNeeded init >> exitSuccess
 parse ["build"] = continueIfValidProject build >> exitSuccess
 parse ["entry"] = continueIfValidProject entry >> exitSuccess
 parse [_] = usage >> exit