-
Notifications
You must be signed in to change notification settings - Fork 0
/
opd.zsh
66 lines (53 loc) · 1.58 KB
/
opd.zsh
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
#!/bin/zsh
opd() {
if [[ "$1" == "v" || "$1" == "-v" ]]; then
echo -e "${BLUE}Open Package Developers (OPD)${NC}"
echo -e "${BOLD}v0.1.1${NC}"
echo -e "${CYAN}comes with OPM${NC}"
fi
if [[ "$1" == "min" ]]; then
min "$2" "$3"
fi
}
min() {
dir="$1"
output_dir="$2"
if [[ "$2" == "" ]];then
output_dir=$dir
fi
mkdir -p "$output_dir"
if [ ! -d "$dir" ]; then
echo "Directory '$dir' not found."
return 1
fi
if [ ! -d "$output_dir" ]; then
mkdir -p "$output_dir"
fi
for file in "$dir"/*; do
if [[ "$file" == *.js ]]; then
minify_js "$file"
elif [[ "$file" == *.css ]]; then
minify_css "$file"
fi
done
echo "${GREEN}Minification completed.${NC}"
}
minify_js() {
local file="$1"
local filename=$(basename -- "$file")
if [[ "$filename" != *.min.js ]]; then
local output_file="$output_dir/${filename%.*}.min.js"
local input_content=$(cat "$file")
curl -X POST -s --data-urlencode "input=$input_content" "https://www.toptal.com/developers/javascript-minifier/api/raw" -o "$output_file"
echo "${YELLOW}Minimized: $filename -> $output_file${NC}"
fi
}
minify_css() {
local file="$1"
local filename=$(basename -- "$file")
if [[ "$filename" != *.min.css ]]; then
local output_file="$output_dir/${filename%.*}.min.css"
cat "$file" | tr -d '\n' | tr -d '[:space:]' > "$output_file"
echo "${YELLOW}Minimized: $filename -> $output_file${NC}"
fi
}