|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import json |
| 6 | +import pathlib |
| 7 | + |
| 8 | +# Root of the repository |
| 9 | +REPO_ROOT = pathlib.Path(__file__).parent.parent.resolve() |
| 10 | + |
| 11 | + |
| 12 | +def bump_toplevel_package(): |
| 13 | + """This script is meant to run as after-bump-version hook in jupyter-releaser |
| 14 | +
|
| 15 | + The script esentially will check the new bumped version of one of the packages |
| 16 | + in the monorepo and uses this version to bump the version in the toplevel |
| 17 | + package.json. |
| 18 | +
|
| 19 | + This script assumes that _all_ packages in the monorepo share the same version and |
| 20 | + are bumped all together. The script ensures the version toplevel package.json is |
| 21 | + consistent with versions of packages |
| 22 | + """ |
| 23 | + # # Check if package_path is provided as CLI arg |
| 24 | + # if len(sys.argv) < 2: |
| 25 | + # print("Package path not found. Usage: bump-package.py <path_to_package>") |
| 26 | + # os.exit(1) |
| 27 | + |
| 28 | + # # Get package path of one of the packages in monorepo |
| 29 | + # # We check for package.json within this package path to get new bumped version |
| 30 | + # package_path = sys.argv[1] |
| 31 | + |
| 32 | + # Get top level package.json |
| 33 | + top_package_json_file = os.path.join(REPO_ROOT, 'package.json') |
| 34 | + |
| 35 | + # Error if package.json does not exist |
| 36 | + if not os.path.exists(top_package_json_file): |
| 37 | + print("No package.json found in the root of the repository. Exiting...") |
| 38 | + os.exit(1) |
| 39 | + |
| 40 | + # Read top level package.json |
| 41 | + top_package_json_file = os.path.join(REPO_ROOT, 'package.json') |
| 42 | + with open(top_package_json_file, 'r') as f: |
| 43 | + top_package_json = json.load(f) |
| 44 | + |
| 45 | + # Get workspaces key where packages are defined |
| 46 | + workspaces = top_package_json['workspaces'] |
| 47 | + |
| 48 | + # Iterate through workspaces and the first package.json that we will find in |
| 49 | + # one of the packages, we read version and break |
| 50 | + bumped_version = '' |
| 51 | + for workspace in workspaces: |
| 52 | + # Remove any glob patterns and trailing slashes for the path |
| 53 | + workspace = workspace.replace('*/', '').replace('*', '').strip('/') |
| 54 | + for path in pathlib.Path(REPO_ROOT).joinpath(workspace).rglob('*.json'): |
| 55 | + if path.name == 'package.json': |
| 56 | + # Read package.json and get version |
| 57 | + with open(path, 'r') as f: |
| 58 | + package_json = json.load(f) |
| 59 | + |
| 60 | + # Get package name |
| 61 | + package_name = package_json['name'] |
| 62 | + |
| 63 | + # Get version |
| 64 | + bumped_version = package_json['version'] |
| 65 | + print(f"Version {bumped_version} fetched from package {package_name}") |
| 66 | + break |
| 67 | + |
| 68 | + if not bumped_version: |
| 69 | + print("Failed to find version from workspaces packges. Exiting...") |
| 70 | + os.exit(1) |
| 71 | + |
| 72 | + # Update top level package.json with new version and dump it back |
| 73 | + top_package_json['version'] = bumped_version |
| 74 | + with open(top_package_json_file, 'w') as f: |
| 75 | + json.dump(top_package_json, f, indent=2) |
| 76 | + f.write('\n') |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + bump_toplevel_package() |
0 commit comments