diff --git a/go.mod b/go.mod index 47258e9..4f57e89 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,6 @@ module github.com/donuts-are-good/bearclaw/v2 go 1.19 require ( - github.com/fsnotify/fsnotify v1.7.0 github.com/russross/blackfriday v1.6.0 mpldr.codes/ansi v1.5.0 ) - -require golang.org/x/sys v0.14.0 // indirect diff --git a/go.sum b/go.sum index 26af7b3..b0555ff 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,4 @@ -github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= -github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww= github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= mpldr.codes/ansi v1.5.0 h1:jAAtDMwU/DC7OPxmrVFpmM3RrSQDcMeLhuk6BuEoWZc= mpldr.codes/ansi v1.5.0/go.mod h1:SYuKX0r6nxvySxMxzQX9frQaKLjEqKapjjg17euonMc= diff --git a/main.go b/main.go index 5ed1541..4c71be5 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,7 @@ import ( "path/filepath" "sort" "strings" - - "github.com/fsnotify/fsnotify" + "time" ) // init runs before main() @@ -65,34 +64,75 @@ func recreateHeaderFooterFiles(templatesFolder string) error { return nil } -// watchfolderforchange will watch an individual folder for +// watchFolderForChange will watch an individual folder for // any type of change, then trigger a rebuild func watchFolderForChange(folder string) { + // Get the initial modification time of the folder + initialModTime := getFolderModTime(folder) - // make a watcher with fsnotify - watcher, err := fsnotify.NewWatcher() - if err != nil { - log.Fatalf("unable to watch %s : %v", folder, err) - } - defer watcher.Close() + for { + // Sleep for a certain duration before checking for changes + time.Sleep(1 * time.Second) - err = watcher.Add(folder) - if err != nil { - log.Fatalf("couldn't add a watcher: %v", err) - } + // Get the current modification time of the folder + currentModTime := getFolderModTime(folder) - for { - select { - case event := <-watcher.Events: - log.Println("modified:", event.Name, " - rebuilding files..") + // Compare the current modification time with the initial one + if currentModTime.After(initialModTime) { + log.Println("Folder modified - rebuilding files..") markdownToHTML(inFolder, outFolder, templateFolder) createPostList(inFolder, outFolder, templateFolder) - case err := <-watcher.Errors: - log.Println("error:", err) + + // Update the initial modification time + initialModTime = currentModTime } } } +// getFolderModTime returns the modification time of a folder +func getFolderModTime(folder string) time.Time { + info, err := os.Stat(folder) + if err != nil { + log.Fatalf("unable to get folder info: %v", err) + } + return info.ModTime() +} + +// sometimes even though it worked well, i regretted adding fsnotify. +// during the period I was writing bearclaw I was using as much stdlib +// as possible before reaching out to third party solutions. +// Not thinking this one through, or not being simple enough resulted +// in me choosing fsnotify and i always regretted it. not because its +// bad, but because i didnt try harder. we'll see if this new way works. + +// // watchfolderforchange will watch an individual folder for +// // any type of change, then trigger a rebuild +// func watchFolderForChange(folder string) { + +// // make a watcher with fsnotify +// watcher, err := fsnotify.NewWatcher() +// if err != nil { +// log.Fatalf("unable to watch %s : %v", folder, err) +// } +// defer watcher.Close() + +// err = watcher.Add(folder) +// if err != nil { +// log.Fatalf("couldn't add a watcher: %v", err) +// } + +// for { +// select { +// case event := <-watcher.Events: +// log.Println("modified:", event.Name, " - rebuilding files..") +// markdownToHTML(inFolder, outFolder, templateFolder) +// createPostList(inFolder, outFolder, templateFolder) +// case err := <-watcher.Errors: +// log.Println("error:", err) +// } +// } +// } + // watchfoldersforchanges loops through a list of folders and // passes them to watchfolderforchange func watchFoldersForChanges(folders []string) {