Skip to content

Instantly share code, notes, and snippets.

@aruss
Last active December 2, 2025 09:45
Show Gist options
  • Select an option

  • Save aruss/82b3ca000cc59dcdd284b154409890d7 to your computer and use it in GitHub Desktop.

Select an option

Save aruss/82b3ca000cc59dcdd284b154409890d7 to your computer and use it in GitHub Desktop.
import argparse
import json
import sys
from collections import OrderedDict
def sync_versions(target_path: str, source_path: str):
"""
Syncs dependency versions in target_path to match source_path.
Ignores versions starting with "workspace:libs/".
"""
IGNORE_PREFIX = "workspace:"
dep_sections = [
'dependencies',
'devDependencies',
'peerDependencies',
'optionalDependencies'
]
try:
with open(source_path, 'r', encoding='utf-8') as f_src:
source_data = json.load(f_src)
with open(target_path, 'r', encoding='utf-8') as f_tgt:
target_data = json.load(f_tgt, object_pairs_hook=OrderedDict)
modified = False
for section in dep_sections:
if section in target_data and section in source_data:
target_deps = target_data[section]
source_deps = source_data[section]
for pkg_name in target_deps:
if pkg_name in source_deps:
source_ver = source_deps[pkg_name]
target_ver = target_deps[pkg_name]
# Skip if either source or target uses the workspace prefix
if source_ver.startswith(IGNORE_PREFIX) or target_ver.startswith(IGNORE_PREFIX):
continue
if target_ver != source_ver:
target_deps[pkg_name] = source_ver
modified = True
if modified:
with open(target_path, 'w', encoding='utf-8') as f_tgt:
f_tgt.write(json.dumps(target_data, indent=2) + '\n')
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"Error processing files: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Sync package.json versions from source to target.")
parser.add_argument("target", help="Path to target package.json")
parser.add_argument("source", help="Path to source package.json")
args = parser.parse_args()
sync_versions(args.target, args.source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment