-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
295 lines (278 loc) · 7.67 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"embed"
_ "embed"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
"github.com/tdewolff/minify/v2/json"
"github.com/tdewolff/minify/v2/svg"
"github.com/tdewolff/minify/v2/xml"
)
var (
cmdWG sync.WaitGroup
version = "v0.0.12"
)
//go:embed site/*
var siteFS embed.FS
func main() {
log.Println("sitegen ", version)
var (
sitePath string
publicPath string
sourceDir string
dataDir string
tplDir string
port string
basePath string
exclude string
create bool
serve bool
clean bool
isMinify bool
buildAll bool
min *minify.M
ss *staticServer
sg *SiteGen
)
flag.BoolVar(&create, "create", false, "Creates a new site template")
flag.StringVar(&sitePath, "site", "./site", "Absolute or relative root site path")
flag.StringVar(&sourceDir, "source", "src", "Source folder relative to site path")
flag.StringVar(&dataDir, "data", "data", "Data folder relative to site path")
flag.StringVar(&tplDir, "templates", "templates", "Template folder relative to site path")
flag.StringVar(&publicPath, "public", "./public", "Absolute or relative public path")
flag.StringVar(&basePath, "base", "/", "Base folder relative to public path")
flag.BoolVar(&serve, "serve", false, "Start a development server and watcher")
flag.StringVar(&exclude, "exclude", "^(node_modules|bower_components)", "Exclude from watcher")
flag.BoolVar(&clean, "clean", false, "Clean public dir before build")
flag.BoolVar(&isMinify, "minify", false, "Minify (HTML|JS|CSS)")
flag.BoolVar(&buildAll, "buildall", false, "Always build all on change")
flag.StringVar(&port, "port", "8888", "Port for localhost")
flag.Parse()
if create {
if stat, err := os.Stat(sitePath); err == nil && stat.IsDir() {
log.Fatalf("Directory %s exists", sitePath)
}
if err := copySite("site", sitePath); err != nil {
log.Fatalf("Failed to create site: %v", err)
}
log.Println("Site template created: ", sitePath)
return
}
if isMinify {
min = minify.New()
min.AddFunc("text/css", css.Minify)
min.AddFunc("text/xml", xml.Minify)
min.AddFunc("application/js", js.Minify)
min.AddFunc("image/svg+xml", svg.Minify)
min.Add("text/html", &html.Minifier{
KeepDocumentTags: true,
})
min.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
min.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
min.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)
}
pubPath, err := filepath.Abs(publicPath)
if err != nil {
log.Fatalln("Error public path ", err)
}
if basePath == "" {
basePath = "/"
}
// should be url path
basePath = strings.ReplaceAll(basePath, "\\", "/")
if basePath != "/" {
basePath = "/" + strings.Trim(basePath, "/") + "/"
}
sg = newSiteGen(sitePath, tplDir, dataDir, sourceDir, pubPath, basePath, min, clean, serve)
sg.buildAll(false)
if sg.dev {
ss = newStaticServer(pubPath, basePath)
watcher, err := fsnotify.NewWatcher()
var mu sync.Mutex
events := make(map[string]bool)
if err != nil {
log.Fatalln("Watcher error", err)
}
defer watcher.Close()
processKey := func(key string) {
action := key[:3]
path := key[4:]
pp, err := filepath.Abs(path)
if err != nil {
log.Println("Failed to get absolute path ", path, " error ", err)
}
time.Sleep(time.Millisecond * 500)
fi, err := os.Stat(pp)
if err != nil {
log.Println("Stat error ", err)
}
if err == nil && fi.IsDir() {
if action == "add" {
if !excluded(exclude, strings.Replace(pp, sg.sitePath+string(os.PathSeparator), "", 1)) {
if err := watcher.Add(pp); err != nil {
log.Println("Watch dir ", pp, " error ", err)
}
}
} else {
if err := watcher.Remove(pp); err != nil {
log.Println("Watch stop dir ", pp, " error ", err)
}
}
} else {
rp := strings.Replace(pp, sg.sitePath, "", 1)
isSrc := strings.HasPrefix(rp, string(os.PathSeparator)+sourceDir)
switch action {
case "add":
if isSrc {
if s, ok := sg.sources[pp]; ok {
s.reloadContent()
} else {
if _, err := sg.newSource(pp, false); err != nil {
log.Println(path, " failed source ", err)
}
}
if err := sg.build(pp); err != nil {
log.Println("Build failed ", pp, " error ", err)
} else {
log.Println("Rebuilt: ", rp)
}
if buildAll {
sg.buildAll(true)
}
} else {
sg.buildAll(true)
}
case "del":
if isSrc {
if err := sg.remove(pp); err != nil {
log.Println("Remove failed ", pp, " error ", err)
} else {
log.Println("Deleted: ", rp)
}
if buildAll {
sg.buildAll(true)
}
} else {
sg.buildAll(true)
}
}
}
mu.Lock()
delete(events, key)
mu.Unlock()
cmdWG.Wait()
ss.notifier <- []byte("updated")
}
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
var op string
if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename {
op = "del"
} else if event.Op&fsnotify.Create == fsnotify.Create || event.Op&fsnotify.Write == fsnotify.Write {
op = "add"
}
if op != "" {
if n := strings.Split(event.Name, string(os.PathSeparator)); strings.HasPrefix(n[len(n)-1], ".") {
return
}
b := false
mu.Lock()
key := op + ":" + event.Name
if _, ok := events[key]; !ok {
events[key] = true
b = true
}
mu.Unlock()
if b {
go processKey(key)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
if err = watcher.Add(sg.sitePath); err != nil {
log.Fatalln("Watch dir ", tplDir, " error ", err)
}
filepath.Walk(sg.sitePath,
func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal("watch dir", path, "error", err)
}
if info.IsDir() && !strings.HasPrefix(path, sg.publicPath) {
if !excluded(exclude, strings.Replace(path, sg.sitePath+string(os.PathSeparator), "", 1)) {
err = watcher.Add(path)
if err != nil {
log.Fatal("watch dir", path, "error", err)
}
return nil
}
}
return nil
})
log.Println("Serving: ", publicPath, " at ", fmt.Sprintf("http://localhost:%s%s", port, basePath))
log.Println("Press Ctrl+C to stop")
http.Handle("/", ss)
log.Println(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
cmdWG.Wait()
}
func excluded(pattern, path string) bool {
if strings.HasPrefix(path, ".") {
return true
}
m, err := regexp.Match(pattern, []byte(path))
if err != nil {
log.Println("Watch exclude pattern", pattern, " error ", err)
return true
}
return m
}
func copySite(folder, target string) error {
entries, err := siteFS.ReadDir(folder)
if err != nil {
return fmt.Errorf("copySite %s ReadDir error %v", folder, err)
}
for _, entry := range entries {
p := filepath.Join(folder, entry.Name())
if entry.IsDir() {
if err := copySite(p, target); err != nil {
return err
}
} else {
b, err := siteFS.ReadFile(p)
if err != nil {
return fmt.Errorf("copySite ReadFile %s error %v", p, err)
}
to := filepath.Join(target, p[strings.Index(p, "/"):])
os.MkdirAll(filepath.Dir(to), os.ModePerm)
if err := ioutil.WriteFile(to, b, 0644); err != nil {
return fmt.Errorf("copySite WriteFile %s error %v", p, err)
}
}
}
return nil
}