forked from cloudaper/sketch-git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre-commit
executable file
·98 lines (86 loc) · 2.73 KB
/
pre-commit
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/sh
# pre-commit hook for Sketch files Git tracking
# Derived from https://github.com/cloudaper/sketch-git-hooks
# Set UTF-8 encoding
export LANG=UTF-8
# Needed to work in SourceTree
export PATH=/usr/local/bin:$PATH
# Git repository absolute path
git_root=`git rev-parse --show-toplevel`
# Unzipped Sketch files path
source_root="$git_root/_source"
# Array of all staged files
git_diff=`git diff --name-only --cached | grep ".sketch$"`
# Check if any Sketch file is staged
if [[ ${git_diff[0]} == '' ]]
then
echo "No Sketch file staged."
else
while read staged_sketch
do
# Absolute Sketch file path
sketch="$git_root/$staged_sketch"
# Unzipped Sketch file directory path
source_directory="$source_root/$staged_sketch"
echo "Processing '`basename $sketch`'."
# Delete existing directory
rm -rf "$source_directory"
# If Sketch file wasn't deleted
if ! git diff --summary --cached -- $staged_sketch | grep "delete" 2>&1 > /dev/null
then
# Create directory
if mkdir -p "$source_directory"
then
if cd "$source_directory"
then
# Unzip Sketch file
if unzip -q "$sketch"
then
echo " '`basename $sketch`' unzipped."
# Remove previews to avoid binary conflict.
if [ -e $source_directory/previews/preview.png ]; then
rm "$source_directory/previews/preview.png"
echo " './previews/preview.png' removed."
fi
# Prettify all JSON files
for json_file in $(find . -name "*.json")
do
if python3 -m json.tool "$json_file" "$json_file".pretty
then
if mv "$json_file".pretty "$json_file"
then
echo " '$json_file' prettified."
else
echo " Couldn't move prettified '$json_file'."
rm -rf "$source_directory"
exit 1
fi
else
echo " Couldn't prettify '$json_file'."
rm -rf "$source_directory"
exit 1
fi
done
else
echo " Couldn't unzip '`basename $sketch`'."
rm -rf "$source_directory"
exit 1
fi
cd "$git_root"
else
echo " Couldn't change directory to '$source_directory'."
exit 1
fi
else
echo " Couldn't create directory '$source_directory'."
exit 1
fi
else
rm "$sketch"
echo " '`basename $sketch`' was removed with all unzipped data."
fi
git add "$source_directory/*"
# Unstage working Sketch file
git rm --cached "$sketch"
done <<< "$git_diff"
fi