Skip to content

Commit

Permalink
build: 📦 add limbo tools
Browse files Browse the repository at this point in the history
  • Loading branch information
yjaaidi committed Dec 5, 2023
1 parent 972d8ad commit c5fe6f7
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"name": "@whiskmate/source",
"version": "0.0.0",
"license": "MIT",
"scripts": {},
"scripts": {
"autorevert": "tools/autorevert.sh",
"limbo": "tools/limbo.sh",
"tcr": "tools/tcr.sh"
},
"private": true,
"devDependencies": {
"@nx/devkit": "17.1.3",
Expand Down
59 changes: 59 additions & 0 deletions tools/autorevert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash

set -e

if [ $# -ne 1 ]; then
COMMAND=$(basename $0)
echo "Usage: $COMMAND <limit in minutes>"
exit 1
fi

LIMIT=$(( $1 * 60 ))

function logWaiting() {
echo -ne "\r\033[K👀 Waiting for changes to start timer..."
}

logWaiting

while true; do
GIT_STATUS=$(git status --porcelain)

# Start timer when user starts making changes.
if [ -z "$STARTED" ] && [ -n "$GIT_STATUS" ]; then
STARTED=$(date +%s)
fi

# User is making changes.
if [ -n "$STARTED" ]; then

# Compute remaining time.
REMAINING_TIME=$(($STARTED + $LIMIT - $(date +%s)))
# Keep it positive.
REMAINING_TIME=$(($REMAINING_TIME > 0 ? $REMAINING_TIME : 0))

# Log remaining time.
if [ $REMAINING_TIME -gt 60 ]; then
echo -ne "\r\033[K\033[1;32)mReverting in $(($REMAINING_TIME / 60)) minutes and $(($REMAINING_TIME % 60)) seconds...\033[0m"
else
echo -ne "\r\033[K\033[1;31)mReverting in $REMAINING_TIME seconds...\033[0m"
fi

# If user has been making changes for more than $LIMIT, revert:
if [ $(($(date +%s) - $STARTED)) -gt $LIMIT ]; then
git reset --hard > /dev/null
git clean -df > /dev/null
echo -ne "\r\033[K🧹 Changes reverted! Waiting for changes to start timer again."
STARTED=""
fi

# Detect when user stops making changes:
if [ -z "$GIT_STATUS" ]; then
logWaiting
STARTED=""
fi
fi

# We don't need this to be super reactive, 5 seconds is fine.
sleep 5
done
9 changes: 9 additions & 0 deletions tools/limbo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

while :;
do
git fetch;
[ $(git rev-parse HEAD) != $(git rev-parse @{u}) ] && git pull --rebase --autostash && git push;
# We don't need this to be super reactive, 5 seconds is fine.
sleep 5
done
39 changes: 39 additions & 0 deletions tools/tcr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

set -e

if [ $# -lt 1 ]; then
COMMAND=$(basename $0)
echo "Usage:"
echo " $COMMAND <message>"
echo " $COMMAND --fixup <commit>"
exit 1
fi

if [ "$1" == "--fixup" ]; then
shift
COMMIT_FIXUP_SHA="$1"
else
COMMIT_MESSAGE="$1"
fi

function test() {
nx run-many -t test,test-ui
}

function revert() {
git diff --name-only | grep -v '.spec.ts' | xargs git checkout
git clean -df
}

function commit() {
git add .

if [ -n "$COMMIT_FIXUP_SHA" ]; then
git commit --fixup "$COMMIT_FIXUP_SHA"
else
git commit -m "$COMMIT_MESSAGE"
fi
}

test && commit || revert

0 comments on commit c5fe6f7

Please sign in to comment.