Created
July 31, 2025 21:34
-
-
Save joelkesler/37c28af3c1861cfdff6150267ac93918 to your computer and use it in GitHub Desktop.
Shell Script to auto-update NX and Angular, plus selected dependancies
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
| #!/bin/bash | |
| set -e # Exit on error | |
| # | |
| # Script created by Joel Kesler - https://github.com/joelkesler | |
| # | |
| # By default, it will update/upgrade Angular v20. | |
| # You can control which version you'd like to use by editing the variable below | |
| # or running the script with the version number selected (for example v19): | |
| # | |
| # $ ./update-angular-and-nx.sh 19 | |
| # | |
| # get current version of angular to upgrade to in an arg. if not provided, use 20 | |
| if [ -z "$1" ]; then | |
| ANGULAR_VERSION="20" | |
| else | |
| ANGULAR_VERSION="$1" | |
| fi | |
| # check if git is installed | |
| if ! command -v git &> /dev/null; then | |
| echo "git could not be found, please install git to use this script." | |
| exit 1 | |
| fi | |
| # make sure git is not in a master or main branch | |
| current_branch=$(git rev-parse --abbrev-ref HEAD) | |
| if [[ "$current_branch" == "master" || "$current_branch" == "main" ]]; then | |
| echo "You are on the '$current_branch' branch." | |
| echo "" | |
| echo "This script adds commits to current branch. Create a new branch and run it there instead." | |
| echo "" | |
| echo "Test your your changes first before merging to master/main." | |
| tput bel | |
| exit 1 | |
| fi | |
| # make sure git it in a clean state | |
| if ! git diff-index --quiet HEAD --; then | |
| echo "Your git working directory is not clean. Please commit or stash your changes before running this script." | |
| tput bel | |
| # exit 1 | |
| fi | |
| printDivider() { | |
| printf %"$COLUMNS"s | tr " " "-" | |
| } | |
| printStep() { | |
| echo "" | |
| echo "" | |
| printDivider | |
| printf "\n$1...\n"; | |
| printDivider | |
| echo "" | |
| echo "" | |
| } | |
| gitCommit() { | |
| local message="${1:-Commit Changes}" | |
| # check if git is not in a clean state or if there are no changes to commit | |
| if test -n "$(git status --porcelain)"; then | |
| git add . | |
| git commit -m "$message" | |
| echo "" | |
| echo "Git Commit added: $1" | |
| else | |
| echo "No changes to commit." | |
| fi | |
| } | |
| runStep() { | |
| local step_command="$1" | |
| # remove "npx nx migrate " from the command to use as the commit message | |
| step_message=$(echo "$step_command" | sed 's/npx nx migrate //') | |
| # if step_message is "latest", set it to "nx" | |
| if [ "$step_message" == "latest" ]; then | |
| step_message="nx" | |
| fi | |
| printStep "Updating $step_message" | |
| $step_command | |
| if [ -f "migrations.json" ]; then | |
| printf "\n\nRunning migrations\n" | |
| npx nx migrate --run-migrations --create-commits --commit-prefix="Upgrade: nx migration" | |
| else | |
| printf "\n\nNo migrations.json found, skipping migrations." | |
| fi | |
| # if it exists, delete the migrations.json file | |
| if [ -f "migrations.json" ]; then | |
| rm migrations.json | |
| echo "Deleted migrations.json file." | |
| fi | |
| printf "\n\nRunning npm install\n\n" | |
| npm install --legacy-peer-deps --silent # use legacy peer deps until all packages are updated | |
| gitCommit "Upgrade Script: $step_message" | |
| } | |
| runStep "npx nx migrate latest" | |
| runStep "npx nx migrate @angular/core@$ANGULAR_VERSION" | |
| runStep "npx nx migrate @angular/cli@$ANGULAR_VERSION" | |
| runStep "npx nx migrate @angular/cdk@$ANGULAR_VERSION" | |
| runStep "npx nx migrate @angular/material@$ANGULAR_VERSION" | |
| runStep "npx nx migrate @schematics/angular@$ANGULAR_VERSION" | |
| runStep "npx nx migrate rxjs" | |
| runStep "npx nx migrate prettier" | |
| runStep "npx nx migrate eslint" | |
| runStep "npx nx migrate @eslint/js" | |
| runStep "npx nx migrate @eslint/eslint" | |
| runStep "npx nx migrate @typescript-eslint/utils" | |
| runStep "npx nx migrate @angular-eslint/schematics@$ANGULAR_VERSION" | |
| runStep "npx nx migrate angular-eslint@$ANGULAR_VERSION" | |
| runStep "npx nx migrate @jest/preset-angular" | |
| # Do a final npm install to ensure all dependencies are up to date | |
| printStep "Final npm install" | |
| npm install --silent | |
| gitCommit "Upgrade Script: update package lock files with npm install" | |
| echo "" | |
| echo "" | |
| printDivider | |
| echo "" | |
| echo "" | |
| echo "Script completed successfully" | |
| echo "" | |
| echo "Please run your app locally to make sure everything is working as expected." | |
| echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment