Skip to content

Instantly share code, notes, and snippets.

@jamesjfoong
Created August 12, 2025 07:18
Show Gist options
  • Save jamesjfoong/ffb04acbe94c37d3a8d6be45b55c25bf to your computer and use it in GitHub Desktop.
Save jamesjfoong/ffb04acbe94c37d3a8d6be45b55c25bf to your computer and use it in GitHub Desktop.
Script to migrate help.catapa.com URLs to environment variable
#!/bin/bash
# Find all TypeScript files and process them
find . -name "*.ts" -type f ! -path "*/node_modules/*" ! -path "*/dist/*" | while read -r file; do
# Check if file contains the pattern to replace
if grep -q "https://help\.catapa\.com/" "$file"; then
echo "Processing: $file"
# Create a temporary file
temp_file=$(mktemp)
# Check if file has been modified
file_modified=false
# Process the file line by line
awk '
BEGIN {
header_found = 0
import_added = 0
file_modified = 0
}
# Check for header pattern
/\*\* ============================= \*\// {
header_found = 1
print $0
if (!import_added) {
print "import { environment } from '\''~projects/shared/environments/environment'\'';"
import_added = 1
}
next
}
# Replace URL patterns
{
# Handle single quotes
if (match($0, /'"'"'https:\/\/help\.catapa\.com\/([^'"'"']*)'"'"'/, arr)) {
sub(/'"'"'https:\/\/help\.catapa\.com\/[^'"'"']*'"'"'/, "`${environment.helpCenterUrl}/" arr[1] "`")
file_modified = 1
}
# Handle double quotes
else if (match($0, /"https:\/\/help\.catapa\.com\/([^"]*)"/, arr)) {
sub(/"https:\/\/help\.catapa\.com\/[^"]*"/, "`${environment.helpCenterUrl}/" arr[1] "`")
file_modified = 1
}
# Handle existing backticks
else if (match($0, /`https:\/\/help\.catapa\.com\/([^`]*)`/, arr)) {
sub(/`https:\/\/help\.catapa\.com\/[^`]*`/, "`${environment.helpCenterUrl}/" arr[1] "`")
file_modified = 1
}
print $0
}
END {
# If no header found but file was modified, add import at the beginning
if (!header_found && file_modified && !import_added) {
system("sed -i '\''1i import { environment } from '\''\\'\''~projects/shared/environments/environment'\''\\'\'';\n'\'' " FILENAME)
}
# Exit with status indicating if file was modified
exit file_modified
}
' "$file" > "$temp_file"
# Check if modifications were made
if [ $? -eq 1 ]; then
# If no header exists and we need to add import at the beginning
if ! grep -q "\*\* ============================= \*/" "$file"; then
# Check if import already exists
if ! grep -q "import { environment } from" "$file"; then
echo "import { environment } from '~projects/shared/environments/environment';" > "$temp_file.import"
echo "" >> "$temp_file.import"
cat "$temp_file" >> "$temp_file.import"
mv "$temp_file.import" "$temp_file"
fi
fi
# Replace the original file
mv "$temp_file" "$file"
file_modified=true
else
rm "$temp_file"
fi
# Track modified files
if [ "$file_modified" = true ]; then
echo "$file" >> modified_files.txt
fi
fi
done
# Run ESLint on modified files to fix import sorting
if [ -f modified_files.txt ]; then
echo "Running ESLint on modified files..."
cat modified_files.txt | xargs yarn eslint --fix
# Clean up
rm modified_files.txt
echo "✅ Script completed successfully!"
else
echo "No files were modified."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment