-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (97 loc) · 2.09 KB
/
main.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
)
func main() {
isWatchMode := len(os.Args) > 1 && os.Args[1] == "watch"
if isWatchMode {
fmt.Println("Watching for changes...")
compile()
watch()
} else {
compile()
}
}
func watch() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println("Error creating watcher:", err)
return
}
defer watcher.Close()
// Create a channel to receive events
done := make(chan bool)
// Start watching in a goroutine
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
fmt.Println("Modified file:", event.Name)
compile()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
fmt.Println("Error:", err)
}
}
}()
// Add directory to watch
err = watcher.Add("./ui/components")
if err != nil {
fmt.Println("Error adding watch:", err)
return
}
<-done // Block forever
}
func compile() {
// Your existing compile function remains the same
start := time.Now()
fmt.Println("Compiling components...")
files := []string{}
var match []string
var content string
match, err := filepath.Glob("./ui/components/*.css")
if err != nil {
fmt.Println(err)
}
for _, file := range match {
fileContent, err := os.ReadFile(file)
fileContentToString := string(fileContent)
if err != nil {
panic(err)
}
content = content + fileContentToString
files = append(files, file)
}
// create dist folder if it doesn't exist
distExists, err := os.Stat("./dist")
if os.IsNotExist(err) {
os.Mkdir("./dist", 0755)
} else if distExists.IsDir() == false {
os.Mkdir("./dist", 0755)
}
// Write to ./dist/tailframe.build.css
err = os.WriteFile("./dist/tailframe.build.css", []byte(content), 0644)
if err != nil {
panic(err)
}
var cmdString string = "action tailwind"
cmd := exec.Command("bash", "-c", cmdString)
err = cmd.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Done in", time.Since(start).Milliseconds(), "ms")
}