Skip to content

Commit a5542c0

Browse files
committed
Twekas to sed commands
1 parent d62a1bf commit a5542c0

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

.githooks/pre-commit

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@
77
# cp .githooks/pre-commit .git/hooks/pre-commit
88
# chmod +x .git/hooks/pre-commit
99

10-
1110
# Get the current date in "Month Day, Year" format (e.g., March 23, 2025)
1211
CURRENT_DATE=$(date -u +"%B %d, %Y")
1312

1413
# Function to update frontmatter
1514
update_frontmatter() {
1615
local file=$1
17-
if grep -q "^updated_at:" "$file"; then
18-
# Update the existing updated_at field (macOS and Linux compatible)
19-
sed -i "" -E "s/^updated_at:.*/updated_at: $CURRENT_DATE/" "$file"
20-
else
21-
# Add updated_at field if it doesn't exist (macOS and Linux compatible)
22-
sed -i "" -E "/^---$/a\\
16+
17+
# Ensure the file contains frontmatter
18+
if [[ $(head -n 1 "$file") == "---" ]]; then
19+
if grep -q "^updated_at:" "$file"; then
20+
# Update the existing "updated_at" field
21+
sed -i "" -E "s/^updated_at:.*/updated_at: $CURRENT_DATE/" "$file"
22+
else
23+
# Insert "updated_at" on the second line of the file
24+
sed -i "" "2i\\
2325
updated_at: $CURRENT_DATE
2426
" "$file"
27+
fi
2528
fi
2629
}
2730

@@ -33,3 +36,5 @@ for file in $(git diff --cached --name-only -- '*.mdx'); do
3336
git add "$file"
3437
fi
3538
done
39+
40+

0 commit comments

Comments
 (0)