-
-
Notifications
You must be signed in to change notification settings - Fork 343
/
main.go
84 lines (65 loc) · 1.64 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
package main
import (
"fmt"
"os/exec"
"regexp"
"strings"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/gson"
)
func main() {
list := getList()
out := `// generated by "lib/js/generate"` +
"\n\npackage js\n\n"
for _, fn := range list.Arr() {
name := fn.Get("name").Str()
def := fn.Get("definition").Str()
out += utils.S(`
var {{.Name}} = &Function{
Name: "{{.name}}",
Definition: {{.definition}},
Dependencies: {{.dependencies}},
}
`,
"Name", fnName(name),
"name", name,
"definition", utils.EscapeGoString(def),
"dependencies", getDeps(def),
)
}
utils.E(utils.OutputFile("lib/js/helper.go", out))
utils.Exec("gofmt", "-s", "-w", "lib/js/helper.go")
}
var regDeps = regexp.MustCompile(`\Wfunctions.(\w+)`)
func getDeps(fn string) string {
ms := regDeps.FindAllStringSubmatch(fn, -1)
list := []string{}
for _, m := range ms {
list = append(list, fnName(m[1]))
}
return "[]*Function{" + strings.Join(list, ",") + "}"
}
func fnName(name string) string {
return strings.ToUpper(name[0:1]) + name[1:]
}
func getList() gson.JSON {
code, err := exec.Command("npx", "-q", "uglify-es", "-c", "-m", "--", "lib/js/helper.js").CombinedOutput()
if err != nil {
panic(string(code))
}
script := fmt.Sprintf(`
%s
const list = []
for (const name in functions) {
const reg = new RegExp('^(async )?' + name)
const definition = functions[name].toString().replace(reg, '$1function')
list.push({name, definition})
}
console.log(JSON.stringify(list))
`, string(code))
b, err := exec.Command("node", "-e", script).CombinedOutput()
if err != nil {
panic(string(b))
}
return gson.New(b)
}