소스 검색

Add watch mode

master
Dylan Baker 3 년 전
부모
커밋
d93851405c
3개의 변경된 파일26개의 추가작업 그리고 0개의 파일을 삭제
  1. 19
    0
      app/Main.hs
  2. 1
    0
      package.yaml
  3. 6
    0
      src/CLI.hs

+ 19
- 0
app/Main.hs 파일 보기

@@ -1,5 +1,6 @@
1 1
 module Main where
2 2
 
3
+import           Control.Concurrent (threadDelay)
3 4
 import           Control.Monad
4 5
 import           Data.Aeson
5 6
 import qualified Data.Map                      as M
@@ -11,6 +12,7 @@ import           Path.IO                        ( copyDirRecur )
11 12
 import           System.Console.ArgParser
12 13
 import           System.Directory
13 14
 import           System.FilePath
15
+import           System.FSNotify
14 16
 import           Text.Pandoc
15 17
 
16 18
 import           Build                          ( buildRawPost
@@ -90,6 +92,22 @@ build dir drafts = do
90 92
             _ -> putStrLn $ unlines $ map show renderErrors
91 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 111
 new :: String -> IO ()
94 112
 new name = do
95 113
   path            <- makeAbsolute name
@@ -108,6 +126,7 @@ new name = do
108 126
 run :: Config -> IO ()
109 127
 run config = case config of
110 128
   (Build dir drafts) -> build dir drafts
129
+  (Watch dir drafts) -> watch dir drafts
111 130
   (New name        ) -> new name
112 131
 
113 132
 main :: IO ()

+ 1
- 0
package.yaml 파일 보기

@@ -26,6 +26,7 @@ dependencies:
26 26
   - directory
27 27
   - doctemplates
28 28
   - filepath
29
+  - fsnotify
29 30
   - pandoc
30 31
   - path
31 32
   - path-io

+ 6
- 0
src/CLI.hs 파일 보기

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

Loading…
취소
저장