Last active
December 29, 2019 05:24
-
-
Save igtm/3148789ff7df73c0444b60e4f92d559b to your computer and use it in GitHub Desktop.
merge-gql-operations.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
PJROOT_DIR=$(pwd) | |
CONFLICT_IDENTIFIER="=======" | |
# expected directory structure | |
# src/gql/queries/*.gql | |
# src/gql/mutations/*.gql | |
# ./schema.graphql | |
# make *.gql conflict like 'git merge' | |
conflictdiff () { | |
diff --unchanged-group-format="%=" --old-group-format="" --new-group-format="%>" --changed-group-format="<<<<<<< previous%c'\\12'%<=======%c'\\12'%>>>>>>>> new%c'\\12'" -wb $1 $2 | |
} | |
export -f conflictdiff | |
# already conflict | |
if grep -qr $CONFLICT_IDENTIFIER $PJROOT_DIR/src/gql | |
then | |
echo -e "\033[31m[graphql] .gql files are conflicted. Firstly, fix below files by hand, and then try again. \033[m" | |
echo "conflicting files ..." | |
grep -nr $CONFLICT_IDENTIFIER $PJROOT_DIR/src/gql | |
exit 1 | |
fi | |
# stash | |
rm -rf $PJROOT_DIR/src/gql_tmp | |
mv -f $PJROOT_DIR/src/gql $PJROOT_DIR/src/gql_tmp | |
# generate *.gql from schema.graphql using 'gql-generator' | |
gqlg --schemaFilePath ../schema.graphql --destDirPath ./src/gql --depthLimit 5 | |
pushd $PJROOT_DIR/src > /dev/null | |
ops=( | |
"queries" | |
"mutations" | |
) | |
for op in "${ops[@]}" ; do | |
rm -rf gql_tmp2 | |
mkdir -p gql_tmp2/$op | |
# make them conflict | |
( ls -l gql/$op ; ls -l gql_tmp/$op ) | awk '{print $9}' | sort | uniq -d | grep .gql | xargs -I{} sh -c "conflictdiff gql_tmp/${op}/{} gql/${op}/{} > gql_tmp2/${op}/{}; cat gql_tmp2/${op}/{} > gql/${op}/{}" | |
rm -rf gql_tmp2 | |
done | |
popd > /dev/null | |
rm -rf $PJROOT_DIR/src/gql_tmp | |
# print if *.gql conflicts | |
if grep -qr $CONFLICT_IDENTIFIER $PJROOT_DIR/src/gql | |
then | |
echo -e "\033[31m[graphql] .gql files are conflicted. fix below files by hand. \033[m" | |
echo "conflicting files ..." | |
grep -nr $CONFLICT_IDENTIFIER $PJROOT_DIR/src/gql | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment