#!/bin/bash
# Define the directory of your Node.js project
if [ -z "$1" ]; then
echo "Usage: $0 <project_directory>"
exit 1
fi
project_directory=$1
# Use find to locate all JavaScript files in the project containing @Controller
js_files=$(find "$project_directory" -type f -name "*.ts" -exec grep -l '@Controller' {} \;)
# Loop through each file with @Controller and use grep to extract URLs from @Get, @Post, etc.
for file in $js_files; do
# echo "Extracting path prefixes and suffixes from $file..."
# Extract path prefix within the @Controller decorator
controller_paths=($(awk -F"'" '/@Controller/{print $2}' $file))
http_methods=($(awk -F"'" '/@(Get|Post|Put|Delete)/{gsub(/.*@/, "", $0); gsub(/\(.*/, "", $0); print toupper($0)}' "$file"))
http_suffixes=($(awk -F"'" '/@(Get|Post|Put|Delete)/{gsub(/.*@/, "", $2);gsub(/".*/, "", $2);print $2}' "$file"))
for controller_path in "${controller_paths[@]}"; do
# echo "Controller Path: $controller_path"
# echo "HTTP Methods: $http_methods"
if [ -n "$http_suffixes" ]; then
for suffix in "${http_suffixes[@]}"; do
echo "- condition:"
echo " method: $http_methods"
full_path="$controller_path$suffix"
echo " pathRegex: $full_path"
echo " name: $http_methods $suffix"
done
else
echo "- condition:"
echo " method: $http_methods"
full_path="$controller_path/"
echo " pathRegex: $full_path"
echo " name: $http_methods $full_path"
fi
done
done
Thanks for posting this @glzmn!