-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
248 lines (178 loc) · 5.2 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/spf13/cobra"
)
func main() {
if err := rootCmd().Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func rootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dl-esm PAKCAGE DIR[optional]",
Short: "Download ESM modules from npm and jsdelivr",
Long: "Download ESM modules from npm and jsdelivr",
Example: `
# download latest version of solid js
dl-easm solid-js
# download a specific version of solid js
dl-easm [email protected]
# download to a specific dir
dl-easm [email protected] /tmp
`,
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
location := "."
packageArg := args[0]
if len(args) == 2 {
location = args[1]
}
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)
}
pkgVer := extractPkgVer(code, packageArg)
path := simplifyPath(fmt.Sprintf("/npm/%s/ esm", pkgVer))
// 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
},
}
return cmd
}
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 extractPkgVer(content string, pkgName string) string {
// if pkgName includes version then return
r := regexp.MustCompile(`([^@] )@(\d \.\d \.\d )`)
matches := r.FindStringSubmatch(pkgName)
if len(matches) > 2 {
return pkgName
}
// extract path from the file
pattern := `Original file: (\/npm\/[^\s] )`
r = regexp.MustCompile(pattern)
matches = r.FindStringSubmatch(content)
if len(matches) < 1 {
panic("Could not find original file")
}
// split sub dirs if there is one
pkgNameSubDir := strings.SplitN(pkgName, "/", 2)
subDir := ""
if len(pkgNameSubDir) == 2 {
subDir = "/" pkgNameSubDir[1]
pkgName = pkgNameSubDir[0]
}
// extract package and version
pattern = fmt.Sprintf(`/%s@(\d \.\d \.\d )`, pkgName)
r = regexp.MustCompile(pattern)
matches = r.FindStringSubmatch(matches[1])
if len(matches) < 2 {
panic("unable to extract package info")
}
return pkgName "@" matches[1] subDir
}
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], "/dist")[0]
packageVersion = strings.ReplaceAll(packageVersion, "/ esm", "")
packageVersion = strings.ReplaceAll(packageVersion, ".", "-")
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
}