Skip to content

Instantly share code, notes, and snippets.

@linusg
Last active July 11, 2019 11:07
Show Gist options
  • Save linusg/9c7430dcbc7bcb61eb8ae8b0297fef4e to your computer and use it in GitHub Desktop.
Save linusg/9c7430dcbc7bcb61eb8ae8b0297fef4e to your computer and use it in GitHub Desktop.
Generate `yarn upgrade` commands for all packages with upgradable minor and patch versions
import ast
import shlex
import subprocess
import sys
import semver
PROJECT_DIRECTORY = "." if len(sys.argv) < 2 else sys.argv[1]
def yarn(command: str, cwd=PROJECT_DIRECTORY) -> str:
process = subprocess.Popen(
shlex.split(f"/usr/local/bin/yarn {command}"),
cwd=cwd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = process.communicate()
return stdout.decode()
yarn_outdated = yarn("outdated")
yarn_outdated = yarn_outdated.splitlines()[6:-1]
for package_line in yarn_outdated:
name, current, wanted, latest, package_type, url = package_line.split()
if wanted == "exotic":
continue
current_version_info = semver.parse_version_info(current)
yarn_info_versions = yarn(f"info {name} versions")
# Remove first and last line
yarn_info_versions = "\n".join(yarn_info_versions.splitlines()[1:-1])
# Evaluate list of version strings
versions = ast.literal_eval(yarn_info_versions)
possible_upgrade_versions = []
for version in versions:
version_info = semver.parse_version_info(version)
# Ignore prereleases
if version_info.prerelease is not None:
continue
# Ignore major versions below and above the current one
if version_info.major != current_version_info.major:
continue
# Ignore minor versions below current one and
# patch version below current one if minor versions are equal
if version_info.minor < current_version_info.minor or (
version_info.minor == current_version_info.minor
and version_info.patch <= current_version_info.patch
):
continue
possible_upgrade_versions.append(version)
if not possible_upgrade_versions:
continue
latest_upgrade_version = possible_upgrade_versions[-1]
print(f"yarn upgrade '{name}@^{latest_upgrade_version}'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment