Files
turso/scripts/update-version.py
Pekka Enberg e76a8448f4 bindings/js: pin esbuild to 0.28.2 and drop the npm lockfile
The dependency tree carried three esbuild copies, two of them in the
range GHSA-67mh-4wv8-2f99 and GHSA-g7r4-m6w7-qqqr name: 0.18.20 behind
the deprecated @esbuild-kit packages that drizzle-kit still pulls in, and
0.27.7 at the top level. A resolutions entry collapses all three to
0.28.2, which is outside both advisories.

Every workflow that touches bindings/javascript installs with yarn, so
package-lock.json was a second lockfile that nothing consumed and that
nothing kept honest -- it had drifted to esbuild 0.27.7 and to a
playwright version the resolutions block pins away from. Worse, npm
rewrites an existing yarn.lock into Yarn 1 format whenever it runs, so
the release script's `npm install` was silently downgrading the lockfile
that CI actually reads, and only the `yarn install` that followed put it
back. Remove the file, ignore it so it does not reappear, and drop the
npm install from the release script.

Tests: yarn install --immutable; yarn workspace @tursodatabase/database-common
build && test; yarn workspace @tursodatabase/database-wasm bundle (vite,
which is what consumes esbuild); yarn npm audit --all --recursive reports
no advisories, only deprecation notices.
2026-08-30 09:25:10 +03:00

339 lines
11 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Script to update version in workspace Cargo manifests, package.json,
package-lock.json, gradle.properties, and Directory.Build.props files for the
Turso project. This script updates all occurrences of the version in the
workspace configuration, updates the JavaScript and WebAssembly bindings
package.json and package-lock.json files, updates the Java bindings
gradle.properties file, updates the .NET bindings Directory.Build.props file,
uses cargo update to update Cargo.lock, creates a git commit, and adds a
version tag.
"""
import argparse
import json
import os
import re
import subprocess
import sys
from pathlib import Path
# Define all npm package paths in one place
NPM_PACKAGES = [
"bindings/javascript",
"bindings/javascript/packages/common",
"bindings/javascript/packages/native",
"bindings/javascript/packages/wasm",
"bindings/javascript/packages/wasm-common",
"bindings/javascript/sync/packages/common",
"bindings/javascript/sync/packages/native",
"bindings/javascript/sync/packages/wasm",
"bindings/react-native",
"packages/turso-cli",
"packages/turso-cli/npm/darwin-arm64",
"packages/turso-cli/npm/darwin-x64",
"packages/turso-cli/npm/linux-arm64-gnu",
"packages/turso-cli/npm/linux-x64-gnu",
"packages/turso-cli/npm/win32-x64-msvc",
]
NPM_WORKSPACE_PACKAGES = [
"@tursodatabase/database-common",
"@tursodatabase/database-wasm-common",
"@tursodatabase/sync-common",
"@tursodatabase/cli-darwin-arm64",
"@tursodatabase/cli-darwin-x64",
"@tursodatabase/cli-linux-arm64-gnu",
"@tursodatabase/cli-linux-x64-gnu",
"@tursodatabase/cli-win32-x64-msvc",
]
NPM_WORKSPACES = [
"bindings/javascript"
]
JAVA_GRADLE_PROPERTIES = "bindings/java/gradle.properties"
DOTNET_DIRECTORY_BUILD_PROPS = "bindings/dotnet/Directory.Build.props"
def iter_cargo_toml_files():
"""Yield all Cargo.toml manifests in the repo, excluding build output."""
for path in sorted(Path(".").rglob("Cargo.toml")):
if "target" in path.parts:
continue
yield path
def parse_args():
parser = argparse.ArgumentParser(description="Update version in project files")
# Version argument
parser.add_argument("version", help="The new version to set (e.g., 0.1.0)")
args = parser.parse_args()
validate_version(args.version)
return args
def validate_version(version):
pattern = r"^\d+\.\d+\.\d+(-[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*)?$"
if not re.match(pattern, version):
print(f"Error: invalid version format '{version}'", file=sys.stderr)
print("Expected format: MAJOR.MINOR.PATCH[-pre.RELEASE] (e.g., 0.6.0, 0.6.0-pre.123)", file=sys.stderr)
sys.exit(1)
if version.startswith("v"):
print("Error: version should not start with 'v' (e.g., use 0.6.0, not v0.6.0)", file=sys.stderr)
sys.exit(1)
def extract_current_version(content):
"""Extract the current version from Cargo.toml content."""
match = re.search(r'workspace\.package\]\s*\nversion\s*=\s*"([^"]+)"', content)
if not match:
sys.exit(1)
return match.group(1)
def update_cargo_toml(new_version):
"""Update all version references in Cargo.toml to the new version."""
try:
cargo_path = Path("Cargo.toml")
if not cargo_path.exists():
sys.exit(1)
content = cargo_path.read_text()
current_version = extract_current_version(content)
# Pattern to match version in various contexts while maintaining the quotes
pattern = r'(version\s*=\s*)"' + re.escape(current_version) + r'"'
updated_content = re.sub(pattern, rf'\1"{new_version}"', content)
cargo_path.write_text(updated_content)
return True
except Exception:
sys.exit(1)
def update_package_json(dir_path, new_version): # noqa: C901
"""Update version in package.json and package-lock.json files."""
dir_path = Path(dir_path)
# Update package.json
try:
package_path = dir_path / "package.json"
if not package_path.exists():
return False
# Read and parse the package.json file
with open(package_path, "r") as f:
package_data = json.load(f)
# Update version regardless of current value
package_data["version"] = new_version
if "dependencies" in package_data:
for dependency in package_data["dependencies"].keys():
if dependency not in NPM_WORKSPACE_PACKAGES:
continue
package_data["dependencies"][dependency] = f"^{new_version}"
if "optionalDependencies" in package_data:
for dependency in package_data["optionalDependencies"].keys():
if dependency not in NPM_WORKSPACE_PACKAGES:
continue
package_data["optionalDependencies"][dependency] = new_version
# Write updated package.json
with open(package_path, "w") as f:
json.dump(package_data, f, indent=2)
except Exception:
return False
# Update package-lock.json if it exists
try:
lock_path = dir_path / "package-lock.json"
if not lock_path.exists():
return True # package.json was updated successfully
# Read and parse the package-lock.json file
with open(lock_path, "r") as f:
lock_data = json.load(f)
# Update version in multiple places in package-lock.json
if "version" in lock_data:
lock_data["version"] = new_version
# Update version in packages section if it exists (npm >= 7)
if "packages" in lock_data:
if "" in lock_data["packages"]: # Root package
if "version" in lock_data["packages"][""]:
lock_data["packages"][""]["version"] = new_version
# Update version in dependencies section if it exists (older npm)
package_name = package_data.get("name", "")
if "dependencies" in lock_data and package_name in lock_data["dependencies"]:
if "version" in lock_data["dependencies"][package_name]:
lock_data["dependencies"][package_name]["version"] = new_version
# Write updated package-lock.json
with open(lock_path, "w") as f:
json.dump(lock_data, f, indent=2)
return True
except Exception:
return False
def run_yarn_install(path):
"""Run yarn install to update yarn-lock.json"""
try:
# Run cargo update showing its output with verbose flag
print(f"Info: run yarn install at path {path}")
subprocess.run(["yarn", "install"], check=True, cwd=path)
return True
except Exception:
return False
def update_all_packages(new_version):
"""Update all npm packages with the new version."""
results = []
for package_path in NPM_PACKAGES:
result = update_package_json(package_path, new_version)
results.append((package_path, result))
for workspace_path in NPM_WORKSPACES:
run_yarn_install(workspace_path)
return results
def update_gradle_properties(new_version):
"""Update version in gradle.properties file."""
try:
gradle_path = Path(JAVA_GRADLE_PROPERTIES)
if not gradle_path.exists():
print(f"Warning: {JAVA_GRADLE_PROPERTIES} not found, skipping Java version update")
return False
content = gradle_path.read_text()
# Pattern to match projectVersion=x.y.z
pattern = r"(projectVersion\s*=\s*)([^\s]+)"
updated_content = re.sub(pattern, rf"\g<1>{new_version}", content)
gradle_path.write_text(updated_content)
print(f"Updated {JAVA_GRADLE_PROPERTIES} to version {new_version}")
return True
except Exception as e:
print(f"Error updating gradle.properties: {e}")
return False
def update_dotnet_props(new_version):
"""Update version in .NET Directory.Build.props file."""
try:
props_path = Path(DOTNET_DIRECTORY_BUILD_PROPS)
if not props_path.exists():
print(f"Warning: {DOTNET_DIRECTORY_BUILD_PROPS} not found, skipping .NET version update")
return False
content = props_path.read_text()
pattern = r"(<Version>)([^<]+)(</Version>)"
updated_content, substitutions = re.subn(
pattern,
rf"\g<1>{new_version}\g<3>",
content,
count=1,
)
if substitutions != 1:
print(f"Error: could not find <Version> in {DOTNET_DIRECTORY_BUILD_PROPS}")
return False
props_path.write_text(updated_content)
print(f"Updated {DOTNET_DIRECTORY_BUILD_PROPS} to version {new_version}")
return True
except Exception as e:
print(f"Error updating Directory.Build.props: {e}")
return False
def run_cargo_update():
"""Run cargo update to update the Cargo.lock file."""
try:
# Run cargo update showing its output with verbose flag
print("Info: run cargo update")
subprocess.run(["cargo", "update", "--workspace", "--verbose"], check=True)
return True
except Exception:
return False
def create_git_commit_and_tag(version):
"""Create a git commit with all changes and add a version tag."""
try:
# Add files that exist and have changes
files_to_add = [str(path) for path in iter_cargo_toml_files()]
files_to_add.append("Cargo.lock")
# Add Java gradle.properties if it exists
if os.path.exists(JAVA_GRADLE_PROPERTIES):
files_to_add.append(JAVA_GRADLE_PROPERTIES)
# Add .NET Directory.Build.props if it exists
if os.path.exists(DOTNET_DIRECTORY_BUILD_PROPS):
files_to_add.append(DOTNET_DIRECTORY_BUILD_PROPS)
# Add all potential package.json and package-lock.json files
for package_path in NPM_PACKAGES:
package_json = f"{package_path}/package.json"
package_lock = f"{package_path}/package-lock.json"
yarn_lock = f"{package_path}/yarn.lock"
if os.path.exists(package_json):
files_to_add.append(package_json)
if os.path.exists(package_lock):
files_to_add.append(package_lock)
if os.path.exists(yarn_lock):
files_to_add.append(yarn_lock)
# Add each file individually
for file in files_to_add:
try:
subprocess.run(["git", "add", file], check=True)
except subprocess.CalledProcessError:
print(f"Warning: Could not add {file} to git")
# Create commit
commit_message = f"Turso {version}"
subprocess.run(["git", "commit", "-m", commit_message], check=True)
# Create tag
tag_name = f"v{version}"
subprocess.run(["git", "tag", "-a", tag_name, "-m", f"Version {version}"], check=True)
return True
except Exception as e:
print(f"Error in git operations: {e}")
return False
def main():
args = parse_args()
new_version = args.version
# Update Cargo.toml
update_cargo_toml(new_version)
# Update all npm packages
update_all_packages(new_version)
# Update Java gradle.properties
update_gradle_properties(new_version)
# Update .NET Directory.Build.props
update_dotnet_props(new_version)
# Update Cargo.lock using cargo update
run_cargo_update()
# Create git commit and tag
create_git_commit_and_tag(new_version)
if __name__ == "__main__":
main()