status-react/nix/mobile/reset-node_modules.sh

83 lines
3.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
#
# Check if we need to copy node_modules (e.g. in case it has been modified after last copy)
#
# The reasoning for a (mostly) read-only node_modules folder:
# ideally wed symlink the folder directly to the Nix store
# so that were guaranteed to have a reproducible source.
# Unfortunately react-native wants to build some stuff after the fact
# and this is incompatible with the concept of a pure Nix package.
# Therefore we copy the whole source to the repo directory,
# allow writing only on the folders where it is absolutely required,
# and therefore we still keep some peace of mind that the rest
# of node_modules is unchanged the rest of the time.
#
set -Eeuo pipefail
deps="$1"
[ -d $deps ] || exit 1
nodeModulesDir="$STATUS_REACT_HOME/node_modules"
needCopyModules=1
sentinelFilePath="$nodeModulesDir/.copied~"
# Check if node_modules exists and is valid
if [ -d "$nodeModulesDir" ]; then
if [ -f "$sentinelFilePath" ]; then
existingPath="$(cat $sentinelFilePath)"
if [ "${existingPath}" != "${deps}" ]; then
echo "Yarn modules changed, copying over new version"
else
echo "Checking for modifications in node_modules..."
modifiedFiles=( $(find $nodeModulesDir -writable -type f -newer $sentinelFilePath \
-not \( -path "$nodeModulesDir/react-native/third-party/*" -prune \
-o -path "$nodeModulesDir/react-native/ReactAndroid/build/*" -prune \
-o -path "$nodeModulesDir/*/android/build/*" -prune \
-o -path "$nodeModulesDir/realm/src/*" -prune \
-o -path './resources/icons/*' -prune \) -print) )
if [ ${#modifiedFiles[@]} -eq 0 ]; then
needCopyModules=0
echo "No modifications detected."
else
echo "Modifications detected in ${#modifiedFiles[@]} files:\n${modifiedFiles[@]}"
fi
fi
fi
if [ $needCopyModules -eq 1 ] && [ -d $nodeModulesDir ]; then
chmod u+w -R $nodeModulesDir
rm -rf $nodeModulesDir
fi
fi
# Replace node_modules if necessary
if [ ! -d "$nodeModulesDir" ]; then
if [ -n "${IN_CI_ENVIRONMENT:-''}" ]; then
# CI jobs might run multiple tasks in parallel, so it is possible that another process is setting up node_modules. In that case, let's wait up to 10 seconds for the .tmp dir goes away
for i in {1..10}; do
if [ -d "$nodeModulesDir.tmp" ]; then
echo "$nodeModulesDir.tmp exists, another process is probably setting up node_modules, waiting for 1 second..."
sleep 1
else
break
fi
done
fi
if [ -d "$nodeModulesDir.tmp" ]; then
echo "$nodeModulesDir.tmp already exists, another shell session is probably currently setting up node_modules. Please try again in a few moments."
ls -al $STATUS_REACT_HOME
exit 1
elif [ ! -d "$nodeModulesDir" ]; then
echo "Copying node_modules from Nix store (${deps}/node_modules)..."
trap "[ -d \"$nodeModulesDir.tmp\" ] && chmod -R u+w \"$nodeModulesDir.tmp\" && rm -rf \"$nodeModulesDir.tmp\"" ERR INT HUP
time cp -HRf --preserve=all ${deps}/node_modules/. "$nodeModulesDir.tmp"
chmod -R u+w "$nodeModulesDir.tmp"
mv -f "$nodeModulesDir.tmp" $nodeModulesDir
echo -n "${deps}" > $sentinelFilePath
trap - ERR INT HUP
echo "Done"
fi
fi