import json
import pathlib
import re
import subprocess
import sys
import tomllib
ROOT = pathlib.Path(__file__).resolve().parents[1]
VERSION = sys.argv[1] if len(sys.argv) == 2 else ""
if not re.fullmatch(r"\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?", VERSION):
raise SystemExit("usage: sync-release-version.py X.Y.Z")
SELECTORS = ["bindings/node/package.json", "cli/package.json"]
synced = 0
for relative in SELECTORS:
manifest_path = ROOT / relative
if not manifest_path.is_file():
raise SystemExit(f"selector manifest does not exist: {relative}")
manifest = json.loads(manifest_path.read_text())
pins = manifest.get("optionalDependencies", {})
siblings = [name for name in pins if name.startswith("@turndb/")]
if not siblings:
raise SystemExit(f"{relative} pins no @turndb platform package; is it still a selector?")
for name in siblings:
pins[name] = VERSION
synced += 1
manifest_path.write_text(json.dumps(manifest, indent=2) + "\n")
print(f"synced {synced} platform pins across {len(SELECTORS)} selectors to {VERSION}")
lock = tomllib.loads((ROOT / "Cargo.lock").read_text())
stale_local_packages = [
package["name"]
for package in lock.get("package", [])
if "source" not in package and package.get("version") != VERSION
]
for package_name_in_lock in stale_local_packages:
subprocess.run(
["cargo", "update", "-p", package_name_in_lock, "--precise", VERSION],
cwd=ROOT,
check=True,
)
lock_path = ROOT / "bindings/node/package-lock.json"
npm_lock = json.loads(lock_path.read_text())
npm_lock["version"] = VERSION
npm_lock["packages"][""]["version"] = VERSION
for name in [n for n in npm_lock["packages"][""].get("optionalDependencies", {}) if n.startswith("@turndb/")]:
npm_lock["packages"][""]["optionalDependencies"][name] = VERSION
npm_lock["packages"][f"node_modules/{name}"] = {"optional": True}
lock_path.write_text(json.dumps(npm_lock, indent=2) + "\n")
report_path = ROOT / "THIRD_PARTY_LICENSES.html"
report = report_path.read_text()
workspace_names = {package["name"] for package in lock.get("package", []) if "source" not in package}
for name in sorted(workspace_names):
report = re.sub(
rf"(>{re.escape(name)} )\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(</a>)",
rf"\g<1>{VERSION}\g<2>",
report,
)
report_path.write_text(report)