-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollapse
58 lines (52 loc) · 1.53 KB
/
collapse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
#
# > collapse <
#
# Opposite of prettyprint, but I didn't want to call it uglyprint.
# Code is not ugly.
#
# Flatten a JavaScript object into one line, multiple spaces into single spaces.
function collapsefile {
# distill the path down to a filename
if [[ $1 =~ '/' ]]; then
origin_filename=$(echo "$1" | sed 's/.*\/\(.*\)$/\1/g')
else
origin_filename=$1
fi
# split the filename components
if [[ $origin_filename =~ '.' ]]; then
rootname=$(echo "$origin_filename" | sed 's/\(.*\)\..*$/\1/g')
extension=$(echo "$origin_filename" | sed 's/.*\(\..*\)$/\1/g')
else
rootname=$origin_filename
extension=''
fi
if [[ -z "$2" ]]; then
# destination=./ # if there's no default destination, otherwise ...
destination=path/to/your/default/directory
elif [ -d "$2" ]; then
# append a trailing slash if there isn't one
destination=$(echo "$2" | sed 's/\(.*[^\/]\)$/\1\//g')
else
echo "Uh-oh. $2 is not a directory."
exit 1
fi
sed -E -e 's/^}$/ }/g' -e 's/[[:space:]]+/ /g' $1 | tr -d "\n" > "$destination$rootname"_collapsed"$extension"
}
# pass a file as the first argument ...
if [ -f "$1" ]; then
collapsefile "$1" "$2"
exit 0
# ... or, pass a directory as the first argument and batch collapse its files
elif [ -d "$1" ]; then
# append a trailing slash if there isn't one
directory=$(echo "$1" | sed 's/\(.*[^\/]\)$/\1\//g')
for file in "$directory"*; do
collapsefile "$file" "$2"
done
exit 0
else
echo "Uh-oh. $1 is not a file."
exit 1
fi