40 lines
1.2 KiB
Bash
Executable File
40 lines
1.2 KiB
Bash
Executable File
#!/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!"
|