Created
March 2, 2020 12:52
-
-
Save danylkaaa/f0fe26a657b168044835219bfdc0a754 to your computer and use it in GitHub Desktop.
Run cmd if checksum changed
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
#... | |
.lets-checksums |
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
commands: | |
maybe_run_npm_i: | |
description: Runs npm i if needed | |
checksum: | |
- package.json | |
cmd: | | |
bash ./scripts/run-if-checksum-changed.sh maybe_run_npm_i $LETS_CHECKSUM "npm i" |
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
#!/usr/bin/env bash | |
LABEL=$1 | |
NEW_CHECKSUM=$2 | |
CMD=$3 | |
CHECKSUM_FILE_NAME=".lets-checksum" | |
function read_checksum() { | |
if [[ ! -f "$CHECKSUM_FILE_NAME" ]]; then | |
touch "$CHECKSUM_FILE_NAME"; | |
fi; | |
PREV_CHECKSUM=$(grep -m 1 -E "^$1=\w+" "$CHECKSUM_FILE_NAME" | awk -F\= '{print $2}') | |
} | |
function update_checksum() { | |
if [[ ! -f "$CHECKSUM_FILE_NAME" ]]; then | |
touch "$CHECKSUM_FILE_NAME"; | |
fi; | |
LINE_NUMBER=$(echo $(grep -m 1 -E "^$1=\w+" "$CHECKSUM_FILE_NAME" -n | cut -f1 -d:)) | |
if [[ -n "$LINE_NUMBER" ]]; then | |
sed "${LINE_NUMBER}"'s/.*/'"$1"'='"$2"'/' "$CHECKSUM_FILE_NAME" > "$CHECKSUM_FILE_NAME"2 && mv "$CHECKSUM_FILE_NAME"2 "$CHECKSUM_FILE_NAME"; | |
else | |
echo $1=$2 >> "$CHECKSUM_FILE_NAME"; | |
fi; | |
} | |
read_checksum "$LABEL" | |
if [[ "$NEW_CHECKSUM" != "$PREV_CHECKSUM" ]]; then | |
update_checksum "$LABEL" "$NEW_CHECKSUM" | |
eval $CMD | |
fi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment