Skip to content

Instantly share code, notes, and snippets.

@aruss
Created January 17, 2024 10:15
Show Gist options
  • Save aruss/82b3ca000cc59dcdd284b154409890d7 to your computer and use it in GitHub Desktop.
Save aruss/82b3ca000cc59dcdd284b154409890d7 to your computer and use it in GitHub Desktop.
import subprocess
import json
import sys
def parse_version(version_str):
try:
# Remove any non-numeric characters like '^' or '~'
return [int(x) for x in version_str.replace('^', '').replace('~', '').split('.')]
except ValueError:
# Handle non-numeric versions differently or skip them
print(f"\033[91mERROR: not able to compare version string \"{version_str}\"\033[0m")
return [0] # You can choose how to handle these cases
def compare_versions(section, dataA, dataB):
for dep, versionA in dataA.get(section, {}).items():
versionB = dataB.get(section, {}).get(dep)
if versionB and parse_version(versionA) < parse_version(versionB):
yield dep, versionB
def addPackage(name, version, isDev=False):
print(f"yarn add {name}@{version} {'--dev' if isDev else ''}")
command = "yarn"
args = ["add", f"{name}@{version}"]
if isDev:
args.append("--dev")
try:
subprocess.run([command] + args, check=True)
except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")
def loadFile(path):
with open(path, 'r') as f:
return json.load(f)
def main():
if len(sys.argv) != 3:
print("Usage: python sys.argv[0] package.json <path_to_master_package.json>")
sys.exit(1)
dataA = loadFile(sys.argv[1])
dataB = loadFile(sys.argv[2])
for dep in compare_versions('dependencies', dataA, dataB):
addPackage(dep[0], dep[1])
for dep in compare_versions('devDependencies', dataA, dataB):
addPackage(dep[0], dep[1], True)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment