-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (89 loc) · 2.3 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
package main
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/mlctrez/bolt"
"github.com/mlctrez/goapp-pf/scripts"
)
var tsInspect = "tsInspect.ts"
func main() {
defer scripts.Recover()
noErr := scripts.NoErr
nodeDir := scripts.NodeTempDir()
copyTypeScript(nodeDir, tsInspect)
db := scripts.Bolt(scripts.InspectDatabase)
defer func() { scripts.BoltClose(db) }()
componentBucket := bolt.Key("components")
layoutBucket := bolt.Key("layouts")
keys := bolt.Keys{componentBucket, layoutBucket}
noErr(db.CreateBuckets(keys))
noErr(os.Chdir("temp/node"))
paths := make(map[bolt.Key][]string)
for _, key := range keys {
path := filepath.Join("node_modules/@patternfly/react-core/src", string(key))
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error {
if info != nil && strings.HasSuffix(info.Name(), ".tsx") {
if strings.Contains(path, "__tests__") || strings.Contains(path, "examples") {
return nil
}
if strings.HasSuffix(path, "index.tsx") {
return nil
}
paths[key] = append(paths[key], path)
}
return nil
})
noErr(err)
}
for key, pathList := range paths {
var savePaths []string
for _, path := range pathList {
v := &bolt.Value{K: bolt.Key(path)}
if err := db.Get(key, v); err == bolt.ErrValueNotFound {
savePaths = append(savePaths, path)
}
if len(savePaths) > 9 {
write(db, key, savePaths...)
savePaths = []string{}
}
}
if len(savePaths) > 9 {
write(db, key, savePaths...)
}
}
}
func write(db *bolt.Bolt, key bolt.Key, savePaths ...string) {
var data []byte
var err error
noErr := scripts.NoErr
args := []string{tsInspect}
args = append(args, savePaths...)
fmt.Println(args)
data, err = exec.Command("node", args...).CombinedOutput()
if err != nil {
fmt.Println(string(data))
noErr(err)
}
m := make(map[string]interface{})
noErr(json.Unmarshal(data, &m))
for p, dataJson := range m {
data, err = json.Marshal(dataJson)
noErr(err)
v := &bolt.Value{K: bolt.Key(p), V: data}
noErr(db.Put(key, v))
}
}
//go:embed *.txt
var tsFiles embed.FS
func copyTypeScript(dir, name string) {
data, err := tsFiles.ReadFile(name + ".txt")
scripts.NoErr(err)
err = os.WriteFile(filepath.Join(dir, name), data, 0644)
scripts.NoErr(err)
}