Last active
January 18, 2018 07:13
-
-
Save edtoken/2891d2d5e7d0683cbd18cf09453783bd to your computer and use it in GitHub Desktop.
npm update all packages. This script will update your dependencies and devDependencies on the latest version
This file contains hidden or 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
## This script will update your dependencies and devDependencies on the latest version | |
## taking into account the versions that you need | |
## for example: "material-ui": "^1.0.0-beta.20" -> "^1.0.0-beta.21" | |
## LICENSE: MIT | |
## Author: Eduard Titov, [email protected] | |
## you can add to package.json "scripts": { "update:packages": "python3 npm-update-packages.py" } | |
## using: python3 npm-update-packages.py | |
import os | |
import json | |
import subprocess | |
import shutil | |
def removeLockFile(): | |
try: | |
os.remove('./package-lock.json') | |
print('package-lock.json removed') | |
except OSError: | |
pass | |
def removeNodeModules(): | |
try: | |
shutil.rmtree('./node_modules') | |
print('node_modules removed') | |
except OSError: | |
pass | |
def preapreVersions(obj): | |
# for k in obj: | |
# if obj[k][0] != '^': | |
# obj[k] = "^{v!s}".format(v=obj[k]) | |
return obj | |
def restoreVersions(obj1, obj2): | |
for k in obj2: | |
if obj1[k] and obj1[k][0] == '^': | |
obj2[k] = "^{v!s}".format(v=obj2[k]) | |
return obj2 | |
def updateNpm(args): | |
bashCommand = "npm update {0}".format(args) | |
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE) | |
output, error = process.communicate() | |
print(args, error, output) | |
def installPackages(packages, args): | |
bashCommand = "npm install {0} {1}".format(" ".join(packages), args) | |
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE) | |
output, error = process.communicate() | |
print(args, error, output) | |
removeLockFile() | |
removeNodeModules() | |
with open('./package.json', 'r+') as json_data: | |
d = json.load(json_data) | |
d['dependencies'] = preapreVersions(d['dependencies']) | |
d['devDependencies'] = preapreVersions(d['devDependencies']) | |
json_data.seek(0) | |
json.dump(d, json_data, indent=2) | |
json_data.truncate() | |
dependencies = d['dependencies'] | |
devDependencies = d['devDependencies'] | |
dependenciesList = list(dependencies.keys()) | |
devDependenciesList = list(devDependencies.keys()) | |
updateNpm('--save') | |
print('npm dependencies updated') | |
updateNpm('--save-dev') | |
print('npm devDependencies updated') | |
installPackages(dependenciesList, '--only=prod --save') | |
print('npm dependencies installed first') | |
installPackages(devDependenciesList, '--only=dev --save-dev') | |
print('npm devDependencies installed first') | |
json_data.close() | |
with open('./package.json', 'r+') as json_new_data: | |
d2 = json.load(json_new_data) | |
d2['dependencies'] = restoreVersions(d['dependencies'], d2['dependencies']) | |
d2['devDependencies'] = restoreVersions(d['devDependencies'], d2['devDependencies']) | |
json_new_data.seek(0) | |
json.dump(d2, json_new_data, indent=2) | |
json_new_data.truncate() | |
json_new_data.close() | |
print('DONE') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment