forked from Jelmerro/Vieb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
85 lines (78 loc) · 3.25 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Vieb - Vim Inspired Electron Browser
# Copyright (C) 2021-2023 Jelmer van Arnhem
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import json
import os
import shutil
import subprocess
import re
overrides = {}
def find_version(text, version):
matches = re.findall(rf"{version}:\s+(.+)(\s+|$)", text)
return version if not matches else matches[0][0]
def main():
print("\n = Checking dependencies\n")
with open("package.json", encoding="utf-8") as f:
package = json.load(f)
for dep_type in ["devDependencies", "dependencies"]:
for dep, version in package.get(dep_type, {}).items():
if version.startswith("github:"):
print(f"- updating {dep} to the latest git version")
continue
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", encoding="utf-8") 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"], check=True)
print("\n = Fixing audit issues\n")
subprocess.run(["npm", "audit", "fix"], check=False)
print("\n = Deduplicating dependencies\n")
subprocess.run(["npm", "dedup"], check=True)
print("\n = Fixing package-lock issues\n")
with open("package-lock.json", encoding="utf-8") as f:
package_lock = json.load(f)
for package in package_lock["packages"]:
if "electron" in package_lock["packages"][package].get(
"peerDependencies", {}):
del package_lock["packages"][package]["peerDependencies"]
with open("package-lock.json", "w", encoding="utf-8") as f:
json.dump(package_lock, f, indent=2)
f.write("\n")
subprocess.run(["npm", "ci"], check=True)
if __name__ == "__main__":
main()