summaryrefslogtreecommitdiff
path: root/src/Hakyll/Preview/Poll.hs
blob: 36b057e92299879ecd4d4bf26bd4bfc23f0c2afe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Hakyll.Preview.Poll
    ( watchUpdates
    ) where

--------------------------------------------------------------------------------
import           Filesystem.Path.CurrentOS (decodeString, encodeString)
import           System.FSNotify           (startManagerConf, watchTree,
                                            Event(..), WatchConfig(..))

--------------------------------------------------------------------------------
import           Hakyll.Core.Configuration



--------------------------------------------------------------------------------
-- | A thread that watches for updates in a 'providerDirectory' and recompiles
-- a site as soon as any changes occur
watchUpdates :: Configuration -> IO () -> IO ()
watchUpdates conf update = do
    _ <- update
    manager <- startManagerConf (Debounce 0.1)
    watchTree manager path (not . isRemove) update'
  where
    path = decodeString $ providerDirectory conf
    update' evt = do
        ignore <- shouldIgnoreFile conf $ eventPath evt
        if ignore then return () else update


eventPath :: Event -> FilePath
eventPath evt = encodeString $ evtPath evt
  where
    evtPath (Added p _) = p
    evtPath (Modified p _) = p
    evtPath (Removed p _) = p


isRemove :: Event -> Bool
isRemove (Removed _ _) = True
isRemove _ = False