-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_esm.py
37 lines (29 loc) · 906 Bytes
/
patch_esm.py
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
import json
from pathlib import Path
pkg = Path("./pkg")
with open(pkg / "package.json", encoding="utf-8") as f:
package = json.load(f)
package["type"] = "module"
package["main"] = "egm96.js"
with open(pkg / "package.json", "w", encoding="utf-8") as f:
json.dump(package, f, indent=2)
with open(pkg / "egm96.js", encoding="utf-8") as f:
lines = f.readlines()
patched = False
with open(pkg / "egm96.js", "w", encoding="utf-8") as f:
for line in lines:
if line.strip() == "input = fetch(input);":
f.write(
"""try {
input = await fetch(input);
} catch (e) {
if (!(e instanceof TypeError)) {
throw e;
}
input = await (await import("node:fs/promises")).readFile(input);
}"""
)
patched = True
else:
f.write(line)
assert patched