Skip to content

Instantly share code, notes, and snippets.

@amphro
Last active June 3, 2017 00:45
Show Gist options
  • Save amphro/436a0dd409da93a0c9d7aae7815862e3 to your computer and use it in GitHub Desktop.
Save amphro/436a0dd409da93a0c9d7aae7815862e3 to your computer and use it in GitHub Desktop.
Utility for converting JSON files to heads down camel case
#/usr/bin/env bash
##
# Utility for converting json files to heads down camel case
##
if [[ $1 == "help" || $1 == "--help" ]]
then
echo "Usage: case-convert [help] path
This utility will convert SFDX json files from HeadsUpCamelCase to headsDownCamelCase.
By default, this will recursively look for and convert all files named \"sfdx-project.json\"
and \"config/*def*.json\". You can change this behaviour by specifying the directory to
look for specific files to replace.
Replace all
$ case-convert
Replace specific file
$ case-convert sfdx-project.json
Replace files in directory
$ case-convert config/*.json
"
exit 0
fi
REPLACE_FUNC_FILE=/tmp/replacefunc
echo '
function(match, p1, p2, p3) {
return `"${p1.toLowerCase()}${p2}"${p3}:`;
}
' > $REPLACE_FUNC_FILE
jqtool=`which jq`
if [[ $jqtool == "" ]]
then
echo "The jq utility is required. Please install using 'brew install jq' on mac or 'sudo apt-get install jq' on linux"
exit 0;
fi
replace=`which replace`
if [[ $replace == "" ]]
then
echo "Installing replace utility"
npm install --global replace
else
echo "Using replace utility $replace"
fi
function convertOrgPrefs {
file=$1
if [[ ! `more $1 | grep orgPreferences` == '' && `more $1 | grep '"enabled":'` == '' ]]
then
echo "Converting $1 to enable/disable"
enabled=`more $1 | jq '.orgPreferences | to_entries | map(select(.value == true)) | if length > 0 then from_entries | keys | .[] |= (match("([a-zA-Z0-9])([a-zA-Z0-9]+)") | (.captures[0].string|ascii_upcase)+.captures[1].string ) else [] end'`
disabled=`more $1 | jq '.orgPreferences | to_entries | map(select(.value == false)) | if length > 0 then from_entries | keys | .[] |= (match("([a-zA-Z0-9])([a-zA-Z0-9]+)") | (.captures[0].string|ascii_upcase)+.captures[1].string ) else [] end'`
converted=`more $1 | jq ". | del(.orgPreferences) | .+{orgPreferences:{ enabled: $enabled, disabled: $disabled}}"`
echo $converted | jq . > /tmp/orgdef
cp /tmp/orgdef $1
rm /tmp/orgdef
fi
}
if [[ $1 == "" ]]
then
echo ""
echo "Converting sfdx-project.json..."
replace '"([A-Z])(\w*)"(\s*):' '' --function-file=$REPLACE_FUNC_FILE sfdx-project.json
echo ""
echo "Converting all config/*def*.json files..."
replace '"([A-Z])(\w*)"(\s*):' '' --function-file=$REPLACE_FUNC_FILE config/*def*.json
for f in `ls config/*def*.json`; do
convertOrgPrefs $f
done
else
echo ""
echo "Converting $1..."
replace '"([A-Z])(\w*)"(\s*):' '' --function-file=$REPLACE_FUNC_FILE $1
for f in $@; do
convertOrgPrefs $f
done
fi
rm $REPLACE_FUNC_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment