Last active
February 11, 2022 10:19
-
-
Save ingvaldlorentzen/616aeb8c6a44522fa291c4fd6df3c312 to your computer and use it in GitHub Desktop.
Get Outdated Dependencies package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
import requests | |
# Hosted on: https://gist.github.com/ingvaldlorentzen/616aeb8c6a44522fa291c4fd6df3c312 | |
# Raw: https://gist.githubusercontent.com/ingvaldlorentzen/616aeb8c6a44522fa291c4fd6df3c312/raw/update_packagejson.py | |
with open('npm_output.json', 'r') as file: | |
outdated_packages = json.load(file) | |
outdated_packages = [ | |
{'package': package, 'current_version': versions['wanted'], 'new_version': versions['latest']} | |
for package, versions in outdated_packages.items() | |
if versions['wanted'] != versions['latest'] | |
] | |
file.close() | |
with open('package.json', 'r') as file: | |
package_json = json.load(file) | |
file.close() | |
# Sort packages alphabetically | |
package_json['dependencies'] = dict(sorted(list(package_json['dependencies'].items()))) | |
package_json['devDependencies'] = dict(sorted(list(package_json['devDependencies'].items()))) | |
package_json['ignoredOutdatedDependencies'] = dict(sorted(list(package_json['ignoredOutdatedDependencies'].items()))) | |
for outdated_package in outdated_packages: | |
outdated_package_name = outdated_package['package'] | |
dependency = package_json['dependencies'].get(outdated_package_name) | |
dev_dependency = package_json['devDependencies'].get(outdated_package_name) | |
# Don't update package_json if ignored, just add info for MR info | |
if ignored_package := package_json.get('ignoredOutdatedDependencies', {}).get(outdated_package_name): | |
outdated_package['reason'] = ignored_package | |
outdated_package['type'] = 'ignored' | |
# Check that dependencies are not duplicated to avoid inconsistent builds | |
elif dependency and dev_dependency: | |
sys.exit(f"{outdated_package_name} found in dependencies and devDependencies.") | |
elif package_json['dependencies'].get(outdated_package_name): | |
package_json['dependencies'][outdated_package_name] = outdated_package['new_version'] | |
outdated_package['type'] = 'dependencies' | |
elif package_json['devDependencies'].get(outdated_package_name): | |
package_json['devDependencies'][outdated_package_name] = outdated_package['new_version'] | |
outdated_package['type'] = 'dev-dependencies' | |
for outdated_package in outdated_packages: | |
response = requests.get(f"https://registry.npmjs.org/{outdated_package['package']}") | |
if response.status_code != 404: | |
response_json = response.json() | |
outdated_package['home_page'] = response_json['homepage'] | |
outdated_package['package_url'] = f"https://www.npmjs.com/package/{outdated_package['package']}" | |
outdated_package['project_urls'] = {} | |
bug_tracker_url = ( | |
response_json.get('bugs') | |
if type(response_json.get('bugs')) is str | |
else response_json.get('bugs', {}).get('url') | |
) | |
if bug_tracker_url: | |
outdated_package['project_urls']['Bug Tracker'] = bug_tracker_url | |
if repository_url := response_json.get('repository', {}).get('url'): | |
outdated_package['project_urls']['Repository'] = repository_url.lstrip('git+').rstrip('.git') | |
else: | |
outdated_package['home_page'] = '' | |
outdated_package['package_url'] = '' | |
outdated_package['project_urls'] = {} | |
with open('package.json', 'w') as file: | |
json.dump(package_json, file, indent=2) | |
file.write('\n') | |
file.close() | |
# Write updates to json file for easy MR description parsing in later stage | |
with open('updates.json', 'w') as file: | |
json.dump(outdated_packages, file, indent=2) | |
file.write('\n') | |
file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment