From 59cadfde2b4f870793728e2ac3a94822214701e8 Mon Sep 17 00:00:00 2001
From: CaTasTrOOf <benoit@besnard.biz>
Date: Wed, 2 Feb 2022 16:45:05 +0100
Subject: [PATCH] Modification pre-commit Hook

* Fix regex bug
* Create missing keys on i18n files
---
 .githooks/pre-commit         |  2 +-
 utils/findMissingI18nKeys.sh | 37 ++++++++++++++++--------------------
 2 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/.githooks/pre-commit b/.githooks/pre-commit
index c10e6b8..3171884 100755
--- a/.githooks/pre-commit
+++ b/.githooks/pre-commit
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 
 $(git rev-parse --show-toplevel)/utils/findMissingI18nKeys.sh
 FINDING_MISSING_KEYS=$?
diff --git a/utils/findMissingI18nKeys.sh b/utils/findMissingI18nKeys.sh
index 262e017..d038d6c 100755
--- a/utils/findMissingI18nKeys.sh
+++ b/utils/findMissingI18nKeys.sh
@@ -1,45 +1,40 @@
-#!/bin/sh
+#!/bin/bash
 
 TMP_FILE=temp
 KEY_FILE=keysUsed
 
-grep --include=\*.vue --exclude=\*template.vue -roE -e "\\\$(t|tc)\((\"|')([a-Z.]*)(\"|')(, [a-Z._]*){0,1}\)" . > $TMP_FILE
+# Get all used keys in Vue files
+grep --include=\*.vue --exclude=\*template.vue -roE -e "\\\$(t|tc)\((\"|')([a-zA-Z.]*)(\"|')(, [a-zA-Z._]*){0,1}\)" . > $TMP_FILE
 sed -i -e "s/'/\"/" $TMP_FILE
 sed -i -e "s/'/\"/" $TMP_FILE
-sed -i -E -e 's/\..*:\$(t|tc)\("([a-Z.]*).*$/\2/' $TMP_FILE
+sed -i -E -e 's/\..*:\$(t|tc)\("([a-zA-Z.]*).*$/\2/' $TMP_FILE
 
+# Remove duplicates keys
 sort $TMP_FILE | uniq > $KEY_FILE
 
+# Verify all i18n files
 FILES="i18n/locales/*.json"
+RETURN_CODE=0
 for f in $FILES
 do
     echo "Processing $f file..."
-    missingFile="$(basename $f).missing"
-    if [ -f $missingFile ]
-    then
-        rm -fr $missingFile
-    fi
+    
+    tmp=$(mktemp)
     while read key; do
+        # Verify if key exists
         jq -e ".$key" < $f > /dev/null
         status=$?
+        # If not, create it
         if [ $status -ne 0 ]
         then
-            echo $key >> $missingFile
+            echo "create $key in file $f"
+            filter=".${key} = \"TO_TRANSLATE\""
+            jq --sort-keys "$filter" $f > "$tmp" && mv "$tmp" $f
+            RETURN_CODE=$((RETURN_CODE+1))
         fi
     done < $KEY_FILE
-    # take action on each file. $f store current file name
-    # cat "$f"
 done
 
-MISSING_FILES="*.missing"
-RETURN_CODE=0
-if ls $MISSING_FILES 1> /dev/null 2>&1; then
-    for f in $MISSING_FILES
-    do
-        RETURN_CODE=$((RETURN_CODE+1))
-        echo "Missing keys in $f"
-        cat $f
-    done
-fi
 rm $KEY_FILE $TMP_FILE
+# Return 0 if no key was created, or number of keys added
 exit $RETURN_CODE
-- 
GitLab