2019-06-04 16:50:29 +00:00
#!/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 we’ d symlink the folder directly to the Nix store
# so that we’ re 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
2019-08-14 13:54:06 +00:00
sentinelFilePath = " $nodeModulesDir /.copied~ "
2019-06-04 16:50:29 +00:00
# Check if node_modules exists and is valid
if [ -d " $nodeModulesDir " ] ; then
2019-08-14 13:54:06 +00:00
if [ -f " $sentinelFilePath " ] ; then
existingPath = " $( cat $sentinelFilePath ) "
if [ " ${ existingPath } " != " ${ deps } " ] ; then
echo "Yarn modules changed, copying over new version"
2019-06-04 16:50:29 +00:00
else
2019-08-14 13:54:06 +00:00
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 './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
2019-06-04 16:50:29 +00:00
fi
fi
2019-08-14 13:54:06 +00:00
if [ $needCopyModules -eq 1 ] && [ -d $nodeModulesDir ] ; then
2019-06-04 16:50:29 +00:00
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
2019-08-14 13:54:06 +00:00
echo -n " ${ deps } " > $sentinelFilePath
2019-06-04 16:50:29 +00:00
trap - ERR INT HUP
echo "Done"
fi
fi