Last active
August 6, 2025 18:56
-
-
Save squatto/9b7d21e4bbcc72fbc56935ce4202ae76 to your computer and use it in GitHub Desktop.
This script intelligently updates the dependencies of a project (yarn/npm, composer, Laravel, etc.)
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
| #!/usr/bin/env zsh | |
| # This script intelligently updates the dependencies of a project. | |
| # | |
| # Update steps: | |
| # 1. Check for the presence of dependency files (yarn.lock, package-lock.json, composer.lock, artisan). | |
| # 2. Create a new git branch for the updates. | |
| # 3. Remove existing dependency directories (vendor, node_modules). | |
| # 4. Update dependencies using composer, yarn, or npm as appropriate. | |
| # 5. If Laravel is detected, update IDE helpers. | |
| # 6. Compile assets if using yarn or npm. | |
| # 7. Add changed files to git. | |
| # 8. Display git status. | |
| # 9. If Laravel is detected, run tests. | |
| # | |
| # --- | |
| # | |
| # Author: Scott Carpenter - https://github.com/squatto | |
| # License: MIT | |
| # Latest Version: https://gist.github.com/squatto/9b7d21e4bbcc72fbc56935ce4202ae76 | |
| [[ -f yarn.lock ]] && is_yarn=true || is_yarn=false | |
| [[ -f package-lock.json ]] && is_npm=true || is_npm=false | |
| [[ -f composer.lock ]] && is_composer=true || is_composer=false | |
| [[ -f artisan ]] && is_laravel=true || is_laravel=false | |
| # does the "prod" script exist in package.json? | |
| has_prod=$(jq -e '.scripts.prod' package.json > /dev/null 2>&1 && echo true || echo false) | |
| # echo the detected states for debugging | |
| echo "Project dependencies:" | |
| echo "- yarn: $is_yarn" | |
| echo "- npm: $is_npm" | |
| echo "- composer: $is_composer" | |
| echo "- laravel: $is_laravel" | |
| echo "- has 'prod' in package.json: $is_laravel" | |
| echo | |
| if [[ ! "$is_yarn" == true && ! "$is_npm" == true && ! "$is_composer" == true && ! "$is_laravel" == true ]]; then | |
| echo -e "\033[31;1mERROR: No dependencies found to update!\033[0m" | |
| exit 1 | |
| fi | |
| if [[ "$is_yarn" == true && "$is_npm" == true ]]; then | |
| echo -e "\033[31;1mERROR: Both yarn.lock and package-lock.json found. Unable to update dependencies!\033[0m" | |
| exit 1 | |
| fi | |
| echo "----------------------------------" | |
| echo | |
| echo -e "\033[4m\033[1mCreating git branch\033[0m" | |
| echo | |
| git checkout -b "update-dependencies-$(date +%Y%m%d)" | |
| echo | |
| echo "----------------------------------" | |
| echo | |
| echo -e "\033[4m\033[1mRemoving dependency directories\033[0m" | |
| echo | |
| [[ -d vendor ]] && rm -rf vendor | |
| [[ -d node_modules ]] && rm -rf node_modules | |
| if [[ "$is_composer" == true ]]; then | |
| echo | |
| echo "----------------------------------" | |
| echo | |
| echo -e "\033[4m\033[1mUpdating composer dependencies\033[0m" | |
| echo | |
| composer update | |
| fi | |
| if [[ "$is_yarn" == true || "$is_npm" == true ]]; then | |
| echo | |
| echo "----------------------------------" | |
| echo | |
| echo -e "\033[4m\033[1mUpdating npm dependencies\033[0m" | |
| echo | |
| if [[ "$is_yarn" == true ]]; then | |
| yarn upgrade | |
| elif [[ "$is_npm" == true ]]; then | |
| npm update | |
| fi | |
| fi | |
| if [[ "$is_laravel" == true ]]; then | |
| echo | |
| echo "----------------------------------" | |
| echo | |
| echo -e "\033[4m\033[1mUpdating Laravel IDE helpers\033[0m" | |
| echo | |
| php artisan ide-helper:generate | |
| php artisan ide-helper:meta | |
| fi | |
| if [[ "$has_prod" == true ]]; then | |
| echo | |
| echo "----------------------------------" | |
| echo | |
| echo -e "\033[4m\033[1mCompiling assets\033[0m" | |
| echo | |
| if [[ "$is_yarn" == true ]]; then | |
| yarn run prod | |
| elif [[ "$is_npm" == true ]]; then | |
| npm run prod | |
| fi | |
| fi | |
| echo | |
| echo "----------------------------------" | |
| echo | |
| echo -e "\033[4m\033[1mAdding changed files to git\033[0m" | |
| echo | |
| if [[ "$is_composer" == true ]]; then | |
| git add composer.lock | |
| fi | |
| if [[ "$is_laravel" == true ]]; then | |
| git add \ | |
| _ide_helper.php _ide_helper_models.php .phpstorm.meta.php \ | |
| public/js public/css public/mix-manifest.json \ | |
| public/vendor | |
| fi | |
| if [[ "$is_yarn" == true ]]; then | |
| git add yarn.lock | |
| elif [[ "$is_npm" == true ]]; then | |
| git add package-lock.json | |
| fi | |
| echo | |
| echo "----------------------------------" | |
| echo | |
| echo -e "\033[4m\033[1mDone!\033[0m" | |
| echo | |
| echo "----------------------------------" | |
| echo | |
| git status | |
| if [[ "$is_laravel" == true ]]; then | |
| echo | |
| echo "----------------------------------" | |
| echo | |
| read -s -k '?Press any key to run Laravel tests or Ctrl+C to skip...' | |
| echo | |
| echo -e "\033[4m\033[1mRunning Laravel tests\033[0m" | |
| echo | |
| php artisan test | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment