Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build if changed script #3543

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ packages/lodestar/.tmpdb/
# Git artifacts
packages/cli/.git-data.json

# Build artifacts
.last_build_unixsec

temp/
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"build:lib:watch": "lerna run build:lib:watch --parallel",
"build:types:watch": "lerna run build:types:watch --parallel",
"build:watch": "run-p build:lib:watch build:types:watch",
"build:ifchanged": "lerna exec -- ../../scripts/build_if_changed.sh",
"lint": "lerna run lint --no-bail",
"check-types": "lerna run check-types --no-bail",
"coverage": "lerna run coverage --no-bail",
Expand Down
1 change: 1 addition & 0 deletions packages/spec-test-runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"lib/**/*.js.map"
],
"scripts": {
"build": "exit 0",
"check-types": "tsc --noEmit",
"download-spec-tests": "node -r ts-node/register test/downloadTests.ts",
"check-tests": "mocha test/checkTests.test.ts",
Expand Down
Empty file.
48 changes: 48 additions & 0 deletions scripts/build_if_changed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env bash

# Writes the last build time in packages/*/last_build_unixsec
# Then reads the file and checks if there has been any changes since then
# If so, do a build

# Exit on errors
set -e

LAST_BUILD_FILEPATH=.last_build_unixsec

# Check being run from a package directory
[ ! -d ./src ] && echo "Must be run in a package dir, no src"
[ ! -f ./package.json ] && echo "Must be run in a package dir, no package.json"

CURRENT_UNIXSEC=$(date +%s)

if [ -f "$LAST_BUILD_FILEPATH" ]; then
# Exists
LAST_BUILD_UNIXSEC=$(cat $LAST_BUILD_FILEPATH)
SINCE_LAST_BUILD_SEC=$(($CURRENT_UNIXSEC - $LAST_BUILD_UNIXSEC))
SINCE_LAST_BUILD_MIN=$(($SINCE_LAST_BUILD_SEC / 60 + 1))
# Only with DEBUG=true, log math
if [ ! -z "$DEBUG" ]; then
echo "LAST_BUILD_UNIXSEC: $LAST_BUILD_UNIXSEC SINCE_LAST_BUILD_SEC: $SINCE_LAST_BUILD_SEC SINCE_LAST_BUILD_MIN: $SINCE_LAST_BUILD_MIN"
fi

CHANGED_FILES=$(find src package.json -cmin -$SINCE_LAST_BUILD_MIN)
if [ -z "$CHANGED_FILES" ]; then
# Empty, no changes
SHOULD_BUILD=false
else
# Not empty, build
SHOULD_BUILD=true
fi
else
# Does not exist, always build
SHOULD_BUILD=true
fi

# If there are changes, build
if [ "$SHOULD_BUILD" = true ]; then
npm run build
fi

# Persist current time after a successful build
echo $CURRENT_UNIXSEC > $LAST_BUILD_FILEPATH