-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (124 loc) · 3.05 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
human "github.com/dustin/go-humanize"
"github.com/skratchdot/open-golang/open"
)
type jsonRPCRequest struct {
Persist bool `json:"DontHideAfterAction"`
Method string `json:"method"`
Parameters []interface{} `json:"parameters"`
}
type jsonRPCResponse struct {
Result []resultItem
}
type resultItem struct {
Title string `json:"Title"`
SubTitle string `json:"SubTitle"`
IcoPath string `json:"IcoPath"`
Action *jsonRPCRequest `json:"JsonRPCAction,omitempty"`
}
type suggestion struct {
Name string `json:"name"`
Description string `json:"description"`
}
type detail struct {
Gzip uint64 `json:"gzip"`
Size uint64 `json:"size"`
Error interface{} `json:"error,omitempty"`
}
func main() {
req, err := parseRPCRequest(os.Args[1])
if err != nil {
return
}
if req.Method == "openBrowser" {
_ = open.Start("https://bundlephobia.com/result?p=" + req.Parameters[0].(string))
return
}
q := strings.TrimSpace(req.Parameters[0].(string))
if q[len(q)-1] == '!' {
handleDetail(strings.TrimRight(q, "!"))
} else {
handlePkgSuggestion(q)
}
}
func parseRPCRequest(str string) (*jsonRPCRequest, error) {
var ret *jsonRPCRequest
if err := json.Unmarshal([]byte(str), &ret); err != nil {
return nil, err
}
return ret, nil
}
func sendRPCResultItems(result []resultItem) error {
b, err := json.Marshal(jsonRPCResponse{Result: result})
if err != nil {
return err
}
_, err = os.Stdout.Write(b)
return err
}
func handlePkgSuggestion(q string) {
if len(q) < 2 {
return
}
resp, err := http.Get("https://www.npmjs.com/search/suggestions?q=" + q)
if err != nil {
return
}
defer resp.Body.Close()
var suggestions []suggestion
if err = json.NewDecoder(resp.Body).Decode(&suggestions); err != nil {
return
}
items := make([]resultItem, len(suggestions))
for idx, suggestion := range suggestions {
items[idx] = resultItem{
Title: suggestion.Name,
SubTitle: suggestion.Description,
IcoPath: "icon.png",
Action: &jsonRPCRequest{
Persist: true,
Method: "Wox.ChangeQuery",
Parameters: []interface{}{"nbp " + suggestion.Name + "!", true},
},
}
}
_ = sendRPCResultItems(items)
}
func handleDetail(q string) {
resp, err := http.Get("https://bundlephobia.com/api/size?package=" + q)
if err != nil {
return
}
defer resp.Body.Close()
var detail detail
if err = json.NewDecoder(resp.Body).Decode(&detail); err != nil {
return
}
if detail.Error != nil {
_ = sendRPCResultItems([]resultItem{
{
Title: "Not found : " + q,
SubTitle: "The package you were looking for doesn't exist.",
IcoPath: "icon.png",
},
})
return
}
_ = sendRPCResultItems([]resultItem{
{
Title: fmt.Sprintf("minified: %s, gzipped: %s", human.Bytes(detail.Size), human.Bytes(detail.Gzip)),
SubTitle: "Open your browser for more information.",
IcoPath: "icon.png",
Action: &jsonRPCRequest{
Method: "openBrowser",
Parameters: []interface{}{q},
},
},
})
}