Skip to content

Instantly share code, notes, and snippets.

@ManoloTonto1
Created September 23, 2023 15:55
Show Gist options
  • Select an option

  • Save ManoloTonto1/a00b196cb190eaa93eaf6ca9e141cbe2 to your computer and use it in GitHub Desktop.

Select an option

Save ManoloTonto1/a00b196cb190eaa93eaf6ca9e141cbe2 to your computer and use it in GitHub Desktop.
MIgration helper script for migrating Next pages to React Server components.
#!/bin/bash
add_use_client_directive() {
local directory="$1"
if [ -z "$directory" ]; then
echo "Usage: $0 <directory>"
return 1
fi
# Use 'find' to locate all TypeScript files in the specified directory and its subfolders
# and then iterate over them.
find "$directory" -type f -name "*.tsx" | while read -r file; do
# Check if the file contains either "use client" or "use server" directive.
if ! grep -q -E "^(\/\/\s*use client|\/\/\s*use server)" "$file"; then
# If not found, add "use client" directive at the beginning of the file.
sed -i '1i\// use client' "$file"
echo "Added 'use client' directive to $file"
else
echo "Directive already exists in $file"
fi
done
}
# Check if a directory argument is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <directory>"
exit 1
fi
# Call the function with the provided folder as an argument
add_use_client_directive "$1"
#!/bin/bash
add_use_client_directive() {
local directory="$1"
if [ -z "$directory" ]; then
echo "Usage: $0 <directory>"
return 1
fi
# Use 'find' to locate all TypeScript files in the specified directory and its subfolders
# and then iterate over them.
find "$directory" -type f -name "*.tsx" | while read -r file; do
# Check if the file contains either "use client" or "use server" directive.
if ! grep -q -E "^(\"use client\"|\"use server\")" "$file"; then
# If not found, add "use client" directive at the beginning of the file.
sed -i '1i"use client"' "$file"
echo "Added 'use client' directive to $file"
else
echo "Directive already exists in $file"
fi
done
}
# Check if a directory argument is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <directory>"
exit 1
fi
# Call the function with the provided folder as an argument
add_use_client_directive "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment