Skip to content
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
35 changes: 20 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
"memfs": "^4.6.0",
"onchange": "^7.1.0",
"openai": "^4.47.2",
"patch-package": "^8.0.0",
"patch-package": "^8.1.0-canary.1",
"peggy": "^4.0.3",
"portfinder": "^1.0.28",
"prettier": "^2.8.8",
Expand Down
94 changes: 51 additions & 43 deletions scripts/applyPatches.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,71 @@
# This is useful because patch-package does not fail on errors or warnings by default,
# which means that broken patches are easy to miss, and leads to developer frustration and wasted time.

SCRIPTS_DIR=$(dirname "${BASH_SOURCE[0]}")
source "$SCRIPTS_DIR/shellUtils.sh"
OS="$(uname)"
if [[ "$OS" != "Darwin" && "$OS" != "Linux" ]]; then
error "Unsupported OS: $OS"
return 1
fi

# Wrapper to run patch-package.
function patchPackage {
# See if we're in the HybridApp repo
IS_HYBRID_APP_REPO=$(scripts/is-hybrid-app.sh)
NEW_DOT_FLAG="${STANDALONE_NEW_DOT:-false}"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
readonly SCRIPT_DIR
source "$SCRIPT_DIR/shellUtils.sh"

# See if we're in the HybridApp repo
IS_HYBRID_APP_REPO=$(scripts/is-hybrid-app.sh)
readonly IS_HYBRID_APP_REPO
readonly NEW_DOT_FLAG="${STANDALONE_NEW_DOT:-false}"

OS="$(uname)"
if [[ "$OS" == "Darwin" || "$OS" == "Linux" ]]; then
# Wrapper to run patch-package.
function patchPackage() {
if [[ "$IS_HYBRID_APP_REPO" == "true" && "$NEW_DOT_FLAG" == "false" ]]; then
TEMP_PATCH_DIR=$(mktemp -d ./tmp-patches-XXX)
trap 'rm -rf "$TEMP_PATCH_DIR"' RETURN

cp -r ./patches/* "$TEMP_PATCH_DIR"
cp -r ./Mobile-Expensify/patches/* "$TEMP_PATCH_DIR"

npx patch-package --patch-dir "$TEMP_PATCH_DIR" --error-on-fail --color=always
EXIT_CODE=$?

rm -rf "$TEMP_PATCH_DIR"
if ! npx patch-package --patch-dir "$TEMP_PATCH_DIR" --error-on-fail --color=always; then
return 1
fi
else
npx patch-package --error-on-fail --color=always
EXIT_CODE=$?
if ! npx patch-package --error-on-fail --color=always; then
return 1
fi
fi
exit $EXIT_CODE
else
error "Unsupported OS: $OS"
exit 1
fi
}

# Run patch-package and capture its output and exit code, while still displaying the original output to the terminal
TEMP_OUTPUT="$(mktemp)"
patchPackage 2>&1 | tee "$TEMP_OUTPUT"
OUTPUT="$(mktemp)"
trap 'rm -rf "$OUTPUT"' EXIT
patchPackage 2>&1 | tee "$OUTPUT"
EXIT_CODE=${PIPESTATUS[0]}
OUTPUT="$(cat "$TEMP_OUTPUT")"
rm -f "$TEMP_OUTPUT"

# Check if the output contains a warning message
echo "$OUTPUT" | grep -q "Warning:"
WARNING_FOUND=$?

printf "\n"
echo

# Determine the final exit code
if [ "$EXIT_CODE" -eq 0 ]; then
if [ $WARNING_FOUND -eq 0 ]; then
# patch-package succeeded but warning was found
error "It looks like you upgraded a dependency without upgrading the patch. Please review the patch, determine if it's still needed, and port it to the new version of the dependency."
exit 1
else
# patch-package succeeded and no warning was found
success "patch-package succeeded without errors or warnings"
exit 0
fi
if [[ "$EXIT_CODE" -eq 0 ]]; then
# patch-package succeeded, but check for warnings
if grep -q "Warning:" "$OUTPUT"; then
error "It looks like you upgraded a dependency without upgrading the patch. Please review the patch, determine if it's still needed, and port it to the new version of the dependency."
exit 1
else
success "patch-package succeeded without errors or warnings"
exit 0
fi
else
# patch-package failed
error "patch-package failed to apply a patch"
exit "$EXIT_CODE"
ERROR_PATCHES_HAVE_FAILED=$(sed 's/\x1b\[[0-9;]*m//g' < "$OUTPUT" | awk '/The patches for/ {print $4}' | grep -v '^$' | sort -u)
if [[ -n "$ERROR_PATCHES_HAVE_FAILED" ]]; then
error "patch-package failed to apply one or more patches, cleaning failed packages and trying once again."
for PACKAGE in $ERROR_PATCHES_HAVE_FAILED; do
info "patch failed to apply for $PACKAGE. Removing it before reinstall..."
rm -rf "$SCRIPT_DIR/../node_modules/$PACKAGE"
done
npm install --ignore-scripts
if ! patchPackage; then
error "patch-package failed after retry, giving up"
exit 1
fi
fi
error "patch-package failed"
exit 1
fi