Files
rc-compat/patch-dynamic-routes.sh
T

40 lines
1.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/zsh
echo "🛠 Patching all dynamic Next.js route pages to use 'await params'..."
# Find all dynamic folders: anything like /[paramName]/
DYNAMIC_PAGES=$(find src/app -type f -path "*/\[*\]/page.js")
for FILE in $DYNAMIC_PAGES; do
echo "📄 Patching: $FILE"
# Extract folder name, e.g. [partId]
FOLDER=$(basename $(dirname "$FILE"))
# Remove brackets -> partId
PARAM_NAME=${FOLDER//[\[\]]/}
# Make backup
cp "$FILE" "$FILE.bak"
# Replace any direct use of params.PARAM_NAME
# Ensure the file declares: const { paramName } = await params;
# Only insert if not already patched
if ! grep -q "await params" "$FILE"; then
echo " Inserting param extraction: const { $PARAM_NAME } = await params;"
# Insert param extraction after function signature
sed -i '' "s/export default async function \(.*\)({ params }) {/export default async function \1({ params }) {\n const { $PARAM_NAME } = await params;/" "$FILE"
else
echo " ✔ Already patched."
fi
echo " 🔧 Updating references to params.$PARAM_NAME..."
# Replace params.PARAM_NAME → PARAM_NAME
sed -i '' "s/params\.$PARAM_NAME/$PARAM_NAME/g" "$FILE"
done
echo "🎉 All dynamic route pages patched successfully!"