forked from Jelmerro/Vieb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
72 lines (64 loc) · 2.62 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
# Vieb - Vim Inspired Electron Browser
# Copyright (C) 2021-2022 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():
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", "--legacy-peer-deps"], check=False)
print("\n = Fixing audit issues\n")
subprocess.run(["npm", "audit", "fix", "--legacy-peer-deps"], check=False)
print("\n = Deduplicating dependencies\n")
subprocess.run(["npm", "dedup", "--legacy-peer-deps"], check=False)
if __name__ == "__main__":
main()