2019-06-04 16:50:29 +00:00
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
#
|
2020-05-07 10:21:39 +00:00
|
|
|
|
# Check if we need to copy node_modules.
|
|
|
|
|
# (e.g. in case it has been modified after last copy)
|
2019-06-04 16:50:29 +00:00
|
|
|
|
#
|
|
|
|
|
# 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
|
|
|
|
|
|
2020-05-15 23:08:10 +00:00
|
|
|
|
GIT_ROOT=$(cd "${BASH_SOURCE%/*}" && git rev-parse --show-toplevel)
|
|
|
|
|
source "${GIT_ROOT}/scripts/colors.sh"
|
|
|
|
|
|
|
|
|
|
# More concise output from 'time'
|
|
|
|
|
export TIMEFORMAT="Done in: %Es"
|
|
|
|
|
|
2022-01-05 13:32:02 +00:00
|
|
|
|
removeDir() {
|
2020-05-15 23:08:10 +00:00
|
|
|
|
[[ ! -d "${tmp}" ]] && return
|
|
|
|
|
chmod -R u+w "${tmp}"
|
|
|
|
|
rm -rf "${tmp}"
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 13:32:02 +00:00
|
|
|
|
copyNodeModules() {
|
2020-05-15 23:08:10 +00:00
|
|
|
|
local src="${1}"
|
|
|
|
|
local dst="${2}"
|
|
|
|
|
# WARNING: The ../ is there to avoid a Nix builtins.path bug:
|
|
|
|
|
# https://github.com/NixOS/nix/issues/3593
|
2022-07-25 20:38:41 +00:00
|
|
|
|
local tmp=$(mktemp -d -p "$(dirname "${dst}")/../")
|
2020-05-15 23:08:10 +00:00
|
|
|
|
|
|
|
|
|
# We use a temporary directory to use mv as "atomic" change
|
|
|
|
|
trap "removeDir ${tmp}" ERR INT HUP
|
|
|
|
|
|
|
|
|
|
# WARNING: The -L here is crucial to let Metro find modules.
|
|
|
|
|
cp -LRf ${src}/node_modules/. "${tmp}"
|
|
|
|
|
chmod -R +w "${tmp}"
|
|
|
|
|
|
|
|
|
|
# WARNING: We can't de-reference .bin symlinks
|
|
|
|
|
cp -Rf ${src}/node_modules/.bin/. "${tmp}/.bin/"
|
2023-12-11 15:52:23 +00:00
|
|
|
|
# Cmake called by react-native-reanimated 3.3.0 during build creates read-only temporary files.
|
2023-12-18 09:08:34 +00:00
|
|
|
|
chmod -R u+w node_modules/react-native-reanimated/android/ -f || true
|
2020-05-15 23:08:10 +00:00
|
|
|
|
rm -r "${dst}"
|
|
|
|
|
mv -f "${tmp}" "${dst}"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Find files that were modified and should cause a re-copying of node modules.
|
|
|
|
|
# Some files are generated/modified by build processes and should be ignored.
|
2022-01-05 13:32:02 +00:00
|
|
|
|
findFilesNewerThan() {
|
2020-05-15 23:08:10 +00:00
|
|
|
|
local sentinel="${1}"
|
|
|
|
|
local dir="${2}"
|
2022-07-25 20:38:41 +00:00
|
|
|
|
find "${dir}" -type f -writable \
|
|
|
|
|
-newer "${sentinel}" \
|
2020-05-15 23:08:10 +00:00
|
|
|
|
-not -ipath "*/*android/build/*" -prune \
|
|
|
|
|
-not -ipath "*/xcuserdata/*" -prune \
|
|
|
|
|
-not -ipath "*/scripts/.packager.env" \
|
2024-03-22 14:21:44 +00:00
|
|
|
|
\ # generated at runtime by react-native \
|
|
|
|
|
\ # related code https://github.com/facebook/react-native/blob/v0.72.5/packages/react-native/ReactCommon/React-rncore.podspec#L19 \
|
2024-03-07 05:30:36 +00:00
|
|
|
|
-not -path "*/node_modules/react-native/ReactCommon/react/renderer/components/rncore/*" \
|
2024-03-22 14:21:44 +00:00
|
|
|
|
\ # generated at runtime by react-native-config \
|
|
|
|
|
\ # related code https://github.com/lugg/react-native-config/blob/v1.5.0/react-native-config.podspec#L52 \
|
2024-03-07 05:30:36 +00:00
|
|
|
|
-not -path "*/node_modules/react-native-config/ios/ReactNativeConfig/GeneratedDotEnv.m" \
|
2024-03-22 14:21:44 +00:00
|
|
|
|
\ # generated at runtime by react-native from v0.73.x onwards \
|
2024-09-20 09:31:42 +00:00
|
|
|
|
-not -path "*/node_modules/react-native/React/Fabric/RCTThirdPartyFabricComponentsProvider.mm" \
|
2022-10-19 12:54:01 +00:00
|
|
|
|
-print
|
2020-05-15 23:08:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 13:32:02 +00:00
|
|
|
|
nodeModulesUnchanged() {
|
2020-05-15 23:08:10 +00:00
|
|
|
|
local src="$1"
|
|
|
|
|
local dst="$2"
|
|
|
|
|
local sentinelFile="${dst}/.copied~"
|
2019-06-04 16:50:29 +00:00
|
|
|
|
|
2019-09-13 13:45:24 +00:00
|
|
|
|
# Check if node_modules exists and is valid
|
2020-05-15 23:08:10 +00:00
|
|
|
|
if [[ ! -f "${sentinelFile}" ]]; then
|
|
|
|
|
# node_modules have not been created by this script
|
|
|
|
|
echo -e "${YLW}Node modules not created by Nix${RST}" >&2
|
|
|
|
|
return 1
|
2019-06-04 16:50:29 +00:00
|
|
|
|
fi
|
|
|
|
|
|
2020-05-15 23:08:10 +00:00
|
|
|
|
# Sentinel file holds location of the node_modules source in Nix store
|
2022-07-25 20:38:41 +00:00
|
|
|
|
currentNixSrc="$(cat "${sentinelFile}")"
|
2023-12-11 15:52:23 +00:00
|
|
|
|
|
2020-05-15 23:08:10 +00:00
|
|
|
|
if [ "${currentNixSrc}" != "${src}" ]; then
|
|
|
|
|
echo -e "${YLW}Yarn modules changed, copying new version over${RST}" >&2
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Some build processes modify files in node_modules
|
|
|
|
|
modifiedFiles=($(findFilesNewerThan "${sentinelFile}" "${dst}"))
|
|
|
|
|
if [ ${#modifiedFiles[@]} -ne 0 ]; then
|
|
|
|
|
echo -e "${YLW}Changes detected in node_modules:${RST} ${#modifiedFiles[@]}" >&2
|
|
|
|
|
# Print files that have changes
|
|
|
|
|
for file in ${modifiedFiles[@]}; do
|
2022-07-25 20:38:41 +00:00
|
|
|
|
echo "- $(realpath --relative-to="${dst}" "${file}")" >&2
|
2020-05-15 23:08:10 +00:00
|
|
|
|
done
|
|
|
|
|
return 1
|
2019-06-04 16:50:29 +00:00
|
|
|
|
fi
|
2020-05-15 23:08:10 +00:00
|
|
|
|
|
|
|
|
|
echo -e "${GRN}No changes detected.${RST}" >&2
|
|
|
|
|
return 0
|
2019-09-13 13:45:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 13:32:02 +00:00
|
|
|
|
replaceNodeModules() {
|
2020-05-15 23:08:10 +00:00
|
|
|
|
local src="$1"
|
|
|
|
|
local dst="$2"
|
|
|
|
|
local sentinelFile="${dst}/.copied~"
|
|
|
|
|
|
2022-07-25 20:38:41 +00:00
|
|
|
|
if nodeModulesUnchanged "${src}" "${dst}"; then
|
2020-05-15 23:08:10 +00:00
|
|
|
|
return
|
2023-12-11 15:52:23 +00:00
|
|
|
|
fi
|
2020-05-15 23:08:10 +00:00
|
|
|
|
|
|
|
|
|
# Replace node_modules if necessary
|
|
|
|
|
echo "Copying node_modules from Nix store:" >&2
|
|
|
|
|
echo " - ${src}" >&2
|
2022-07-25 20:38:41 +00:00
|
|
|
|
copyNodeModules "${src}" "${dst}"
|
2020-05-15 23:08:10 +00:00
|
|
|
|
echo -n "${src}" > "${sentinelFile}"
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-17 12:37:46 +00:00
|
|
|
|
# Destination folder, Nix sets STATUS_MOBILE_HOME
|
|
|
|
|
dst="$STATUS_MOBILE_HOME/node_modules"
|
2020-05-15 23:08:10 +00:00
|
|
|
|
# Source of Node modules from /nix/store
|
|
|
|
|
src="$1"
|
|
|
|
|
|
|
|
|
|
if [[ ! -d ${src} ]]; then
|
|
|
|
|
echo -e "${RED}No such folder:${RST} ${src}" >&2
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
2019-09-13 13:45:24 +00:00
|
|
|
|
|
2020-05-15 23:08:10 +00:00
|
|
|
|
# Make those available in shell spawned by flock
|
|
|
|
|
export -f replaceNodeModules nodeModulesUnchanged copyNodeModules findFilesNewerThan removeDir
|
2019-09-13 13:45:24 +00:00
|
|
|
|
|
2020-05-15 23:08:10 +00:00
|
|
|
|
mkdir -p "${dst}"
|
|
|
|
|
# Leverage file lock to create an exclusive lock.
|
|
|
|
|
# Otherwise multiple calls to this script would clash.
|
2022-07-25 20:38:41 +00:00
|
|
|
|
flock "${dst}/" sh -c "time replaceNodeModules '${src}' '${dst}'"
|