-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmain.go
118 lines (100 loc) · 2.75 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
118
package main
import (
"fmt"
"github.com/Gioni06/terminalcss/internal/build"
"github.com/fsnotify/fsnotify"
"log"
"net/http"
"os"
"path/filepath"
)
func main() {
// Get the current working directory
cwd, err := os.Getwd()
if err != nil {
log.Fatal("Error getting current working directory:", err)
}
// libPath is the path to the terminal.css library source
libPath := filepath.Join(cwd, "lib", "terminal.css")
// libPublishPath is the path to the terminal.css library publish directory that gets published to npm
libPublishPath := filepath.Join(cwd, "dist")
// docsPath is the path to the destination directory
docsPath := filepath.Join(cwd, "public")
// clear docsPath directory
err = os.RemoveAll(docsPath)
err = os.RemoveAll(libPublishPath)
library := build.Library{
SourcePath: libPath,
DestinationPaths: []string{libPublishPath, docsPath},
}
library.Build()
layoutsPath := filepath.Join(cwd, "templates/layouts")
partialsPath := filepath.Join(cwd, "templates/partials")
pagesPath := filepath.Join(cwd, "pages")
staticAssetsPath := filepath.Join(cwd, "static")
docs := build.Docs{
PackageJSONPath: filepath.Join(cwd, "package.json"),
DestinationPath: docsPath,
LayoutsPath: layoutsPath,
PartialsPath: partialsPath,
PagesPath: pagesPath,
StaticAssetsPath: staticAssetsPath,
}
err = docs.Build()
if err != nil {
panic(err)
}
// Check if program has the --serve flag
if len(os.Args) == 2 && os.Args[1] == "--serve" {
// Setup watchers for libPath and docsPath
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Function to rebuild library and docs
rebuild := func() {
library.Build()
err = docs.Build()
if err != nil {
log.Println("Error rebuilding docs:", err)
}
}
// Watch for changes in libPath and docsPath
pathsToWatch := []string{libPath, pagesPath, layoutsPath, partialsPath, staticAssetsPath}
for _, path := range pathsToWatch {
err = watcher.Add(path)
if err != nil {
log.Fatal(err)
}
}
// Start a goroutine to handle events
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("Detected change, rebuilding...")
rebuild()
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Watcher error:", err)
}
}
}()
fmt.Println("Serving dist directory at http://localhost:8080. Press Ctrl+C to stop. Restart the program to rebuild the docs.")
// serve dist directory at localhost:8080
port := ":8080"
handler := http.FileServer(http.Dir(docsPath))
err = http.ListenAndServe(port, handler)
if err != nil {
return
}
}
}