Last active
May 4, 2019 15:56
-
-
Save chadlavi/8e35ee3c3e13c13f036ffa2125563189 to your computer and use it in GitHub Desktop.
a (macOS-targed) bash script to combine multiple named imports from a single library into one statement
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/bash | |
# In this script, the library I maintain for work, "@casecommons/cbp-undercase" | |
# is used. I'll abstract it out in a future version of this so that the syntax | |
# is more like "combine-imports.sh [file] [library name] | |
imports=$(cat $1 | grep '@casecommons/cbp-undercase' | wc -l) | |
if [[ $imports -le 1 ]]; then | |
# next line commented out for scripted use | |
# echo "$1: no imports to combine." | |
exit 0 | |
fi | |
cat $1 | perl -pe " | |
# concatenate multiline imports | |
s/,\n/, /g if /(^import|^ {2}[^:}. )']+,$)/; | |
s/{\n/{/g if /^import/; | |
" | \ | |
perl -0777 -pe ' | |
# if there are any combined imports that dont end in a | |
# comma before the closing bracket, inline those, too | |
s/\n} from/} from/g; | |
' | \ | |
perl -0777 -pe ' | |
# if there are named imports whose "from" is on a newline, fix that | |
s/}\n *from/} from/g; | |
' | \ | |
perl -pe ' | |
# remove errant spaces inside imports | |
s/, +/, /g if /^import/; | |
s/{ +/{ /g if /^import/; | |
s/, *}/ }/g if /^import/; | |
' | \ | |
perl -0777 -pe ' | |
# get rid of blank lines between imports | |
s/\n\nim/\nim/g if /^import/ | |
' | \ | |
# have to add a blank line to the end of the file in case there is none, | |
# otherwise "tail -r" causes problems | |
sed -e '$a\' | \ | |
# add "##" marker after the last import | |
tail -r | awk '!p && /^import/{print "##"; p=1} 1' | tail -r | \ | |
perl -ne ' | |
# find range between the first import and the "##" marker | |
$between = /^import/ .. /^##/; | |
# push each of those lines to a buffer if it starts with "import" | |
push @buff, $_ if $between && /^import/; | |
# sort imports and print them | |
print sort splice(@buff) if $between =~ /E/; | |
# print everything else | |
print unless $between | |
' | \ | |
perl -pe " | |
# chop off the end of the import and replace it with a comma | |
s/ ?} from '\@casecommons\/cbp-undercase'/,/g; | |
" | \ | |
perl -pe ' | |
# chop of the start of the import and pad the beginning and end of lines with "##" | |
s/^import { (.*),$/##\1##/g; | |
' | \ | |
perl -0777 -pe ' | |
# replace ##\n## between imported elements with comma; now all imports for this | |
# library are on a single line, bounded by ## at the ^ and $ | |
s/##\n##/, /g; | |
' | \ | |
perl -pe " | |
# replace remaining ## with the front and back of the import | |
s/^##/import { /g; | |
s/##$/ } from '\@casecommons\/cbp-undercase'/g; | |
" | \ | |
perl -pe ' | |
# clean up any errant double commas | |
s/,+/,/g; | |
# clean up any errant double spaces in import statements | |
s/ +/ /g if /^import/; | |
' | \ | |
perl -pe " | |
# insert newlines back into combined import and indent elements 2 - n | |
s/, /,\n /g if /^import/; | |
# insert a comma and a newline after last element of import | |
s/ }/,\n}/g if /cbp-undercase'$/; | |
" | \ | |
perl -pe ' | |
# move the first element of the import to a new line and indent | |
s/{ /{\n /g if (/^import/ && /,$/); | |
' | \ | |
perl -pe " | |
# add a newline back after the combined import | |
s/(\@casecommons\/cbp-undercase')$/\1\n/g; | |
" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment