Add build if changed script (#3543)

* Add build if changed script

* Remove stale comment
This commit is contained in:
Lion - dapplion
2021-12-21 11:11:40 +01:00
committed by GitHub
parent 89dd289798
commit 148ac6f4ea
5 changed files with 53 additions and 0 deletions

3
.gitignore vendored
View File

@@ -56,4 +56,7 @@ packages/lodestar/.tmpdb/
# Git artifacts
packages/cli/.git-data.json
# Build artifacts
.last_build_unixsec
temp/

View File

@@ -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",

View File

@@ -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",

View File

48
scripts/build_if_changed.sh Executable file
View File

@@ -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