Running NPM on Forge when needed
When you add a new site in Forge the deploy script will be some like:
1cd /home/forge/sample.com2git pull origin master3$FORGE_COMPOSER install --no-interaction --prefer-dist --optimize-autoloader45( flock -w 10 9 || exit 16 echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock78if [ -f artisan ]; then9 $FORGE_PHP artisan migrate --force10fi
To make add checks to run the NPM, you need to add the following code after the one above:
1cd $FORGE_SITE_PATH23touch hash_resources.txt45HASH_RESOURCES="$(cat hash_resources.txt)"67find ./resources -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > hash_resources.txt89HASH_NEW_RESOURCES="$(cat hash_resources.txt)"1011if [ "$HASH_RESOURCES" != "$HASH_NEW_RESOURCES" ]; then12 echo "Running: npm"13 npm install14 npm run prod15 rm -rf node_modules16else17 echo "Skipped: npm run prod"18fi
What is does
Create the file that will hold the hash if they do not exist.
Get the current content of the hash file.
Get the hash of all the files inside the resources folder, and save it inside the hash file.
1find ./resources -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum > hash_resources.txt
Note that if you change only your webpack.mix.js (or any other file outside the above cited folder) you need to run it manually in your server
Get the new calculated hash
Then we compare the old hash with the new one. If a single char was changed in the files, it will be a different hash, and then the commands will run.
1if [ "$HASH_RESOURCES" != "$HASH_NEW_RESOURCES" ]; then2 echo "Running: npm"3 npm install --production4 npm run prod5else6 echo "Skipped: npm run prod"7fi
1rm -rf node_modules
.gitignore you need to add the file created above: