Created
July 21, 2023 19:00
-
-
Save sfdcale/2c3b65fa76feee95618a0009b4ef0484 to your computer and use it in GitHub Desktop.
Auto Publish community on LWC changes
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
#!/bin/bash | |
# Validates command line arguments to make sure everything looks correct. | |
validateArguments() { | |
if [ "$lwcFolderToWatch" = "" ] || [ "$orgUserNameOrAlias" = "" ] || [ "$communityToPublish" = "" ]; then | |
echo "Error: Incorrect number of arguments. Please provide exactly 3 arguments." | |
exit 1 | |
fi | |
} | |
# Retrieves org base url with org alias passed as argument. Also removes, "https://" part from it. | |
getOrgBaseUrl() { | |
echo $(SF_JSON_TO_STDOUT=true sf org display --target-org $orgUserNameOrAlias --json 2> /dev/null | jq '.result.instanceUrl' -r | awk -F[/:] '{print $4}') | |
} | |
# Validates that we have a non empty org base url. If not, throws error. | |
validateOrgUrl() { | |
if [[ -z "${orgBaseUrl}" || "${orgBaseUrl}" == " " ]]; then | |
echo "There was an error in getting base url of the org. Please verify." | |
exit 1 | |
fi | |
} | |
# Checks if file/directory changed by doing shasum | |
hashChanged() { | |
echo $(mtree -c -K sha256digest -p "$1" | sed '4d' | shasum) | |
} | |
# Checks if there is a network connection open to org from vscode (Code). | |
isNetworkConnOpenToOrg() { | |
echo $(lsof -i | grep -i 'Code\\x20H' | grep -o $orgBaseUrl) | |
} | |
# Polls network connection to check if connection status is as expected. | |
# Expects number of seconds to wait as argument until status changes to expected status. | |
pollNetworkConn() { | |
local noOfSecondsToWait=$1 | |
local expectedConnStatus=$2 | |
local noOfSecondsElapsed=0 | |
local connStatusMatchesExpectedVal=false | |
while [ $noOfSecondsElapsed -lt $noOfSecondsToWait ]; do | |
if [ "$(isNetworkConnOpenToOrg)" = "$expectedConnStatus" ]; then | |
connStatusMatchesExpectedVal=true | |
break | |
else | |
sleep 1 | |
let "noOfSecondsElapsed+=1" | |
fi | |
done | |
echo $connStatusMatchesExpectedVal | |
} | |
# Checks if deploy started in given time limit. | |
hasDeployStartedInTime() { | |
local deployStarted=$(pollNetworkConn $noOfSecondsToWaitForDeployToStart $orgBaseUrl) | |
echo $deployStarted | |
return 0 | |
} | |
# Checks if deploy finished in given time limit. | |
hasDeployFinishedInTime() { | |
local deployFinished=$(pollNetworkConn $noOfSecondsToWaitForDeployToFinish) | |
echo $deployFinished | |
return 0 | |
} | |
# Publishes community. | |
publishCommunity() { | |
sf community publish --name "$communityToPublish" | |
} | |
# Main function that watches and publishes community | |
watchAndDeploy() { | |
local oldHash=$(hashChanged $lwcFolderToWatch) | |
while true; do | |
local newHash=$(hashChanged $lwcFolderToWatch) | |
if [ "$oldHash" != "$newHash" ]; then | |
echo "Change detected" | |
oldHash="$newHash" | |
echo "Waiting for deploy to start in $noOfSecondsToWaitForDeployToStart seconds.." | |
local deployStarted=$(hasDeployStartedInTime) | |
if [ $deployStarted = "true" ]; then | |
echo "Deploy Started" | |
echo "Waiting for deploy to finish in $noOfSecondsToWaitForDeployToFinish seconds.." | |
if [ $(hasDeployFinishedInTime) = "true" ]; then | |
echo "Deploy finished in time. Publishing community now." | |
publishCommunity | |
else | |
echo "Deploy didn't finish in time." | |
fi | |
else | |
echo "Deploy did not start in time." | |
fi | |
fi | |
sleep 1 | |
done | |
} | |
lwcFolderToWatch=$1 | |
orgUserNameOrAlias=$2 | |
communityToPublish=$3 | |
noOfSecondsToWaitForDeployToStart=60 | |
noOfSecondsToWaitForDeployToFinish=180 | |
validateArguments | |
orgBaseUrl=$(getOrgBaseUrl) | |
validateOrgUrl | |
watchAndDeploy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment