-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (82 loc) · 1.97 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
package main
import (
"fmt"
"os"
"path/filepath"
)
var CSSFilesToLookFor = [...]string{
"main.css",
"app.css",
"index.css",
"globals.css",
"global.css",
}
func searchFile(root, fileName string) (string, error) {
var foundPath string
err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && d.Name() == "node_modules" {
return filepath.SkipDir
}
if !d.IsDir() && d.Name() == fileName {
foundPath = path
return filepath.SkipDir
}
return nil
})
if err != nil {
return "", err
}
if foundPath != "" {
absPath, err := filepath.Abs(foundPath)
if err != nil {
return "", err
}
return absPath, nil
}
return "", nil
}
func writeTheCSSReset(path string, cssResetBuffer []byte) {
existingContent, err := os.ReadFile(path)
if err != nil {
fmt.Printf("Error reading existing file: %v\n", err)
return
}
combinedContent := append(existingContent, []byte("\n\n")...)
combinedContent = append(combinedContent, cssResetBuffer...)
err = os.WriteFile(path, combinedContent, 0644)
if err != nil {
fmt.Printf("Error writing CSS reset file: %v\n", err)
} else {
fmt.Println(`CSS reset has been added to your project! 🎉`)
}
}
func main() {
var cssFilePath string
for i := 0; i < len(CSSFilesToLookFor); i++ {
var search, searchErr = searchFile(".", CSSFilesToLookFor[i])
if searchErr != nil {
fmt.Printf("Error reading CSS reset file: %v\n", searchErr)
return
}
if search != "" {
cssFilePath = search
break
}
}
execPath, err := os.Executable()
if err != nil {
fmt.Printf("Error getting executable path: %v\n", err)
return
}
execDir := filepath.Dir(execPath)
resetCSSPath := filepath.Join(execDir, "resetcss")
cssResetReadBuffer, cssResetReadErr := os.ReadFile(resetCSSPath)
if cssResetReadErr != nil {
fmt.Printf("Error reading CSS reset file: %v\n", cssResetReadErr)
return
}
writeTheCSSReset(cssFilePath, cssResetReadBuffer)
}