Browse Source

Add watch mode

master
Dylan Baker 3 years ago
parent
commit
d93851405c
3 changed files with 26 additions and 0 deletions
  1. 19
    0
      app/Main.hs
  2. 1
    0
      package.yaml
  3. 6
    0
      src/CLI.hs

+ 19
- 0
app/Main.hs View File

1
 module Main where
1
 module Main where
2
 
2
 
3
+import           Control.Concurrent (threadDelay)
3
 import           Control.Monad
4
 import           Control.Monad
4
 import           Data.Aeson
5
 import           Data.Aeson
5
 import qualified Data.Map                      as M
6
 import qualified Data.Map                      as M
11
 import           System.Console.ArgParser
12
 import           System.Console.ArgParser
12
 import           System.Directory
13
 import           System.Directory
13
 import           System.FilePath
14
 import           System.FilePath
15
+import           System.FSNotify
14
 import           Text.Pandoc
16
 import           Text.Pandoc
15
 
17
 
16
 import           Build                          ( buildRawPost
18
 import           Build                          ( buildRawPost
90
             _ -> putStrLn $ unlines $ map show renderErrors
92
             _ -> putStrLn $ unlines $ map show renderErrors
91
     _ -> putStrLn $ unlines $ map show parseErrors
93
     _ -> putStrLn $ unlines $ map show parseErrors
92
 
94
 
95
+handleEvent :: FilePath -> Bool -> Event -> IO ()
96
+handleEvent dir drafts _event = do
97
+  putStrLn "Rebuilding"
98
+  build dir drafts
99
+
100
+watch :: FilePath -> Bool -> IO ()
101
+watch dir drafts = do
102
+  withManager $ \mgr -> do
103
+    putStrLn "Watching..."
104
+    watchTree
105
+      mgr
106
+      dir
107
+      (const True)
108
+      (handleEvent dir drafts)
109
+    forever $ threadDelay 1000000
110
+
93
 new :: String -> IO ()
111
 new :: String -> IO ()
94
 new name = do
112
 new name = do
95
   path            <- makeAbsolute name
113
   path            <- makeAbsolute name
108
 run :: Config -> IO ()
126
 run :: Config -> IO ()
109
 run config = case config of
127
 run config = case config of
110
   (Build dir drafts) -> build dir drafts
128
   (Build dir drafts) -> build dir drafts
129
+  (Watch dir drafts) -> watch dir drafts
111
   (New name        ) -> new name
130
   (New name        ) -> new name
112
 
131
 
113
 main :: IO ()
132
 main :: IO ()

+ 1
- 0
package.yaml View File

26
   - directory
26
   - directory
27
   - doctemplates
27
   - doctemplates
28
   - filepath
28
   - filepath
29
+  - fsnotify
29
   - pandoc
30
   - pandoc
30
   - path
31
   - path
31
   - path-io
32
   - path-io

+ 6
- 0
src/CLI.hs View File

8
 
8
 
9
 data Config =
9
 data Config =
10
   Build FilePath Bool
10
   Build FilePath Bool
11
+  | Watch FilePath Bool
11
   | New String
12
   | New String
12
   deriving (Show)
13
   deriving (Show)
13
 
14
 
18
       (Build `parsedBy` optPos "." "directory" `andBy` boolFlag "drafts")
19
       (Build `parsedBy` optPos "." "directory" `andBy` boolFlag "drafts")
19
       "build"
20
       "build"
20
     )
21
     )
22
+  , ( "watch"
23
+    , mkDefaultApp
24
+      (Watch `parsedBy` optPos "." "directory" `andBy` boolFlag "drafts")
25
+      "watch"
26
+    )
21
   , ("new", mkDefaultApp (New `parsedBy` reqPos "name") "new")
27
   , ("new", mkDefaultApp (New `parsedBy` reqPos "name") "new")
22
   ]
28
   ]
23
 
29
 

Loading…
Cancel
Save