-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
64 lines (55 loc) · 2.05 KB
/
update.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
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
import json
import os
import shutil
import subprocess
import re
overrides = {
# Copy from package.json what you don't want to update.
# Example:
# "react": "17.0.0",
"eslint": "8.57.0",
}
def find_version(text, version):
matches = re.findall(rf"{version}:\s+(.+)(\s+|$)", text)
return version if not matches else f"^{matches[0][0]}"
def main():
with open("package.json") as f:
package = json.load(f)
print("\n = Checking dependencies\n")
for dep_type in ["devDependencies", "dependencies"]:
for dep, version in package.get(dep_type, {}).items():
info = subprocess.run(
["npm", "dist-tags", dep], stdout=subprocess.PIPE, check=True)
info = info.stdout.decode()
latest = find_version(info, "latest")
custom = overrides.get(dep, "latest")
wanted = find_version(info, custom)
if wanted and latest:
if wanted == version:
print(
f"- {dep} already using the '{custom}' "
f"version {version}")
else:
print(f"- updating {dep} from {version} to {wanted}")
if wanted != latest:
print(f" | the 'latest' version is at {latest}")
package[dep_type][dep] = wanted
else:
print(f"- failed to find {wanted} version for {dep}")
with open("package.json", "w") as f:
json.dump(package, f, indent=2)
f.write("\n")
shutil.rmtree("./node_modules", ignore_errors=True)
try:
os.remove("./package-lock.json")
except OSError:
pass
print("\n = Installing modules\n")
subprocess.run(["npm", "install", "--force"], check=False)
if not overrides:
print("\n = Fixing audit issues\n")
subprocess.run(["npm", "audit", "fix", "--force"], check=False)
print("\n = Deduplicating dependencies\n")
subprocess.run(["npm", "dedup", "--force"], check=False)
if __name__ == "__main__":
main()