-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 1.25 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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"time"
)
const outDir = "out"
func main() {
if err := os.RemoveAll(outDir); err != nil {
panic(err)
}
if err := os.MkdirAll(outDir, 0755); err != nil {
panic(err)
}
build()
if len(os.Args) > 1 && (os.Args[1] == "watch" || os.Args[1] == "w") {
if len(os.Args) > 2 {
serverAddr = os.Args[2]
}
serve()
watch()
}
}
func build() {
if !fileExists("index.html") {
logError("An index.html entry file is required at to root of the project")
return
}
done = make(map[string]string) // Reset file URLs
startTime := time.Now()
handleFile("index.html")
copySpecialFiles()
logSuccess(fmt.Sprintf("⚡️ SSG in %dms", time.Now().Sub(startTime).Milliseconds()))
}
// Copy potential special files to out directory that are generally not referenced in HTML but useful for the build
func copySpecialFiles() {
for fn := range map[string]struct{}{
".htaccess": {},
"robots.txt": {},
} {
from, err := os.Open(fn)
if err != nil {
if os.IsNotExist(err) {
continue
}
panic(err)
}
defer from.Close()
to, err := os.Create(filepath.Join(outDir, fn))
if err != nil {
panic(err)
}
defer to.Close()
_, err = io.Copy(to, from)
if err != nil {
panic(err)
}
}
}