-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
220 lines (155 loc) · 4.33 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/urfave/cli/v2"
)
func main() {
app := &cli.App{
Name: "download-esm",
Usage: "Download ESM modules from npm and jsdelivr",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "package",
Aliases: []string{"p"},
Usage: "Package to download",
Required: true,
},
&cli.StringFlag{
Name: "location",
Aliases: []string{"l"},
Usage: "Location to save files",
DefaultText: ".",
},
},
Action: func(c *cli.Context) error {
packageArg := c.String("package")
location := c.String("location")
root := filepath.Join(location)
if _, err := os.Stat(root); os.IsNotExist(err) {
os.MkdirAll(root, os.ModePerm)
}
var code string
if strings.HasPrefix(packageArg, "https://") {
response, err := http.Get(packageArg)
if err != nil {
return err
}
defer response.Body.Close()
bytes, err := io.ReadAll(response.Body)
if err != nil {
return err
}
code = string(bytes)
} else {
code = fetchCode(packageArg)
}
originalFile := extractOriginalFile(code)
path := simplifyPath(originalFile)
// Rewrite code and save to path
rewrittenCode, capturedPaths := rewriteCode(code)
err := os.WriteFile(filepath.Join(root, path), []byte(rewrittenCode), 0644)
if err != nil {
return err
}
fmt.Fprintln(os.Stderr, path)
// Do the same thing for all the captured paths, recursively
toFetch := map[string]string{}
for k, v := range capturedPaths {
toFetch[k] = v
}
for len(toFetch) > 0 {
for path, simplifiedPath := range toFetch {
delete(toFetch, path)
url := "https://cdn.jsdelivr.net" + path
response, err := http.Get(url)
if err != nil {
return err
}
defer response.Body.Close()
bytes, err := io.ReadAll(response.Body)
if err != nil {
return err
}
code := string(bytes)
rewrittenCode, moreCapturedPaths := rewriteCode(code)
err = os.WriteFile(filepath.Join(root, simplifiedPath), []byte(rewrittenCode), 0644)
if err != nil {
return err
}
fmt.Fprintln(os.Stderr, simplifiedPath)
for k, v := range moreCapturedPaths {
toFetch[k] = v
}
}
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func fetchCode(packageArg string) string {
url := fmt.Sprintf("https://cdn.jsdelivr.net/npm/%s/+esm", packageArg)
response, err := http.Get(url)
if err != nil {
panic(err)
}
defer response.Body.Close()
bytes, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
return string(bytes)
}
func extractOriginalFile(content string) string {
pattern := `Original file: (\/npm\/[^\s]+)`
re := regexp.MustCompile(pattern)
match := re.FindStringSubmatch(content)
if len(match) > 1 {
return match[1]
} else {
panic("Could not find original file")
}
}
func simplifyPath(path string) string {
split := strings.Split(path, "/npm/")
split[1] = strings.TrimPrefix(split[1], "@")
packageInfo := strings.SplitN(split[1], "@", 2)
packageName := strings.ReplaceAll(packageInfo[0], "/", "-")
packageVersion := strings.Split(packageInfo[1], "/")[0]
packageVersion = strings.ReplaceAll(packageVersion, ".", "-")
simplifiedName := fmt.Sprintf("%s-%s.js", packageName, packageVersion)
return simplifiedName
}
func rewriteCode(code string) (string, map[string]string) {
pattern := `(?P<keyword>import|export)\s*(?P<imports>\{?[^}]+?\}?)\s*from\s*"(?P<path>\/npm\/[^"]+)"`
re := regexp.MustCompile(pattern)
capturedPaths := map[string]string{}
replaceImport := func(match string) string {
submatch := re.FindStringSubmatch(match)
keyword := submatch[1]
imports := submatch[2]
path := submatch[3]
simplifiedPath := simplifyPath(path)
capturedPaths[path] = simplifiedPath
return fmt.Sprintf(`%s %s from "./%s";`, keyword, imports, simplifiedPath)
}
rewrittenCode := re.ReplaceAllStringFunc(code, replaceImport)
rewrittenCode = removeSourceMappingComments(rewrittenCode)
return rewrittenCode, capturedPaths
}
func removeSourceMappingComments(code string) string {
pattern := `\/\/#\s*sourceMappingURL=.*?\.map`
re := regexp.MustCompile(pattern)
cleanedCode := re.ReplaceAllString(code, "")
return cleanedCode
}