types: Update sync-types.sh to improve header management and file copying logic

This commit is contained in:
Carlos Santos 2025-03-11 16:11:06 +01:00
parent ec8f930407
commit 84e5db7309

View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
HEADER_KEY="THIS HEADER IS AUTOGENERATED. DO NOT MODIFY MANUALLY."
HEADER="/** $HEADER_KEY For any changes, please update the '/openvidu-meet/types' directory. */"
@ -11,53 +11,40 @@ TYPES_DIR="../../openvidu-meet-pro/types/src/ce"
add_headers() {
find "$SOURCE_DIR" -type f -name "*.ts" | while IFS= read -r file; do
if ! grep -qF "$HEADER_KEY" "$file"; then
printf "%s\n\n%s" "$HEADER" "$(cat "$file")" > "${file}.tmp" && mv "${file}.tmp" "$file"
printf "%s\n\n%s" "$HEADER" "$(cat "$file")" >"${file}.tmp" && mv "${file}.tmp" "$file"
fi
done
}
remove_headers() {
copy_files_with_headers() {
TARGET_DIR="$1"
mkdir -p "$TARGET_DIR"
find "$SOURCE_DIR" -type f -name "*.ts" | while IFS= read -r file; do
if grep -qF "$HEADER_KEY" "$file"; then
awk -v header_key="$HEADER_KEY" '
BEGIN { skip = 0 }
{
if (skip) {
if ($0 ~ /^$/) { skip = 0 }
next
}
if ($0 ~ /^\/\*\* .*'"$HEADER_KEY"'.*\*\/$/) {
skip = 1
next
}
print
}
' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file"
fi
RELATIVE_PATH="${file#$SOURCE_DIR/}"
DEST_FILE="$TARGET_DIR/$RELATIVE_PATH"
mkdir -p "$(dirname "$DEST_FILE")"
printf "%s\n\n%s" "$HEADER" "$(cat "$file")" >"$DEST_FILE"
done
}
if [[ $1 == "ce" ]]; then
TARGET_DIRS=("$FRONTEND_DIR" "$BACKEND_DIR")
elif [[ $1 == "pro" ]]; then
TARGET_DIRS=("$TYPES_DIR")
if [ "$1" = "ce" ]; then
TARGET_DIRS="$FRONTEND_DIR $BACKEND_DIR"
elif [ "$1" = "pro" ]; then
TARGET_DIRS="$TYPES_DIR"
else
echo "No argument provided. Copying to both CE and PRO"
TARGET_DIRS=("$FRONTEND_DIR" "$BACKEND_DIR" "$TYPES_DIR")
TARGET_DIRS="$FRONTEND_DIR $BACKEND_DIR $TYPES_DIR"
fi
echo "Adding autogenerated comments to files..."
add_headers
echo "Copying files to target directories..."
for TARGET_DIR in "${TARGET_DIRS[@]}"; do
mkdir -p "$TARGET_DIR"
cp -rT "$SOURCE_DIR" "$TARGET_DIR"
for TARGET_DIR in $TARGET_DIRS; do
copy_files_with_headers "$TARGET_DIR"
done
echo "Restoring original files..."
remove_headers
# echo "Adding autogenerated comments to files..."
# add_headers
echo "Types have been synced successfully."