forked from bastean/codexgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopydeps.go
More file actions
113 lines (85 loc) · 2.84 KB
/
copydeps.go
File metadata and controls
113 lines (85 loc) · 2.84 KB
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
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
regExpEveryMinFile = `^.+\.min\.(js|css)$`
regExpEveryWoff2File = `^.+\.woff2$`
)
const (
staticPath = "internal/app/server/static/dist"
)
const (
jquerySourcePath = "node_modules/jquery/dist"
jqueryStaticPath = staticPath + "/jquery.com"
)
const (
fomanticSourcePath = "node_modules/fomantic-ui/dist"
fomanticStaticPath = staticPath + "/fomantic-ui.com"
)
const (
lodashSourcePath = "node_modules/lodash"
lodashStaticPath = staticPath + "/lodash.com"
)
func Panic(who error, what, where string) {
log.Panicf("(%s): %s: [%s]", where, what, who)
}
func CreateDirectory(path string) {
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
Panic(err, fmt.Sprintf("failed to create \"%s\"", path), "CreateDirectory")
}
log.Printf("created: \"%s\"", path)
}
func CopyFile(filename, sourcePath, targetPath string) {
data, err := os.ReadFile(filepath.Join(sourcePath, filepath.Base(filename)))
if err != nil {
Panic(err, fmt.Sprintf("failed to read \"%s\" from \"%s\"", filename, sourcePath), "CopyFile")
}
err = os.WriteFile(filepath.Join(targetPath, filepath.Base(filename)), data, os.ModePerm)
if err != nil {
Panic(err, fmt.Sprintf("failed to write \"%s\" on \"%s\"", filename, targetPath), "CopyFile")
}
log.Printf("created: \"%s\"", filepath.Join(targetPath, filepath.Base(filename)))
}
func CopyDeps(filenames []string, sourcePath, targetPath string) {
files, err := os.ReadDir(sourcePath)
if err != nil {
Panic(err, fmt.Sprintf("failed to copy \"%s\" from \"%s\"", filenames, sourcePath), "CopyDeps")
}
CreateDirectory(targetPath)
if strings.HasPrefix(filenames[0], "^") && strings.HasSuffix(filenames[0], "$") {
isMinFile := regexp.MustCompile(filenames[0]).MatchString
for _, file := range files {
if isMinFile(file.Name()) {
CopyFile(file.Name(), sourcePath, targetPath)
}
}
return
}
for _, filename := range filenames {
for _, file := range files {
if filepath.Base(filename) == file.Name() {
CopyFile(filename, sourcePath, targetPath)
}
}
}
}
func main() {
err := os.RemoveAll(staticPath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
Panic(err, fmt.Sprintf("failed to remove \"%s\"", staticPath), "main")
}
CreateDirectory(staticPath)
CopyDeps([]string{"jquery.min.js"}, jquerySourcePath, jqueryStaticPath)
CopyDeps([]string{"semantic.min.js", "semantic.min.css"}, fomanticSourcePath, fomanticStaticPath)
CopyDeps([]string{regExpEveryMinFile}, filepath.Join(fomanticSourcePath, "components"), filepath.Join(fomanticStaticPath, "components"))
CopyDeps([]string{regExpEveryWoff2File}, filepath.Join(fomanticSourcePath, "themes/default/assets/fonts"), filepath.Join(fomanticStaticPath, "themes/default/assets/fonts"))
CopyDeps([]string{"lodash.min.js"}, lodashSourcePath, lodashStaticPath)
}