-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforGithub.go
96 lines (76 loc) · 2.44 KB
/
forGithub.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
package main
import (
"fmt"
"io/ioutil"
"regexp"
"strings"
"github.com/spf13/viper"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/js"
"github.com/tdewolff/minify/json"
)
var m *minify.M
func init() {
m = minify.New()
m.AddFunc("text/css", css.Minify)
m.AddFunc("text/js", js.Minify)
m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
}
func main() {
viper.SetConfigName("config")
viper.AddConfigPath(".")
errViperRead := viper.ReadInConfig()
if errViperRead != nil {
panic(fmt.Errorf("Fatal error config file: %s", errViperRead))
}
var jsonLink = viper.GetString("jsonLink")
replace("./src/index.html", "./index.html", []string{"../static/","../src/", "http://localhost:8080", "loc_data = r.response.data;"}, []string{"./static/","./static/", jsonLink, "loc_data = r.response;"})
replace("./src/data.json", "./data.json", []string{"name", "url", "locations", "loc", "country"}, []string{"n", "u", "ls", "l", "c"})
replace("./src/render.js", "./static/render.js", []string{"e.name", "e.url", "e.locations", "e.loc", "e.country", "].loc", "].country"}, []string{"e.n", "e.u", "e.ls", "e.l", "e.c", "].l", "].c"})
replace("./src/index-main.js", "./static/index-main.js", []string{"].locations", "].loc", "].country"}, []string{"].ls", "].l", "].c"})
// NOTE THAT SOME FILES TO BE MINIFIED ARE IN STATIC
minifyOnly("./static/render.js", "./static/render.js", "text/js")
minifyOnly("./static/index-main.js", "./static/index-main.js", "text/js")
minifyOnly("./src/index-main.css", "./static/index-main.css", "text/css")
minifyOnly("./data.json", "./data.json", "application/json")
/*MINIFY
Json
render
index-main.js
css
*/
}
func replace(path, newPath string, replaceThis, withThis []string) {
read, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
if len(replaceThis) != len(withThis) {
panic("-_-")
}
oldContent := read
var newContents string
for i := 0; i < len(replaceThis); i++ {
newContents = strings.Replace(string(oldContent), replaceThis[i], withThis[i], -1)
oldContent = []byte(newContents)
}
err = ioutil.WriteFile(newPath, []byte(newContents), 0)
if err != nil {
panic(err)
}
}
func minifyOnly(path, newPath, mediatype string) {
read, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
read, err = m.Bytes(mediatype, read)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(newPath, read, 0)
if err != nil {
panic(err)
}
}